Require components:
1x PGM5506
4x wires
1x LED 5mm
1x Resistor 10k
1x Resistor 200ohm
Code test:
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(A0);
Serial.println("Analog value : ");
Serial.println(value);
delay(200);
}
Code turn on LED when PGM5506 in low light:
const int PIN_LED = 9;
const int LIGHT_MIN = 350; // you may change this value to 0, depends on your environment
const int LIGHT_MAX = 750; // you may change this value to 1024, depends on your environment
void setup(){}
void loop() {
int valueRead = analogRead(A0); // analogWrite values from 0 to 255, https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
byte valueWrite = 0; // analogRead values go from 0 to 1023, https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
if(valueRead <= LIGHT_MIN) {
valueWrite = 255;
} else if(valueRead >= LIGHT_MAX) {
valueWrite = 0;
} else {
valueWrite = map(valueRead, LIGHT_MIN, LIGHT_MAX, 255, 0);
}
analogWrite(PIN_LED, valueWrite);
delay(100);
}
No comments:
Post a Comment