Using a lilypad acceleropmeter to control an rgb led


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.



//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(){
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
//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: “);
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: “);
int zMap = map(zVal, 150,500,0,255); //map the value to a range of 0-255
greenBrightness = yMap;
analogWrite(green, greenBrightness);

delay (10);


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s