Skip to content

Instantly share code, notes, and snippets.

@raykao
Created August 15, 2014 17:01
Show Gist options
  • Save raykao/2eb0c3bcac150c76ba33 to your computer and use it in GitHub Desktop.
Save raykao/2eb0c3bcac150c76ba33 to your computer and use it in GitHub Desktop.
Code to make your own blinking turn light tilt sensor safety vest
int groundpin = 18; // analog input pin 4
int powerpin = 19; // analog input pin 5
int xpin = A3; // x-axis of the accelerometer
int ypin = A2; // y-axis
int zpin = A1; // z-axis (only on 3-axis models)
long xy_max = 500;
long xy_min = 540;
int leftPin = 11;
int rightPin = 8;
int state = LOW;
long x = 0;
long y = 0;
void setup()
{
Serial.begin(1200);
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop()
{
x = analogRead(xpin);
y = analogRead(ypin);
if (y > xy_min) { //left
if(state == HIGH) {
state = LOW;
digitalWrite(leftPin, state);
}
else {
state = HIGH;
digitalWrite(leftPin, state);
}
Serial.println("The arduino is moving(tilted) left");
} else if (y <= xy_min){
digitalWrite(leftPin, LOW);
}
if (y < xy_max) { //right
if(state == HIGH) {
state = LOW;
digitalWrite(rightPin, state);
}
else {
state = HIGH;
digitalWrite(rightPin, state);
}
Serial.println("The arduino is moving(tilted) right");
} else if (y >= xy_max){
digitalWrite(rightPin, LOW);
}
if ( y <= xy_min && y >= xy_max) { // nothing
state = LOW;
digitalWrite(leftPin, HIGH);
digitalWrite(rightPin, HIGH);
// Serial.println("The arduino is currently not moving(tilted) in any direction");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment