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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 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.