Week 2: RGB-LED w/ Accelerometer

The LED’s color changes based on orientation of accelerometer.

Watch pretty lights change as I rotate by Arduino.

//reg-led digital pins
int R = 11;
int G = 10;
int B = 9;

//accelerometer analog pins
int Xout = 0; // select the input pin for the sensor
int Yout = 1;
int Zout = 2;

//accelerometer variables
int xVal = 0; // variable to store the value coming from the sensor
int yVal = 0;
int zVal = 0;

//map
int scaledValue;

void setup(){
Serial.begin(9600);
//”AREF” pin – gives specific/precise readings
analogReference(EXTERNAL); // use AREF for reference voltage
}

void loop(){

xVal = analogRead(Xout); // read the value from the sensor
//Serial.println(xVal); //0-1023 Val – find lowest & highest values to map
analogWrite(R,map(xVal, 390,620, 0,255));//0-255 rgb

yVal = analogRead(Yout); // read the value from the sensor
//Serial.println(map(yVal, 390,630, 0,255));
analogWrite(G,map(yVal, 390,630, 0,255));

zVal = analogRead(Zout); // read the value from the sensor
//Serial.println(map(zVal, 390,640, 0,255));
analogWrite(B,map(zVal, 390,640, 0,255));

}

Leave a Reply

Your email address will not be published. Required fields are marked *