Skip to content

Instantly share code, notes, and snippets.

@bevchou
Last active September 27, 2017 02:25
Show Gist options
  • Save bevchou/a943702e687438f56e35e90cb9bc9072 to your computer and use it in GitHub Desktop.
Save bevchou/a943702e687438f56e35e90cb9bc9072 to your computer and use it in GitHub Desktop.
Second iteration with light level
//connect LEDs based on pins
//the light sensor must be connected to A5
int LED1Pin = 3;
int LED2Pin = 4;
int LED3Pin = 5;
int LED4Pin = 6;
void setup(){
Serial.begin(9600);
pinMode(LED1Pin, OUTPUT);
pinMode(LED2Pin, OUTPUT);
pinMode(LED3Pin, OUTPUT);
pinMode(LED4Pin, OUTPUT);
}
void loop(){
//read input
int sensorReading = analogRead(A5);
Serial.println(sensorReading);
int level = map(sensorReading, 0, 1023, 1, 7);
if (level > 2) {
digitalWrite(LED1Pin, HIGH);
} else {
digitalWrite(LED1Pin, LOW);
}
if (level > 3) {
digitalWrite(LED2Pin, HIGH);
} else {
digitalWrite(LED2Pin, LOW);
}
if (level > 4) {
digitalWrite(LED3Pin, HIGH);
} else {
digitalWrite(LED3Pin, LOW);
}
if (level > 5) {
digitalWrite(LED4Pin, HIGH);
} else {
digitalWrite(LED4Pin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment