This changes the speed of the lights when the accelerometer is moved on the x-axis.
When I first started the project I tried to make it so when the sensor is shaken the lights would speed up but I had trouble figuring out how to accomplish that. My attempt is commented out in the posted code.
I used Read Analog Accelerometer from the course website and Multiple Blinking Lights from Instructables to make this
int ledYellow1 = 2; int ledBlue2 = 3; int ledRed3 = 4; int ledYellow4 = 5; int ledBlue5 = 6; int ledRed6 = 7; float lastX=0; float lastY=0; float lastZ=0; float lastChange=0; void setup(){ pinMode(ledYellow1,OUTPUT); pinMode(ledBlue2,OUTPUT); pinMode(ledRed3,OUTPUT); pinMode(ledYellow4,OUTPUT); pinMode(ledBlue5,OUTPUT); pinMode(ledRed6,OUTPUT); } // ================================================================================ // Run one iteration of the main event loop. The Arduino system will call this // function over and over forever. void loop() { // Read the accelerations as uncalibrated values. int x_raw = analogRead(0); int y_raw = analogRead(1); int z_raw = analogRead(2); // The following will rescale the values to be approximately in Earth gravity units. const int OFFSET = 335; const float SCALE = 0.013; float x = (x_raw - OFFSET) * SCALE; float y = (y_raw - OFFSET) * SCALE; float z = (z_raw - OFFSET) * SCALE; // Print the raw outputs. //float changeX = abs(x)-lastX; //float changeY = abs(y)-lastY; //float changeZ = abs(z)-lastZ; //int averageChange=(changeX+changeY+changeZ)/3 Serial.begin(9600); Serial.print("Last: X: "); Serial.print(lastX); Serial.print(" Y: "); Serial.print(lastY); Serial.print(" Z: "); Serial.print(lastZ); Serial.print(" Change: "); //Serial.print(averageChange); // Print the calibrated outputs. Serial.print(" Calib: X: "); Serial.print(x); Serial.print(" Y: "); Serial.print(y); Serial.print(" Z: "); Serial.println(z); // println appends an end-of-line character //lastX = abs(x); //lastY = abs(y); //lastZ = abs(z); //int speedChange=abs(lastChange-averageChange); float delayTime=map(x*10,-5,5,300,50); //Serial.print(delayTime); digitalWrite(ledYellow1,HIGH); delay(delayTime); digitalWrite(ledYellow1,LOW); delay(delayTime); digitalWrite(ledBlue2,HIGH); delay(delayTime); digitalWrite(ledBlue2,LOW); delay(delayTime); digitalWrite(ledRed3,HIGH); delay(delayTime); digitalWrite(ledRed3,LOW); delay(delayTime); digitalWrite(ledYellow4,HIGH); delay(delayTime); digitalWrite(ledYellow4,LOW); delay(delayTime); digitalWrite(ledBlue5,HIGH); delay(delayTime); digitalWrite(ledBlue5,LOW); delay(delayTime); digitalWrite(ledRed6,HIGH); delay(delayTime); digitalWrite(ledRed6,LOW); delay(delayTime); //lastChange = averageChange; // Delay for a short interval to create a periodic sampling rate. delay(500); }
Leave a Reply
You must be logged in to post a comment.