Created
December 10, 2011 22:54
-
-
Save dydx/1456877 to your computer and use it in GitHub Desktop.
illuminate left or right led with accelerometer tilt
This file contains 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
/** | |
Two LEDs triggered by tilting of accelerometer | |
along Y axis. | |
Arduino Diecimila and Memsic 2125 | |
*/ | |
int ledPins[] = {6,7}; | |
int yPin = 10; | |
int yRaw; | |
double ymG, yTheta; | |
void setup() { | |
Serial.begin(9600); | |
// initialize LED driving pins to OUTPUT and LOW | |
for(int i = 0; i <= size(ledPins); i++) { | |
pinMode(ledPins[i], OUTPUT); | |
digitalWrite(ledPins[i], LOW); | |
} | |
pinMode(yPin, INPUT); | |
} | |
void loop() { | |
yRaw = pulseIn(yPin, HIGH); // Raw data from the sensor | |
ymG = ((yRaw / 10) - 500) * 8; // milli-Gs | |
yTheta = asin(ymG / 1000.0); // radians of Y axis | |
yTheta = yTheta * (360 / (2*M_PI)); // convert rad to deg | |
//Serial.print("Y Raw: "); | |
//Serial.println(yRaw); | |
//Serial.print("Y milli-Gs: "); | |
//Serial.println(ymG); | |
Serial.print("Y Angle: "); | |
Serial.println(yTheta); | |
if (yTheta >= 10) { | |
rightLed(); | |
} else if (yTheta <= -10) { | |
leftLed(); | |
} else { | |
offLed(); | |
} | |
} | |
void leftLed() { | |
digitalWrite(ledPins[0], HIGH); | |
digitalWrite(ledPins[1], LOW); | |
} | |
void rightLed() { | |
digitalWrite(ledPins[0], LOW); | |
digitalWrite(ledPins[1], HIGH); | |
} | |
void offLed() { | |
digitalWrite(ledPins[0], LOW); | |
digitalWrite(ledPins[1], LOW); | |
} | |
int size(int arr[]) { | |
int i = 0; | |
while(arr[i] != NULL) i++; | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment