#define LDR_PIN A0
#define RED 13
#define YELLOW 12
#define GREEN 11
enum keadaan{
TERANG,
REDUP,
GELAP,
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
bacaCahaya();
indicator();
nyalaLED();
}
int bacaCahaya(){
int cahaya = analogRead(LDR_PIN);
Serial.println(bacaCahaya());
delay(500);
return cahaya;
}
void indicator(){
if (bacaCahaya()<300){
keadaan = TERANG;
}
else if (bacaCahaya()>700){
keadaan = GELAP;
}
else{
keadaan = REDUP;
}
}
void nyalaLED(){
switch (keadaan) {
case TERANG:
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
break;
case REDUP:
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
break;
case GELAP:
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
break;
}
}