Created
September 21, 2011 04:34
-
-
Save julioterra/1231253 to your computer and use it in GitHub Desktop.
Simple code for the Arduino to read data from a GSR sensor and turn on one to four leds based on the reading.
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
// save pin numbers to variables | |
int sensorPin = A0; | |
int ledPin1 = 2; | |
int ledPin2 = 3; | |
int ledPin3 = 4; | |
int ledPin4 = 5; | |
void setup() { | |
// define which pins are used for input and output | |
pinMode(sensorPin, INPUT); | |
pinMode(ledPin1, OUTPUT); | |
pinMode(ledPin2, OUTPUT); | |
pinMode(ledPin3, OUTPUT); | |
pinMode(ledPin4, OUTPUT); | |
// open a serial port connection to computer | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read data from appropriate pin and assign value to variable | |
int sensorVal = analogRead(sensorPin); | |
// print sensor reading to the the computer via serial port | |
Serial.println(sensorVal); | |
// set all leds to off | |
digitalWrite(ledPin1, LOW); | |
digitalWrite(ledPin2, LOW); | |
digitalWrite(ledPin3, LOW); | |
digitalWrite(ledPin4, LOW); | |
// turn on leds based on value from sensor | |
if (sensorVal >= 20) { | |
digitalWrite(ledPin1, HIGH); | |
} | |
if (sensorVal >= 40) { | |
digitalWrite(ledPin2, HIGH); | |
} | |
if (sensorVal >= 60) { | |
digitalWrite(ledPin3, HIGH); | |
} | |
if (sensorVal >= 80) { | |
digitalWrite(ledPin4, HIGH); | |
} | |
// wait for 20 milliseconds before reading sensor again | |
delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment