Created
February 11, 2013 21:16
-
-
Save ekelleyv/4757697 to your computer and use it in GitHub Desktop.
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
//Flex sensor mapping | |
//adapted from http://garagelab.com/profiles/blogs/tutorial-flex-sensor-with-arduino | |
/* | |
Light based laser tags. | |
*/ | |
int max_val = 120; | |
int red = 3; | |
int green = 6; | |
void setup() | |
{ | |
// initialize serial communications | |
Serial.begin(9600); | |
pinMode(red, OUTPUT); | |
pinMode(green, OUTPUT); | |
} | |
void loop() | |
{ | |
int sensor, deg; | |
// read the voltage from the voltage divider (sensor plus resistor) | |
sensor = analogRead(2); | |
deg = map(sensor, 640, 700, -1*max_val, max_val); | |
// print out the result | |
Serial.print("analog input: "); | |
Serial.print(sensor,DEC); | |
Serial.print(" degrees: "); | |
Serial.println(deg,DEC); | |
if (deg < 0) { | |
controlRed(abs(deg)); | |
controlGreen(0); | |
} | |
else { | |
controlGreen(deg); | |
controlRed(0); | |
} | |
// pause before taking the next reading | |
delay(100); | |
} | |
// Yellow LED control | |
void controlGreen(int index) { | |
analogWrite(yellow, min(index, 255)); | |
} | |
// Red LED control | |
void controlRed(int index) { | |
analogWrite(red, min(index, 255)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment