Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Final Prototype - Arduino Code for interactive redesign of Giovi lamp
- gesture recognition and accelerated color interpolation
- */
- ///////////////////////////////////
- // things to implement in this prototype
- // - smoother brightness transition -done
- // - acceleration for color change -done
- ////////////////////////////////////
- //debugging
- //boolean debug = true;
- //boolean color_debug = true;
- // constants:
- #define DEBUG false
- #define BRDEBUG false
- #define CDEBUG false
- #define MAX_INDEX 144
- #define COLOR_STEP 12
- #define pass_time 1000
- #define sensor_max 550
- #define sensor_min 120
- #define max_distance 150
- #define REDPIN 5
- #define BLUEPIN 3
- #define GREENPIN 6
- int accel = -4;
- // global variables
- //rgb - color
- int red_index, green_index, blue_index;
- int color_index = 0, br_index=255;
- int new_br_index = 255;
- int red = 255, green = 255, blue = 255;
- int new_red=255, new_green=255, new_blue=255;
- int br_change;
- int br_step;
- int br_time;
- //SENSORS:
- //right:
- int right_center, right_up, right_down;
- int rc_distance, ru_distance, rd_distance;
- int rc_time, ru_time, rd_time;
- boolean rc_hand = false, ru_hand = false, rd_hand = false;
- boolean rc_on = false, ru_on = false, rd_on = false;
- //left:
- int left_center, left_up, left_down;
- int lc_distance, lu_distance, ld_distance;
- int lc_time, lu_time, ld_time;
- boolean lc_hand = false, lu_hand = false, ld_hand = false;
- boolean lc_on = false, lu_on = false, ld_on = false;
- boolean br=false;
- int d1,d2,d3,d4,ccw_time, cw_time;
- int diff2, distance2, prev_distance2;
- int diff1, distance1, prev_distance1;
- int abs_displacement, displacement;
- int prev_rc_dist = 150;
- int prev_lc_dist = 150;
- int rc_diff = 0;
- int lc_diff = 0;
- int time;
- boolean cw = false, ccw = false;
- boolean animate = false;
- void setup ()
- {
- Serial.begin(9600);
- pinMode(3, OUTPUT);
- pinMode(4,INPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- analogWrite(REDPIN,255);
- analogWrite(BLUEPIN,255);
- analogWrite(GREENPIN,255);
- }
- void loop ()
- {
- right_down = analogRead(0);
- right_center = analogRead(1);
- right_up = analogRead(2);
- left_up = analogRead(3);
- left_center = analogRead(4);
- left_down = analogRead(5);
- // record time, release after 1 sec
- if( (right_center <= sensor_max) && (right_center >= sensor_min) )
- {
- rc_hand = true;
- //rc_distance = bits_to_cm(right_center);
- rc_on = true;
- rc_time = millis();
- }
- else
- {
- rc_hand = false;
- rc_distance = max_distance;
- }
- if( (right_up <= sensor_max) && (right_up >= sensor_min) )
- {
- ru_hand = true;
- ru_on = true;
- //ru_distance = bits_to_cm(right_up);
- ru_time = millis();
- }
- else
- {
- ru_hand = false;
- ru_distance = max_distance;
- //ccw = false;
- }
- if( (right_down <= sensor_max) && (right_down >= sensor_min) )
- {
- rd_hand = true;
- rd_on = true;
- //rd_distance = bits_to_cm(right_down);
- rd_time = millis();
- }
- else
- {
- rd_hand = false;
- rd_distance = max_distance;
- }
- if( (left_center <= sensor_max) && (left_center >= sensor_min) )
- {
- lc_hand = true;
- lc_on = true;
- //lc_distance = bits_to_cm(left_center);
- lc_time = millis();
- }
- else
- {
- lc_hand = false;
- lc_distance = max_distance;
- }
- if( (left_up <= sensor_max) && (left_up >= sensor_min) )
- {
- lu_hand = true;
- lu_on = true;
- //lu_distance = bits_to_cm(left_up);
- lu_time = millis();
- }
- else
- {
- lu_hand = false;
- lu_distance = max_distance;
- }
- if( (left_down <= sensor_max) && (left_down >= sensor_min) )
- {
- ld_hand = true;
- ld_on = true;
- //ld_distance = bits_to_cm(left_down);
- ld_time = millis();
- }
- else
- {
- ld_hand = false;
- ld_distance = max_distance;
- }
- if(DEBUG)
- {
- Serial.print("left: ");
- Serial.print(lu_hand?"yes":"no");
- Serial.print(" ");
- Serial.print(lc_hand?"yes":"no");
- Serial.print(" ");
- Serial.print(ld_hand?"yes":"no");
- Serial.print("\tright hands: ");
- Serial.print(ru_hand?"yes":"no");
- Serial.print(" ");
- Serial.print(rc_hand?"yes":"no");
- Serial.print(" ");
- Serial.print(rd_hand?"yes":"no");
- // Serial.print("\tright time: ");
- // Serial.print(ru_on?"yes":"no");
- // Serial.print(" ");
- // Serial.print(rc_on?"yes":"no");
- // Serial.print(" ");
- // Serial.print(rd_on?"yes":"no");
- Serial.print("\tcw: ");
- Serial.print(cw?"yes":"no");
- Serial.print("ccw:");
- Serial.print(ccw?"yes":"no");
- // Serial.print("\tm: ");
- // Serial.print(m);
- Serial.print("\tcolor: ");
- Serial.print(color_index);
- Serial.print("\tbrightness: ");
- Serial.println(br_index);
- //
- // Serial.print("right: ");
- // Serial.print(rc_distance);
- // Serial.print("\t");
- // Serial.print(prev_rc_dist);
- // Serial.print("\t");
- // Serial.print(rc_diff);
- //
- //
- // Serial.print("\tleft:\t");
- // Serial.print(lc_distance);
- // Serial.print("\t");
- // Serial.print(prev_lc_dist);
- // Serial.print("\t");
- // Serial.print(lc_diff);
- // Serial.print("\t");
- // Serial.print(displacement);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ACTIONS
- if(
- (ld_hand && rd_hand)
- ||
- (lc_hand && rd_hand) || (ld_hand && rc_hand)
- )
- {
- lc_on = false;
- lu_on = false;
- ld_on = false;
- rc_on = false;
- ru_on = false;
- rd_on = false;
- }
- if(
- (lc_hand && rc_hand) ||
- (lu_hand && rd_hand) ||
- (ld_hand && ru_hand)
- )
- {
- //lc_time = 0;
- //rc_time = 0;
- br=true;
- br_time = millis();
- //debug = false;
- if(BRDEBUG) Serial.print("br in");
- if ((lc_hand && rc_hand))
- {
- distance1 = bits_to_cm(left_center);
- distance2 = bits_to_cm(right_center);
- }
- else if (lu_hand && rd_hand)
- {
- distance1 = bits_to_cm(left_up);
- distance2 = bits_to_cm(right_down);
- }
- else if (ld_hand && ru_hand)
- {
- distance1 = bits_to_cm(left_down);
- distance2 = bits_to_cm(right_up);
- }
- if(BRDEBUG)
- {
- Serial.print("\tdistance 1 and 2: ");
- Serial.print(distance1);
- Serial.print(" ");
- Serial.print(distance2);
- }
- diff1 = distance1 - prev_distance1;
- diff2 = distance2 - prev_distance2;
- if(BRDEBUG)
- {
- Serial.print("\tdiff 1 and 2: ");
- Serial.print(diff1);
- Serial.print(" ");
- Serial.print(diff2);
- }
- if(
- ((diff1>1 && diff1<100) && (diff2>1 && diff2<100))
- ||
- ((diff1<-1 && diff1>-100) && (diff2<-1 && diff2>-100))
- )
- {
- displacement = int((diff1 + diff2)/2);
- //br_change = map(abs(displacement),2,30,0,255);
- abs_displacement = abs(displacement);
- if(abs_displacement >1 && abs_displacement<=2) {
- br_change = 50;
- br_step = 3;
- }
- if(abs_displacement >2 && abs_displacement<=3) {
- br_change = 70;
- br_step = 5;
- }
- if(abs_displacement >3 && abs_displacement<=5) {
- br_change = 100;
- br_step = 8;
- }
- else if (abs_displacement >5 && abs_displacement<=10) {
- br_change = 150;
- br_step = 10;
- }
- else if (abs_displacement >10 && abs_displacement<=15) {
- br_change = 200;
- br_step = 12;
- }
- else if (abs_displacement >15 && abs_displacement<=20) {
- br_change = 230;
- br_step = 15;
- }
- else if (abs_displacement >20 && abs_displacement<=25) {
- br_change = 250;
- br_step = 10;
- }
- //else if (abs_displacement >25 && abs_displacement<=30) br_change = 300;
- }
- else
- {
- displacement = 0;
- br_change = 0;
- }
- if(BRDEBUG)
- {
- Serial.print("\tbr_change: ");
- Serial.println(br_change);
- Serial.print("\tdisplacement: ");
- Serial.println(displacement);
- }
- if((displacement<-1) && (displacement>-50))
- {
- //decrease light
- new_br_index = br_index - br_change;
- new_br_index = constrain(new_br_index, 0, 255);
- while((br_index-new_br_index)>0)
- {
- br_index-=br_step;
- br_index = constrain(br_index, 0, 255);
- //Serial.println(br_index);
- // hello tamer, good work my friend!!!! but dont stress me so much...i am old
- analogWrite(REDPIN, map(br_index,0,255,0,red));
- analogWrite(GREENPIN,map(br_index,0,255,0,green));
- analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
- delay(20);
- }
- }
- else if ((displacement>1) && (displacement<50))
- {
- //increase light
- new_br_index = br_index +br_change;
- new_br_index = constrain(new_br_index, 0, 255);
- while((new_br_index-br_index)>0)
- {
- br_index+=br_step;
- br_index = constrain(br_index, 0, 255);
- //Serial.println(br_index);
- analogWrite(REDPIN, map(br_index,0,255,0,red));
- analogWrite(GREENPIN,map(br_index,0,255,0,green));
- analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
- delay(20);
- }
- }
- prev_distance1 = distance1;
- prev_distance2 = distance2;
- }
- ///// CCW rotation
- //////////////////////////////////////////////////////////////////////
- else if(
- ((rd_on && rc_hand && !rd_hand) ||
- //(rd_on && rc_on && ru_hand) ||
- (rc_on && ru_hand && !rc_hand) ||
- (lu_on && lc_hand && !lu_hand) ||
- (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)
- && !br
- )
- {
- //Serial.println("function in");
- ccw = true;
- d1=(ru_time - rc_time);
- d2=(rc_time - rd_time);
- d3=(lc_time - lu_time);
- d4=(ld_time-lc_time);
- //Serial.println(d1);
- //Serial.println(d2);
- if(d1>0 && d1<500)
- {
- ccw_time =d1;
- }
- else if (d2>0 && d2<500)
- {
- ccw_time = d2;
- }
- else if (d3>0 && d3<500)
- {
- ccw_time = d3;
- }
- else if (d4>0 && d4<500)
- {
- ccw_time = d4;
- }
- //Serial.println(ccw_time);
- //ccw = false;
- shift_color(ccw_time,false);
- }
- ///////////////////////////////////////////// CW Rotation
- else if (
- ((rc_on && rd_hand && !rc_hand) ||
- //(rd_on && rc_on && ru_hand) ||
- (ru_on && rc_hand && !ru_hand) ||
- (lc_on && lu_hand && !lc_hand) ||
- (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)
- && !br
- )
- {
- cw = true;
- d1=(rd_time - rc_time);
- d2=(rc_time - ru_time);
- d3=(lu_time - lc_time);
- d4=(lc_time-ld_time);
- //Serial.println(d1);
- //Serial.println(d2);
- if(d1>0 && d1<500)
- {
- cw_time =d1;
- }
- else if (d2>0 && d2<500)
- {
- cw_time = d2;
- }
- else if (d3>0 && d3<500)
- {
- cw_time = d3;
- }
- else if (d4>0 && d4<500)
- {
- cw_time = d4;
- }
- //Serial.println(ccw_time);
- //ccw = false;
- shift_color(cw_time,true);
- }
- else
- {
- //debug = true;
- prev_distance1=150;
- prev_distance2=150;
- cw=false;
- ccw=false;
- //displacement = 0;
- //prev_distance1=150;
- //prev_distance2=150;
- }
- //////////////////////////////////////////// Time reset
- time= millis();
- if((time-rc_time) > pass_time)
- {
- rc_on = false;
- rc_time = 0;
- }
- if((time-ru_time) > pass_time)
- {
- ru_on = false;
- ru_time = 0;
- }
- if((time-rd_time) > pass_time)
- {
- rd_on = false;
- rd_time = 0;
- }
- if((time-lc_time) > pass_time)
- {
- lc_on = false;
- lc_time = 0;
- }
- if((time-lu_time) > pass_time)
- {
- lu_on = false;
- lu_time = 0;
- }
- if((time-ld_time) > pass_time)
- {
- ld_on = false;
- ld_time = 0;
- }
- if((time-br_time) > 2000)
- {
- br = false;
- br_time = 0;
- }
- // debug
- //Serial.println("this is the end");
- }
- //int red_array []= {255, 255, 255, 254, 255, 255, 255, 204, 186, 115, 0, 127, 127};
- //int green_array[]={255, 255, 255, 179, 127, 70, 0, 0, 0, 8, 0, 0, 127};
- //int blue_array[] ={255, 127, 0, 0, 0, 0, 0, 175, 255, 165, 255, 255, 255};
- //
- //
- int red_array []= {
- 142, 207, 249, 246, 238, 233, 230, 227, 224, 221, 226, 155, 49};
- int green_array[]={
- 174, 215, 251, 248, 242, 237, 226, 188, 158, 128, 121, 10, 10};
- int blue_array[] ={
- 223, 239, 253, 206, 141, 76, 2, 3, 6, 8, 12, 217, 217};
- //
- //index no btw 0 & 120
- //circular interpolation
- int colorpolate (String color, int index)
- {
- int from_min, from_max, to_min,to_max;
- if (index<(MAX_INDEX-COLOR_STEP))
- {
- from_min = (index/COLOR_STEP)*COLOR_STEP;
- from_max = (index/COLOR_STEP + 1)*COLOR_STEP;
- to_min = index/COLOR_STEP;
- to_max = (index/COLOR_STEP)+1;
- }
- else
- {
- from_min = (index/COLOR_STEP)*COLOR_STEP;
- from_max = (index/COLOR_STEP + 1)*COLOR_STEP;
- to_min = index/COLOR_STEP;
- to_max = 0;
- }
- if (color.equals("red"))
- {
- //interpolate red value
- return map(index,from_min,from_max,red_array[to_min], red_array[to_max]);
- }
- else if (color.equals("green"))
- {
- //interpolate green value
- return map(index,from_min,from_max,green_array[to_min], green_array[to_max]);
- }
- else if (color.equals("blue"))
- {
- //interpolate blue value
- return map(index,from_min,from_max,blue_array[to_min], blue_array[to_max]);
- }
- }
- int distance_map [] = {
- 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45, 50};
- int sensout_map []= {
- 501, 434, 379, 338, 307, 286, 229, 198, 174, 153, 135, 127};
- int bits_to_cm (int input)
- {
- //zero case: return 0;
- if (input == 0)
- return 0;
- // variable declarations
- boolean not_found = true;
- int i =0;
- int from_min, from_max;
- int to_min, to_max;
- from_min = 550;
- to_min = 10;
- from_max = sensout_map[i];
- to_max = distance_map[i];
- // calculate interpolation min max values:
- while(not_found)
- {
- if (sensout_map[i]<=input)
- {
- not_found = false;
- from_max = sensout_map[i];
- to_max = distance_map[i];
- if(i == 0)
- return 10;
- }
- else
- {
- from_min = sensout_map[i];
- to_min = distance_map[i];
- if(i<11)
- i++;
- else
- return 50; //map(float(input),float(from_min), 0.0, to_min, 80.0);
- // map( input, 61,40,40,60);
- }
- }
- //perform linear interpolation here
- return int(map(input,from_min, from_max, to_min, to_max));
- }
- // dir : true --> cw, false --> ccw
- void shift_color(int rot_time, boolean dir)
- {
- int vel=0;
- //Change colors in ccw, forward
- if(rot_time<500 && rot_time>= 60)
- vel = map(rot_time,60,300, 20,5);
- if(CDEBUG)
- {
- Serial.println("inside color");
- Serial.println(color_index);
- Serial.println(rot_time);
- Serial.println(vel);
- }
- while(vel>0)
- {
- if(dir)
- {
- color_index -=vel;
- }
- else
- {
- color_index +=vel;
- }
- vel += accel;
- //color_index +=5;
- if(CDEBUG)
- {
- Serial.println("inside color while");
- Serial.println(color_index);
- Serial.println(vel);
- }
- if(color_index>=MAX_INDEX) color_index=0;
- else if (color_index<0) color_index = MAX_INDEX-1;
- new_red = colorpolate("red", color_index);
- new_green = colorpolate("green", color_index);
- new_blue = colorpolate("blue", color_index);
- while(
- ((new_red-red)!=0) ||
- ((new_green-green)!=0) ||
- ((new_blue-blue)!= 0)
- )
- {
- //change values
- if((new_red - red)!=0)
- red += (new_red - red)/abs(new_red - red);
- if((new_green - green)!=0)
- green += (new_green - green)/abs(new_green - green);
- if((new_blue - blue)!=0)
- blue += (new_blue - blue)/abs(new_blue - blue);
- analogWrite(REDPIN, map(br_index,0,255,0,red));
- analogWrite(GREENPIN,map(br_index,0,255,0,green));
- analogWrite(BLUEPIN,map(br_index,0,255,0,blue));
- delay(5);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement