Created
March 6, 2014 21:36
-
-
Save arielchuri/9400206 to your computer and use it in GitHub Desktop.
Code for the LED meter project. Sparkle Labs / Discover Arduino Bundle
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
// Sparkle Labs / Discover Arduino Bundle | |
// LED Meter Project | |
// --------------------------------------- | |
// Increment 3 LEDs by turning a knob. | |
// | |
// Circuit: | |
// Pins 0, 1, 2 are connected to ground through 3 LED drivers. | |
// A potentiometer is connected to power and ground on either side with the center pin (wiper) connected to Pin A0 on the Arduino. | |
// | |
// Parts: | |
// 3 LEDs | |
// 3 Resistors (100 - 220 ohms) | |
// 1 Potentiometer | |
// | |
// Schematic: | |
// | |
// +5V -----------------------------+ | |
// | | |
// +---------------|--------Resistor-->LED---+ | |
// | +-------------|-----+ | | |
// | | +-----------|--+ +--Resistor-->LED---O | |
// +-------------------------------+-+-+---+ | | | | |
// | G 1 1 1 1 9 8 7 6 5 4 3 2 1 0 | | +-----Resistor-->LED---O | |
// | N 3 2 1 0 | | | | |
// | | | | | |
// +---+---+ D ~ ~ ~ ~ ~ ~ | | | | |
// | | | | | | |
// | USB | +-+ | | | |
// +---+---+ +---------------+ | | | | |
// | | ARDUINO UNO | | | +---------------+ | | |
// | +---------------+ | | | | | | |
// | | | | Potentiometer | | | |
// | G G | | | | | | |
// | 5 N N A A A A A A | | +---+---+---+---+ | | |
// | V D D 0 1 2 3 4 5 +-+ | | | +-------O | |
// +-------------------------+-------------+ +---------+ | | | |
// | | ---+--- | |
// +-----------------------------------+ --+-- | |
// -+- | |
// * | |
void setup() { | |
pinMode(0,OUTPUT); | |
pinMode(1,OUTPUT); | |
pinMode(2,OUTPUT); | |
} | |
void loop() { | |
// read the input on analog pin 0: | |
int sensorValue = analogRead(A0); | |
// The analog pin reads from 0 to 1023. | |
// This commands maps those numbers from 4 to 0 | |
// So 0 becomes 4 and 1023 becomes 0 | |
int mappedSensorValue = map( sensorValue, 0, 1023, 4, 0); | |
//Here we test the mappedsensorvalue. If it is greater then 1 we turn | |
// on the LED on Pin 2 and then see if it is greater then 2. | |
if (mappedSensorValue > 1) { | |
digitalWrite ( 2, HIGH); | |
if (mappedSensorValue > 2) { | |
digitalWrite ( 1, HIGH); | |
if (mappedSensorValue > 3) { | |
digitalWrite ( 0, HIGH); | |
}else{//If it is not greater we start turning LEDs off. | |
digitalWrite ( 0, LOW); | |
} | |
}else{ | |
digitalWrite ( 1, LOW); | |
} | |
}else{ | |
digitalWrite ( 2, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment