Created
October 30, 2020 02:14
-
-
Save sequoiap/d8bc69c78b993bca30c889f1c03eae30 to your computer and use it in GitHub Desktop.
Simple code to light up a set of four LED's attached to an arduino depending on the intensity measured by the photodiode.
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
const int P25 = 2; | |
const int P50 = 3; | |
const int P75 = 4; | |
const int P100 = 5; | |
const int SENSOR = A0; | |
void setup() | |
{ | |
pinMode(P25, OUTPUT); | |
pinMode(P50, OUTPUT); | |
pinMode(P75, OUTPUT); | |
pinMode(P100, OUTPUT); | |
pinMode(SENSOR, INPUT); | |
digitalWrite(P25, LOW); | |
digitalWrite(P50, LOW); | |
digitalWrite(P75, LOW); | |
digitalWrite(P100, LOW); | |
Serial.begin(9600); | |
} | |
long level = 0; | |
void updateLeds(long level) | |
{ | |
if (level == 0) { | |
digitalWrite(P25, LOW); | |
digitalWrite(P50, LOW); | |
digitalWrite(P75, LOW); | |
digitalWrite(P100, LOW); | |
} | |
else if (level == 1) { | |
digitalWrite(P25, HIGH); | |
digitalWrite(P50, LOW); | |
digitalWrite(P75, LOW); | |
digitalWrite(P100, LOW); | |
} | |
else if (level == 2) { | |
digitalWrite(P25, HIGH); | |
digitalWrite(P50, HIGH); | |
digitalWrite(P75, LOW); | |
digitalWrite(P100, LOW); | |
} | |
else if (level == 3) { | |
digitalWrite(P25, HIGH); | |
digitalWrite(P50, HIGH); | |
digitalWrite(P75, HIGH); | |
digitalWrite(P100, LOW); a | |
} | |
else { | |
digitalWrite(P25, HIGH); | |
digitalWrite(P50, HIGH); | |
digitalWrite(P75, HIGH); | |
digitalWrite(P100, HIGH); | |
} | |
} | |
void loop() | |
{ | |
Serial.println(analogRead(SENSOR)); | |
level = map(analogRead(SENSOR), 1005, 1022, 0, 4); | |
updateLeds(level); | |
Serial.print(level); | |
Serial.print("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment