Last active
January 4, 2016 05:09
-
-
Save dmccreary/8572812 to your computer and use it in GitHub Desktop.
Position sensor code for RC Helicopter target landing game. Try to land your RC Helicopter (Syma S107 preferred) in a 10cm target. Sensor detect how far you are away from center. Three LEDs (green, orange and red) will tell your score.
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
/* | |
Arduino 1 Dimensional RC Helicopter Target Center Detection | |
Mount sensor near a circle 10 cm across | |
Try to land your RC Helicopter (Syma S107 preferred) | |
If the Green LED lights up you are in the target zone and you get 3 points | |
If the Orange LED lights up you near but not in the target zone and you get 1 points | |
If the Red LED lights up you only get a single point | |
Circuit: | |
I used HC-SR04 Ping distance sensor | |
Connect Sensor VCC to Arduino 5v | |
Connect Sensor GND to arduino GND | |
Connect Sensor Echo to Arduino pin 13 | |
Connect Sensor Trig to Arduino pin 12 | |
Connect RedLED through 220 Ohm Resistor between to Arduino pin 6 | |
Connect OrangeLED through 220 Ohm Resistor between to Arduino pin 5 | |
Connect OrangeLED through 220 Ohm Resistor between to Arduino pin 5 | |
TODO - add a second sensor for the other dimension | |
*/ | |
#define trigPin 12 | |
#define echoPin 13 | |
#define redLED 6 | |
#define orangeLED 5 | |
#define greenLED 3 | |
void setup() { | |
Serial.begin (9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(redLED, OUTPUT); | |
pinMode(orangeLED, OUTPUT); | |
pinMode(greenLED, OUTPUT); | |
} | |
void loop() { | |
long duration, distance; | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(1); // Added this line | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(1); // Added this line | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration/2) / 29.1; // may require some tuning.... | |
if (distance >= 10 && distance <= 20) { // This is where the LED On/Off happens | |
digitalWrite(greenLED,HIGH); // we are in the score zone | |
digitalWrite(orangeLED,LOW); | |
digitalWrite(redLED,LOW); | |
Serial.print(distance); | |
Serial.println(" cm for 3 points"); | |
} | |
else if (distance > 5 && distance < 30) { // This is where the LED On/Off happens | |
digitalWrite(greenLED,LOW); // we are near the score zone | |
digitalWrite(orangeLED,HIGH); | |
digitalWrite(redLED,LOW); | |
Serial.print(distance); | |
Serial.println(" cm for 2 points"); | |
} | |
else if (distance >= 200 || distance <= 5){ | |
digitalWrite(greenLED,LOW); // we are out of the score zone | |
digitalWrite(orangeLED,LOW); | |
digitalWrite(redLED,HIGH); | |
Serial.print(distance); | |
Serial.println(" cm for 1 point"); | |
}; | |
delay(300); // 1000 = delay a second | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment