Voltage Divider
At first, I ran into the problem of the LED not lighting up, even though the circuit connections were all correct. After double checking my code, I realized that it was simply an issue of my unique lighting set up, and quickly changed the parameter value in the code based on the light intensity levels of my room as reported by the serial monitor.
Blink Speed Change
After successfully using the circuit to change the brightness of the LED, I tried to then use it to change the blink speed so that the darker it was, the faster the LED would blink.
Code
const int ledPin=9;
const int sensorPin = A0;
void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin,INPUT);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range
// to the output LED range
if (sensorReading <= 450) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
} else {
// change blink speed:
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}
Circuit
The circuit was the same for both experiments.
No comments:
Post a Comment