Using a lilypad acceleropmeter to control an rgb led

20140407_133640

I used the x, y, and z values I received from accelerometer to control the RGB channels of an LED. I was not able to capture the color change on video because it was too fast.

Links
http://web.media.mit.edu/~leah/LilyPad/build/accelero_shirt.html
https://solarbotics.com/product/50465/
https://www.sparkfun.com/datasheets/DevTools/LilyPad/ADXL335.pdf

code

//Use a lilypad accelerometer to control an RGB LED
int x = A0;
int y = A1;
int z = A2;
int red = 10;
int green = 11;
int blue = 9;
int redBrightness = 0;
int greenBrightness = 0;
int blueBrightness = 0;

void setup(){
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
digitalWrite(red, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
}

void loop (){
//use x value to control red
int xVal = analogRead(x); //read the x value of the accelerometer
Serial.print(“The x value is: “); //print the value to the console
Serial.println(xVal);
//I adjusted the range of the map to reflect the values I was receiving from the accelerometer)
int xMap = map(xVal, 150,500,0,255); //map the value to a range of 0-255
redBrightness = xMap;
analogWrite(red, redBrightness);

//Use y value to control blue
int yVal = analogRead(y);
Serial.print(“The y value is: “);
Serial.println(yVal);
int yMap = map(yVal, 150,500,0,255); //map the value to a range of 0-255
blueBrightness = yMap;
analogWrite(blue, blueBrightness);

//Use z value to control green
int zVal = analogRead(z);
Serial.print(“The z value is: “);
Serial.println(zVal);
int zMap = map(zVal, 150,500,0,255); //map the value to a range of 0-255
greenBrightness = yMap;
analogWrite(green, greenBrightness);

delay (10);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s