I had a lot of issues with this experimentation. In the end, the only thing I was able to figure out was having the LED fade constantly and turn off when something was covering the LDR. What I was hoping to do with this project was to have the LDR control the brightness of the LED and then have another button that determines whether or not the LED is on or off.
I'm still trying to figure out this project, but hopefully I learn my mistake soon!
const int ledPin = 9;
const int ldrPin = A0;
int brightness = 0;
int fadeAmount = 5;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 400) {
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);;
}
else {
digitalWrite (ledPin, LOW);
}
}
No comments:
Post a Comment