ilteris kaplan blog

Archive of blog posts since 2005

April 5, 2008

Wiki

Networkedobjects Projects Moodbox Development Firstphase

#wiki

First Phase / Lighting LEDs

According to the project scenario, users are able to push buttons and light up different colors which are associated to different emotions. So in order to achieve that I am going to use two tricolor LEDs for each box which are connected to 8 different buttons and each of them will symbolize different feelings.

Here is my circuit in a picture:

Here is the basic working video.

Here is the Arduino code(There is still space for lots of optimization):




// declare variables:
int switchPinHappy = 2;      //  digital input pin for a switch
int switchPinSad   = 5;
int switchPinAngry= 6;


byte switchState  = 0;    // the state of the switch

byte yellowCalled = 0;
byte redCalled    = 0;
byte angryCalled  = 0;
// don't forget to put a reminder
// when the millis reach 30 seconds reset the called variables. 


// RGB PINS
int redPin         = 9;  // RED LED
int greenPin       = 10; // GREEN LED
int bluePin        = 11; // BLUE LED

int redVal, greenVal, blueVal;




void setup() {
  Serial.begin(19200); 
  Serial.println("hello");
  pinMode(switchPinSad,    INPUT);
  pinMode(switchPinHappy,  INPUT);      
  pinMode(switchPinAngry, INPUT);
  pinMode(redPin,         OUTPUT);   // set the yellow LED pin to be an output
  pinMode(greenPin,       OUTPUT);      // set the red LED pin to be an output
  pinMode(bluePin,        OUTPUT);      // set the red LED pin to be an output


}

void loop() {
  // read the switch input:
  switchState = checkPressed();

  if ((switchState == 1) && (yellowCalled == 0)) {
   
    Serial.println("yellowCalled");
    goYellow();
   
  } 
  else if ((switchState == 2) && (redCalled == 0)) {
    Serial.println("redCalled");
    goRed();
   }
  else if ((switchState == 3) && (angryCalled ==0)) {
    Serial.println("happyCalled");
    goAngry();
  }
  
  delay(10);
}

void goRed() {
  redCalled     = 1;
  yellowCalled  = 0;
  angryCalled   = 0;

   int redDest    = 250;
  int greenDest  = 135;
  int blueDest   = 20;

  goZero(redDest, greenDest, blueDest);
  
}


void goAngry() {
  yellowCalled = 0;
  redCalled    = 0;
  angryCalled  = 1;
  
  int redDest    = 254;
  int greenDest  = 0;
  int blueDest   = 0;

  goZero(redDest, greenDest, blueDest);
  
  
}
void goYellow() {
  yellowCalled = 1;
  redCalled    = 0;
  angryCalled  = 0;
  
  
  int redDest    = 200;
  int greenDest  = 100;
  int blueDest   = 130;

  goZero(redDest, greenDest, blueDest);
}


void goZero(int destR, int destG, int destB) {

  int counter = 5;


  while((redVal + greenVal + blueVal) > counter + 1) {
    Serial.print("redVal: ");
    Serial.print(redVal);
    Serial.print("\t");
    Serial.print("greenVal: ");
    Serial.print(greenVal);
    Serial.print("\t");
    Serial.print("blueVal: ");
    Serial.println(blueVal);


    if(redVal   <= 1) { 
      redVal   = 1; 
    } 
    else { 
      redVal -= counter;   
    }
    if(greenVal <= 1) { 
      greenVal = 1; 
    } 
    else { 
      greenVal -= counter; 
    }
    if(blueVal  <= 1) { 
      blueVal  = 1; 
    } 
    else { 
      blueVal -= counter;  
    }

    analogWrite(redPin,    redVal);
    analogWrite(greenPin,  greenVal);
    analogWrite(bluePin,   blueVal);
 
  }

  goCurrent(destR, destG, destB);

  // if you go negatives with values they reverse like in a color wheel and go white.

}

void goCurrent(int destR, int destG, int destB) {
  int counter = 2;


  while(redVal+greenVal+blueVal < destR+destG+destB ) {
    Serial.print("redVal: ");
    Serial.print(redVal);
    Serial.print("\t");
    Serial.print("greenVal: ");
    Serial.print(greenVal);
    Serial.print("\t");
    Serial.print("blueVal: ");
    Serial.println(blueVal);



    if(redVal  >= destR) { 
      redVal   = destR; 
    } 
    else { 
      redVal += counter;   
    }

    if(greenVal  >= destG) { 
      greenVal   = destG; 
    } 
    else { 
      greenVal += counter;   
    }

    if(blueVal  >= destB) { 
      blueVal   = destB; 
    } 
    else { 
      blueVal += counter;   
    }


    analogWrite(redPin,    redVal); 
    analogWrite(greenPin,  greenVal);
    analogWrite(bluePin,   blueVal);
    
    
  }

  Serial.print("redVal: ");
    Serial.print(redVal);
    Serial.print("\t");
    Serial.print("greenVal: ");
    Serial.print(greenVal);
    Serial.print("\t");
    Serial.print("blueVal: ");
    Serial.println(blueVal);
}

byte checkPressed() {
  byte happy = digitalRead(switchPinHappy);
  byte sad   = digitalRead(switchPinSad);
  byte angry   = digitalRead(switchPinAngry);

  if(happy == 1) {
    return 1;
  } 
  else if(sad == 1) {
    return 2;
  } 
   else if(angry == 1) {
    return 3;
  } 
  else {
    // do nothing for now
  }

}


Continue Reading

Back to Archive