Created
November 26, 2014 04:10
-
-
Save Craigson/ab5bd1967829b4153126 to your computer and use it in GitHub Desktop.
accelerometer code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Average.h> | |
| int sensorValueX; | |
| int sensorValueY; | |
| #define count 10 | |
| int xArray[count]; | |
| int yArray[count]; | |
| int xValue; | |
| int yValue; | |
| const int motorPin = 3; | |
| void setup(){ | |
| Serial.begin(9600); | |
| pinMode(motorPin, OUTPUT); | |
| } | |
| void loop(){ | |
| for (int i = 0; i < count; i++){ | |
| xArray[i] = analogRead(A0); | |
| delay(1); | |
| } | |
| sensorValueX = mean(xArray, count); | |
| delay(1); | |
| for (int i = 0; i < count; i++){ | |
| yArray[i] = analogRead(A1); | |
| delay(1); | |
| } | |
| sensorValueY = mean(yArray, count); | |
| //Serial.println(sensorValue); | |
| if (sensorValueY < 319){ | |
| yValue = -2; | |
| digitalWrite(motorPin,HIGH); | |
| }else { | |
| digitalWrite(motorPin, LOW); | |
| } | |
| if (sensorValueY > 319 && sensorValueY < 329){ | |
| yValue = -1; | |
| } | |
| if (sensorValueY > 329 && sensorValueY < 340){ | |
| yValue = 0; | |
| } | |
| if (sensorValueY > 340 && sensorValueY < 350){ | |
| yValue = 1; | |
| } | |
| if (sensorValueY > 360){ | |
| yValue = 2; | |
| digitalWrite(motorPin,HIGH); | |
| }else { | |
| digitalWrite(motorPin, LOW); | |
| } | |
| if (sensorValueX < 319){ | |
| xValue = -2; | |
| digitalWrite(motorPin,HIGH); | |
| }else { | |
| digitalWrite(motorPin, LOW); | |
| } | |
| if (sensorValueX > 319 && sensorValueX < 329){ | |
| xValue = -1; | |
| } | |
| if (sensorValueX > 329 && sensorValueX < 340){ | |
| xValue = 0; | |
| } | |
| if (sensorValueX > 340 && sensorValueX < 350){ | |
| xValue = 1; | |
| } | |
| if (sensorValueX > 350){ | |
| xValue = 2; | |
| digitalWrite(motorPin,HIGH); | |
| } else { | |
| digitalWrite(motorPin, LOW); | |
| } | |
| Serial.print("x: "); | |
| Serial.print(xValue); | |
| Serial.print(", "); | |
| Serial.print("y: "); | |
| Serial.println(yValue); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment