Advertisement
tameraslan

Giovi Lamp

Dec 19th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 KB | None | 0 0
  1. /*
  2. Final Prototype - Arduino Code for interactive redesign of Giovi lamp
  3. gesture recognition and accelerated color interpolation
  4. */
  5.  
  6.  
  7. ///////////////////////////////////
  8. // things to implement in this prototype
  9. // - smoother brightness transition -done
  10. // - acceleration for color change -done
  11. ////////////////////////////////////
  12.  
  13. //debugging
  14. //boolean debug = true;
  15. //boolean color_debug = true;
  16.  
  17.  
  18. // constants:
  19. #define DEBUG false
  20. #define BRDEBUG false
  21. #define CDEBUG false
  22.  
  23. #define MAX_INDEX 144
  24. #define COLOR_STEP 12
  25.  
  26. #define pass_time 1000
  27. #define sensor_max 550
  28. #define sensor_min 120
  29. #define max_distance 150
  30. #define REDPIN 5
  31. #define BLUEPIN 3
  32. #define GREENPIN 6
  33.  
  34. int accel = -4;
  35. // global variables
  36.  
  37. //rgb - color
  38. int red_index, green_index, blue_index;
  39. int color_index = 0, br_index=255;
  40. int new_br_index = 255;
  41.  
  42. int red = 255, green = 255, blue = 255;
  43. int new_red=255, new_green=255, new_blue=255;
  44. int br_change;
  45. int br_step;
  46. int br_time;
  47. //SENSORS:
  48. //right:
  49. int right_center, right_up, right_down;
  50. int rc_distance, ru_distance, rd_distance;
  51. int rc_time, ru_time, rd_time;
  52. boolean rc_hand = false, ru_hand = false, rd_hand = false;
  53. boolean rc_on = false, ru_on = false, rd_on = false;
  54. //left:
  55. int left_center, left_up, left_down;
  56. int lc_distance, lu_distance, ld_distance;
  57. int lc_time, lu_time, ld_time;
  58. boolean lc_hand = false, lu_hand = false, ld_hand = false;
  59. boolean lc_on = false, lu_on = false, ld_on = false;
  60. boolean br=false;
  61. int d1,d2,d3,d4,ccw_time, cw_time;
  62.  
  63. int diff2, distance2, prev_distance2;
  64. int diff1, distance1, prev_distance1;
  65.  
  66. int abs_displacement, displacement;
  67. int prev_rc_dist = 150;
  68. int prev_lc_dist = 150;
  69. int rc_diff = 0;
  70. int lc_diff = 0;
  71.  
  72. int time;
  73. boolean cw = false, ccw = false;
  74. boolean animate = false;
  75.  
  76. void setup ()
  77. {
  78.  
  79. Serial.begin(9600);
  80.  
  81. pinMode(3, OUTPUT);
  82. pinMode(4,INPUT);
  83. pinMode(5, OUTPUT);
  84.  
  85. pinMode(6, OUTPUT);
  86.  
  87.  
  88. analogWrite(REDPIN,255);
  89. analogWrite(BLUEPIN,255);
  90. analogWrite(GREENPIN,255);
  91. }
  92.  
  93. void loop ()
  94. {
  95.  
  96.  
  97. right_down = analogRead(0);
  98. right_center = analogRead(1);
  99. right_up = analogRead(2);
  100. left_up = analogRead(3);
  101. left_center = analogRead(4);
  102. left_down = analogRead(5);
  103.  
  104. // record time, release after 1 sec
  105. if( (right_center <= sensor_max) && (right_center >= sensor_min) )
  106. {
  107. rc_hand = true;
  108. //rc_distance = bits_to_cm(right_center);
  109. rc_on = true;
  110. rc_time = millis();
  111.  
  112. }
  113. else
  114. {
  115. rc_hand = false;
  116. rc_distance = max_distance;
  117. }
  118.  
  119. if( (right_up <= sensor_max) && (right_up >= sensor_min) )
  120. {
  121. ru_hand = true;
  122. ru_on = true;
  123. //ru_distance = bits_to_cm(right_up);
  124. ru_time = millis();
  125. }
  126. else
  127. {
  128. ru_hand = false;
  129. ru_distance = max_distance;
  130. //ccw = false;
  131. }
  132.  
  133. if( (right_down <= sensor_max) && (right_down >= sensor_min) )
  134. {
  135. rd_hand = true;
  136. rd_on = true;
  137. //rd_distance = bits_to_cm(right_down);
  138. rd_time = millis();
  139. }
  140. else
  141. {
  142. rd_hand = false;
  143. rd_distance = max_distance;
  144. }
  145.  
  146. if( (left_center <= sensor_max) && (left_center >= sensor_min) )
  147. {
  148. lc_hand = true;
  149. lc_on = true;
  150. //lc_distance = bits_to_cm(left_center);
  151. lc_time = millis();
  152. }
  153. else
  154. {
  155. lc_hand = false;
  156. lc_distance = max_distance;
  157. }
  158.  
  159. if( (left_up <= sensor_max) && (left_up >= sensor_min) )
  160. {
  161. lu_hand = true;
  162. lu_on = true;
  163. //lu_distance = bits_to_cm(left_up);
  164. lu_time = millis();
  165. }
  166. else
  167. {
  168. lu_hand = false;
  169. lu_distance = max_distance;
  170. }
  171. if( (left_down <= sensor_max) && (left_down >= sensor_min) )
  172. {
  173. ld_hand = true;
  174. ld_on = true;
  175. //ld_distance = bits_to_cm(left_down);
  176. ld_time = millis();
  177. }
  178. else
  179. {
  180. ld_hand = false;
  181. ld_distance = max_distance;
  182. }
  183.  
  184.  
  185. if(DEBUG)
  186. {
  187. Serial.print("left: ");
  188. Serial.print(lu_hand?"yes":"no");
  189. Serial.print(" ");
  190. Serial.print(lc_hand?"yes":"no");
  191. Serial.print(" ");
  192. Serial.print(ld_hand?"yes":"no");
  193.  
  194. Serial.print("\tright hands: ");
  195. Serial.print(ru_hand?"yes":"no");
  196. Serial.print(" ");
  197. Serial.print(rc_hand?"yes":"no");
  198. Serial.print(" ");
  199. Serial.print(rd_hand?"yes":"no");
  200. // Serial.print("\tright time: ");
  201. // Serial.print(ru_on?"yes":"no");
  202. // Serial.print(" ");
  203. // Serial.print(rc_on?"yes":"no");
  204. // Serial.print(" ");
  205. // Serial.print(rd_on?"yes":"no");
  206.  
  207.  
  208. Serial.print("\tcw: ");
  209. Serial.print(cw?"yes":"no");
  210. Serial.print("ccw:");
  211. Serial.print(ccw?"yes":"no");
  212.  
  213. // Serial.print("\tm: ");
  214. // Serial.print(m);
  215. Serial.print("\tcolor: ");
  216. Serial.print(color_index);
  217. Serial.print("\tbrightness: ");
  218. Serial.println(br_index);
  219. //
  220. // Serial.print("right: ");
  221. // Serial.print(rc_distance);
  222. // Serial.print("\t");
  223. // Serial.print(prev_rc_dist);
  224. // Serial.print("\t");
  225. // Serial.print(rc_diff);
  226. //
  227. //
  228. // Serial.print("\tleft:\t");
  229. // Serial.print(lc_distance);
  230. // Serial.print("\t");
  231. // Serial.print(prev_lc_dist);
  232. // Serial.print("\t");
  233. // Serial.print(lc_diff);
  234. // Serial.print("\t");
  235. // Serial.print(displacement);
  236. }
  237.  
  238. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ACTIONS
  239.  
  240. if(
  241. (ld_hand && rd_hand)
  242. ||
  243. (lc_hand && rd_hand) || (ld_hand && rc_hand)
  244.  
  245. )
  246. {
  247. lc_on = false;
  248. lu_on = false;
  249. ld_on = false;
  250. rc_on = false;
  251. ru_on = false;
  252. rd_on = false;
  253.  
  254.  
  255. }
  256.  
  257.  
  258. if(
  259. (lc_hand && rc_hand) ||
  260. (lu_hand && rd_hand) ||
  261. (ld_hand && ru_hand)
  262. )
  263. {
  264. //lc_time = 0;
  265. //rc_time = 0;
  266. br=true;
  267. br_time = millis();
  268.  
  269. //debug = false;
  270. if(BRDEBUG) Serial.print("br in");
  271. if ((lc_hand && rc_hand))
  272. {
  273. distance1 = bits_to_cm(left_center);
  274. distance2 = bits_to_cm(right_center);
  275.  
  276. }
  277. else if (lu_hand && rd_hand)
  278. {
  279. distance1 = bits_to_cm(left_up);
  280. distance2 = bits_to_cm(right_down);
  281.  
  282. }
  283. else if (ld_hand && ru_hand)
  284. {
  285. distance1 = bits_to_cm(left_down);
  286. distance2 = bits_to_cm(right_up);
  287. }
  288. if(BRDEBUG)
  289. {
  290. Serial.print("\tdistance 1 and 2: ");
  291. Serial.print(distance1);
  292. Serial.print(" ");
  293. Serial.print(distance2);
  294. }
  295. diff1 = distance1 - prev_distance1;
  296. diff2 = distance2 - prev_distance2;
  297. if(BRDEBUG)
  298. {
  299. Serial.print("\tdiff 1 and 2: ");
  300. Serial.print(diff1);
  301. Serial.print(" ");
  302. Serial.print(diff2);
  303. }
  304.  
  305. if(
  306. ((diff1>1 && diff1<100) && (diff2>1 && diff2<100))
  307. ||
  308. ((diff1<-1 && diff1>-100) && (diff2<-1 && diff2>-100))
  309. )
  310. {
  311. displacement = int((diff1 + diff2)/2);
  312.  
  313. //br_change = map(abs(displacement),2,30,0,255);
  314. abs_displacement = abs(displacement);
  315. if(abs_displacement >1 && abs_displacement<=2) {
  316. br_change = 50;
  317. br_step = 3;
  318. }
  319. if(abs_displacement >2 && abs_displacement<=3) {
  320. br_change = 70;
  321. br_step = 5;
  322. }
  323. if(abs_displacement >3 && abs_displacement<=5) {
  324. br_change = 100;
  325. br_step = 8;
  326. }
  327. else if (abs_displacement >5 && abs_displacement<=10) {
  328. br_change = 150;
  329. br_step = 10;
  330. }
  331. else if (abs_displacement >10 && abs_displacement<=15) {
  332. br_change = 200;
  333. br_step = 12;
  334. }
  335. else if (abs_displacement >15 && abs_displacement<=20) {
  336. br_change = 230;
  337. br_step = 15;
  338. }
  339. else if (abs_displacement >20 && abs_displacement<=25) {
  340. br_change = 250;
  341. br_step = 10;
  342. }
  343. //else if (abs_displacement >25 && abs_displacement<=30) br_change = 300;
  344.  
  345.  
  346.  
  347. }
  348. else
  349.  
  350. {
  351. displacement = 0;
  352. br_change = 0;
  353. }
  354.  
  355.  
  356. if(BRDEBUG)
  357. {
  358. Serial.print("\tbr_change: ");
  359.  
  360. Serial.println(br_change);
  361. Serial.print("\tdisplacement: ");
  362.  
  363. Serial.println(displacement);
  364.  
  365. }
  366. if((displacement<-1) && (displacement>-50))
  367. {
  368. //decrease light
  369. new_br_index = br_index - br_change;
  370. new_br_index = constrain(new_br_index, 0, 255);
  371.  
  372. while((br_index-new_br_index)>0)
  373. {
  374. br_index-=br_step;
  375. br_index = constrain(br_index, 0, 255);
  376. //Serial.println(br_index);
  377.  
  378. // hello tamer, good work my friend!!!! but dont stress me so much...i am old
  379.  
  380.  
  381. analogWrite(REDPIN, map(br_index,0,255,0,red));
  382. analogWrite(GREENPIN,map(br_index,0,255,0,green));
  383. analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
  384. delay(20);
  385. }
  386.  
  387. }
  388. else if ((displacement>1) && (displacement<50))
  389. {
  390. //increase light
  391. new_br_index = br_index +br_change;
  392. new_br_index = constrain(new_br_index, 0, 255);
  393.  
  394. while((new_br_index-br_index)>0)
  395. {
  396. br_index+=br_step;
  397. br_index = constrain(br_index, 0, 255);
  398. //Serial.println(br_index);
  399.  
  400. analogWrite(REDPIN, map(br_index,0,255,0,red));
  401. analogWrite(GREENPIN,map(br_index,0,255,0,green));
  402. analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
  403. delay(20);
  404. }
  405.  
  406.  
  407. }
  408.  
  409.  
  410. prev_distance1 = distance1;
  411. prev_distance2 = distance2;
  412.  
  413. }
  414. ///// CCW rotation
  415. //////////////////////////////////////////////////////////////////////
  416. else if(
  417. ((rd_on && rc_hand && !rd_hand) ||
  418. //(rd_on && rc_on && ru_hand) ||
  419. (rc_on && ru_hand && !rc_hand) ||
  420. (lu_on && lc_hand && !lu_hand) ||
  421. (lc_on && ld_hand && !lc_hand)) && !(ld_hand && rd_hand) && !(lc_hand && rd_hand) && !(ld_hand && rc_hand) && !(ld_hand && !rd_hand) && !(!ld_hand && rd_hand)
  422. && !br
  423. )
  424. {
  425. //Serial.println("function in");
  426. ccw = true;
  427. d1=(ru_time - rc_time);
  428. d2=(rc_time - rd_time);
  429. d3=(lc_time - lu_time);
  430. d4=(ld_time-lc_time);
  431. //Serial.println(d1);
  432. //Serial.println(d2);
  433. if(d1>0 && d1<500)
  434. {
  435. ccw_time =d1;
  436. }
  437. else if (d2>0 && d2<500)
  438. {
  439. ccw_time = d2;
  440. }
  441. else if (d3>0 && d3<500)
  442. {
  443. ccw_time = d3;
  444. }
  445. else if (d4>0 && d4<500)
  446. {
  447. ccw_time = d4;
  448. }
  449.  
  450. //Serial.println(ccw_time);
  451. //ccw = false;
  452.  
  453. shift_color(ccw_time,false);
  454. }
  455. ///////////////////////////////////////////// CW Rotation
  456. else if (
  457. ((rc_on && rd_hand && !rc_hand) ||
  458. //(rd_on && rc_on && ru_hand) ||
  459. (ru_on && rc_hand && !ru_hand) ||
  460. (lc_on && lu_hand && !lc_hand) ||
  461. (ld_on && lc_hand && !ld_hand)) && !(ld_hand && rd_hand) && !(lc_hand && rd_hand) && !(ld_hand && rc_hand) && !(ld_hand && !rd_hand) && !(!ld_hand && rd_hand)
  462. && !br
  463. )
  464. {
  465. cw = true;
  466. d1=(rd_time - rc_time);
  467. d2=(rc_time - ru_time);
  468. d3=(lu_time - lc_time);
  469. d4=(lc_time-ld_time);
  470. //Serial.println(d1);
  471. //Serial.println(d2);
  472. if(d1>0 && d1<500)
  473. {
  474. cw_time =d1;
  475. }
  476. else if (d2>0 && d2<500)
  477. {
  478. cw_time = d2;
  479. }
  480. else if (d3>0 && d3<500)
  481. {
  482. cw_time = d3;
  483. }
  484. else if (d4>0 && d4<500)
  485. {
  486. cw_time = d4;
  487. }
  488.  
  489. //Serial.println(ccw_time);
  490. //ccw = false;
  491.  
  492. shift_color(cw_time,true);
  493.  
  494.  
  495.  
  496. }
  497.  
  498.  
  499.  
  500. else
  501. {
  502. //debug = true;
  503. prev_distance1=150;
  504. prev_distance2=150;
  505. cw=false;
  506. ccw=false;
  507. //displacement = 0;
  508. //prev_distance1=150;
  509. //prev_distance2=150;
  510. }
  511.  
  512. //////////////////////////////////////////// Time reset
  513. time= millis();
  514. if((time-rc_time) > pass_time)
  515. {
  516. rc_on = false;
  517. rc_time = 0;
  518. }
  519.  
  520. if((time-ru_time) > pass_time)
  521. {
  522. ru_on = false;
  523. ru_time = 0;
  524. }
  525. if((time-rd_time) > pass_time)
  526. {
  527. rd_on = false;
  528. rd_time = 0;
  529. }
  530. if((time-lc_time) > pass_time)
  531. {
  532. lc_on = false;
  533. lc_time = 0;
  534. }
  535. if((time-lu_time) > pass_time)
  536. {
  537. lu_on = false;
  538. lu_time = 0;
  539. }
  540. if((time-ld_time) > pass_time)
  541. {
  542. ld_on = false;
  543. ld_time = 0;
  544. }
  545. if((time-br_time) > 2000)
  546. {
  547. br = false;
  548. br_time = 0;
  549. }
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556. // debug
  557. //Serial.println("this is the end");
  558.  
  559.  
  560.  
  561. }
  562.  
  563.  
  564. //int red_array []= {255, 255, 255, 254, 255, 255, 255, 204, 186, 115, 0, 127, 127};
  565. //int green_array[]={255, 255, 255, 179, 127, 70, 0, 0, 0, 8, 0, 0, 127};
  566. //int blue_array[] ={255, 127, 0, 0, 0, 0, 0, 175, 255, 165, 255, 255, 255};
  567. //
  568.  
  569.  
  570. //
  571. int red_array []= {
  572. 142, 207, 249, 246, 238, 233, 230, 227, 224, 221, 226, 155, 49};
  573. int green_array[]={
  574. 174, 215, 251, 248, 242, 237, 226, 188, 158, 128, 121, 10, 10};
  575. int blue_array[] ={
  576. 223, 239, 253, 206, 141, 76, 2, 3, 6, 8, 12, 217, 217};
  577. //
  578.  
  579. //index no btw 0 & 120
  580. //circular interpolation
  581.  
  582. int colorpolate (String color, int index)
  583. {
  584. int from_min, from_max, to_min,to_max;
  585.  
  586. if (index<(MAX_INDEX-COLOR_STEP))
  587. {
  588. from_min = (index/COLOR_STEP)*COLOR_STEP;
  589. from_max = (index/COLOR_STEP + 1)*COLOR_STEP;
  590. to_min = index/COLOR_STEP;
  591. to_max = (index/COLOR_STEP)+1;
  592. }
  593. else
  594. {
  595. from_min = (index/COLOR_STEP)*COLOR_STEP;
  596. from_max = (index/COLOR_STEP + 1)*COLOR_STEP;
  597. to_min = index/COLOR_STEP;
  598. to_max = 0;
  599.  
  600. }
  601.  
  602.  
  603. if (color.equals("red"))
  604. {
  605. //interpolate red value
  606. return map(index,from_min,from_max,red_array[to_min], red_array[to_max]);
  607.  
  608. }
  609. else if (color.equals("green"))
  610. {
  611. //interpolate green value
  612. return map(index,from_min,from_max,green_array[to_min], green_array[to_max]);
  613. }
  614. else if (color.equals("blue"))
  615. {
  616. //interpolate blue value
  617. return map(index,from_min,from_max,blue_array[to_min], blue_array[to_max]);
  618. }
  619.  
  620. }
  621.  
  622.  
  623.  
  624.  
  625.  
  626. int distance_map [] = {
  627. 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45, 50};
  628. int sensout_map []= {
  629. 501, 434, 379, 338, 307, 286, 229, 198, 174, 153, 135, 127};
  630.  
  631. int bits_to_cm (int input)
  632. {
  633. //zero case: return 0;
  634. if (input == 0)
  635. return 0;
  636.  
  637. // variable declarations
  638. boolean not_found = true;
  639. int i =0;
  640. int from_min, from_max;
  641. int to_min, to_max;
  642. from_min = 550;
  643. to_min = 10;
  644. from_max = sensout_map[i];
  645. to_max = distance_map[i];
  646.  
  647.  
  648.  
  649. // calculate interpolation min max values:
  650. while(not_found)
  651. {
  652. if (sensout_map[i]<=input)
  653. {
  654. not_found = false;
  655. from_max = sensout_map[i];
  656. to_max = distance_map[i];
  657. if(i == 0)
  658. return 10;
  659.  
  660.  
  661. }
  662. else
  663. {
  664. from_min = sensout_map[i];
  665. to_min = distance_map[i];
  666. if(i<11)
  667. i++;
  668. else
  669. return 50; //map(float(input),float(from_min), 0.0, to_min, 80.0);
  670. // map( input, 61,40,40,60);
  671. }
  672. }
  673.  
  674.  
  675. //perform linear interpolation here
  676.  
  677. return int(map(input,from_min, from_max, to_min, to_max));
  678. }
  679.  
  680. // dir : true --> cw, false --> ccw
  681.  
  682. void shift_color(int rot_time, boolean dir)
  683. {
  684.  
  685. int vel=0;
  686.  
  687. //Change colors in ccw, forward
  688. if(rot_time<500 && rot_time>= 60)
  689. vel = map(rot_time,60,300, 20,5);
  690.  
  691.  
  692. if(CDEBUG)
  693. {
  694. Serial.println("inside color");
  695. Serial.println(color_index);
  696. Serial.println(rot_time);
  697. Serial.println(vel);
  698. }
  699. while(vel>0)
  700. {
  701. if(dir)
  702. {
  703. color_index -=vel;
  704. }
  705. else
  706. {
  707. color_index +=vel;
  708. }
  709. vel += accel;
  710.  
  711. //color_index +=5;
  712. if(CDEBUG)
  713. {
  714. Serial.println("inside color while");
  715. Serial.println(color_index);
  716. Serial.println(vel);
  717. }
  718.  
  719. if(color_index>=MAX_INDEX) color_index=0;
  720. else if (color_index<0) color_index = MAX_INDEX-1;
  721.  
  722. new_red = colorpolate("red", color_index);
  723. new_green = colorpolate("green", color_index);
  724. new_blue = colorpolate("blue", color_index);
  725.  
  726.  
  727.  
  728.  
  729. while(
  730. ((new_red-red)!=0) ||
  731. ((new_green-green)!=0) ||
  732. ((new_blue-blue)!= 0)
  733. )
  734. {
  735. //change values
  736. if((new_red - red)!=0)
  737. red += (new_red - red)/abs(new_red - red);
  738. if((new_green - green)!=0)
  739. green += (new_green - green)/abs(new_green - green);
  740. if((new_blue - blue)!=0)
  741. blue += (new_blue - blue)/abs(new_blue - blue);
  742.  
  743. analogWrite(REDPIN, map(br_index,0,255,0,red));
  744. analogWrite(GREENPIN,map(br_index,0,255,0,green));
  745. analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
  746. delay(5);
  747. }
  748. }
  749. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement