Created
November 13, 2013 02:28
-
-
Save interstateone/7442608 to your computer and use it in GitHub Desktop.
A little Arduino sketch that maps a potentiometer value to a row of LEDs
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
int sensorPin = A0; // select the input pin for the potentiometer | |
int sensorValue = 0; // variable to store the value coming from the sensor | |
int mappedValue = 0; | |
void setup() { | |
// declare the LED pins as OUTPUTs: | |
pinMode(2, OUTPUT); | |
pinMode(3, OUTPUT); | |
pinMode(4, OUTPUT); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
Serial.begin(9600); //for testing | |
} | |
void loop() { | |
// read the value from the sensor: | |
sensorValue = analogRead(sensorPin); | |
// map the sensor range (0-1023) to the number of LEDs (4, plus one for off) | |
mappedValue = map(sensorValue, 0, 1023, 0, 5); | |
// for testing | |
Serial.println(sensorValue); | |
Serial.println(mappedValue); | |
// loop through all of the pins | |
for(int i = 0; i <= 4; i++){ | |
// if a pin is within the mapped sensor value, turn it on | |
if(i < mappedValue){ | |
// +2 because pins 0 and 1 are for serial I/O | |
digitalWrite(i+2, HIGH); | |
// if a pin is outside sensor value, turn it off | |
} else { | |
digitalWrite(i+2, LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment