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 ledPin1 = 11; | |
int ledPin2 = 12; | |
int ledPin3 = 13; | |
int ledPin4 = 10; | |
int ledPin5 = 9; | |
int ledPin6 = 8; | |
void setup() { | |
pinMode(ledPin1,OUTPUT); | |
pinMode(ledPin2,OUTPUT); | |
pinMode(ledPin3,OUTPUT); | |
pinMode(ledPin4, OUTPUT); | |
pinMode(ledPin5, OUTPUT); | |
pinMode(ledPin6, 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 | |
Serial.println(sensorValue); | |
int mappedSensorValue = map( sensorValue, 0, 1023, 7 , 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 (ledPin1, HIGH); | |
if (mappedSensorValue > 2) { | |
digitalWrite (ledPin2, HIGH); | |
if (mappedSensorValue > 3) { | |
digitalWrite (ledPin3, HIGH); | |
if (mappedSensorValue > 4) { | |
digitalWrite (ledPin4, HIGH); | |
if (mappedSensorValue > 5) { | |
digitalWrite (ledPin5, HIGH); | |
if (mappedSensorValue > 6) { | |
digitalWrite (ledPin6, HIGH); | |
} | |
else{//If it is not greater we start turning LEDs off. | |
digitalWrite (ledPin6, LOW); | |
} | |
} | |
else{ | |
digitalWrite (ledPin5, LOW); | |
} | |
} | |
else{ | |
digitalWrite (ledPin4, LOW); | |
} | |
} | |
else{ | |
digitalWrite (ledPin3, LOW); | |
} | |
} | |
else{ | |
digitalWrite (ledPin2, LOW); | |
} | |
} | |
else{ | |
digitalWrite (ledPin1, LOW); | |
} | |
} |
I added 3 leds to this code so now there are 6 lights