Light and Magic

stephen.backer's picture

First off... I'm excited that I got something to work! 

Experience

Push on the FSR to make the motor rev faster and faster. 

The cork spinning on the motor provides an auditory representation of its speed. 

As the speed increases, the diffused LED's shift from Red to Yellow to Green like a traffic light. 

 

Materials

Trusty Diffuser:  composed of cotton, cut-off bottom of Trader Joe's container, and soup bowl.

DC Motor & Cork.

Rubber bands to secure the whole thing.

3 RBG LED's. 

FSR.

 

Code

/*
* Light and Magic
* Stephen Backer
* October 11, 2011
*
* Adapted from Serial LED RGB, Jennifer Wang's "Rainbow Maker", and Pot Fades Motor
* Control 3 LEDs and a Motor by using the force of an FSR.  
* As speed of motor increases with increased force on the FSR, LED's transition from Red to Yellow to Green
*
*/
 
int potPin = 2;   // select the input pin for the potentiometer
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int val = 0;      // variable to store the value coming from the sensor
int redVal = 0; // calculated value of red LED brightness
int greenVal = 0; // calculated value of green LED brightness
int blueVal = 0; // calculated value of blue LED brightness
int motorPin = 3; // select the pin for the Motor
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  val = analogRead(potPin);    // read the value from the sensor, between 0 - 1024
  Serial.println(val);
  analogWrite(motorPin, val/4); // analogWrite can be between 0-255
  // NOTE: coefficients of linear equations to determine LED brightness are rounded,
  // so border values are explicitly set
 
  // 1024/7 colors (ROYGBIV) = 146
  // red when val == 0 (red LED only)
  // orange when val == 146
  // yellow when val == 292
  // green when val == 438 (green LED only)
  // blue when val == 584
  // indigo when val == 730 (blue LED only)
  // violet when val == 876
  // red when val == 1024
 
    //  red
  if (val >= 0 & val < 438) {
   redVal = 255;
    greenVal = 0;
    blueVal = 0;
   
  }
  // yellow
  else if (val == 438) {
    redVal = 255;
    greenVal = 100;
    blueVal = 0;
  }
  // yellow
  else if (val > 438 & val < 730) {
    redVal = 255;
    greenVal = 100;
    blueVal = 0;
  }
 
  // yellow
  else if (val == 730) {
    redVal = 255;
    greenVal = 100;
    blueVal = 0;
  }
  // green
  else if (val > 730 & val < 1024) {
     redVal = 50;
    greenVal = 255;
    blueVal = 0;
    
  }
  // green
  else if (val == 1024) {
    redVal = 50;
    greenVal = 255;
    blueVal = 0;
  }
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
 
  Serial.println();
  Serial.println(redVal);
  Serial.println(greenVal);
  Serial.println(blueVal);
  Serial.println();
}

Light & Magic
0
Your rating: None