I apologize in advance for asking this probbably dumb question, but i bought an arduino few days ago and i don't have even basic skills at programming. I want when I turn on the arduino that my led statrs to fade in until it reaches limited brightness, and then i want to turn it off for 2 seconds. The problem is, everything works ok first time, but when it reaches the end of the loop it doesn't repeat the same thing again, it just goes fade in untill some brightness, than fade in again and again... it doesn't turns off for 2 seconds like it should. I tried putting "for" in every place possible because i probabbly putted it in the wrong place because it seems to me that loop is skipping that "for". Can someone explain where did I go wrong and what is the correct way to execute this? Please explain to me like I'm retarded because I am retarded in programming, but I am trying to learn and understand something... Thanks
int led1 = 9; // led 1 pin 9
int brightness1 = 0; // led brightness
int fadeAmount1 = 5; // fade amount
void setup () {
pinMode(led1, OUTPUT);
}
void loop() {
analogWrite(led1, brightness1);
// fade in the led
brightness1 = brightness1 + fadeAmount1;
// turn off the led for 3 sec when led brighness reaches certain level
if (brightness1 == 150) {
digitalWrite(led1, LOW);
delay(2000);
}
delay(50);
}
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.
Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.
You need to reset the value of brightness1 when it reaches the end of the fade:
Thanks for the quick reply... I'm sorry for the troubles with the code, i will use auto format from now on ... If it's not the problem for you can you try to explain to me why I have to reset the brightness? Shouldn't the program when it comes to an end of the loop be returned to the beginning of the loop where the brightness is set to 0 (analogWrite led1, brightness1)?
int led1 = 9; // led 1 pin 9
int brightness1 = 0; // led brightness
int fadeAmount1 = 5; // fade amount
void setup () {
Serial.begin(9600);
while (!Serial) {}
Serial.println("Starting sketch");
pinMode(led1, OUTPUT);
}
void loop() {
analogWrite(led1, brightness1);
Serial.print("brightness1 = ");
Serial.println(brightness1);
// fade in the led
brightness1 = brightness1 + fadeAmount1;
// turn off the led for 3 sec when led brighness reaches certain level
if (brightness1 == 150) {
digitalWrite(led1, LOW);
Serial.println("Fade is finished");
delay(2000);
}
delay(50);
}
Then do this:
Tools > Serial Monitor
Select 9600 from the menu in the bottom right corner of the Serial Monitor
Adding debug serial output is a really useful tool for understanding what's happening in your Arduino.
Great, i got the point... Except for one thing... After first fade the brightness is just increasing, how come that the led is still fading in because on the serial monitor the brightness is only increasing? Shouldn't it stay on?
What happens is that the PWM can be set to 0-255. If you pass a larger value to analogWrite() it overflows. So analogWrite(led1, 256) is the same as analogWrite(led1, 0)
analogWrite(led1, 384) is the same as analogWrite(led1, 128)
analogWrite(led1, 511) is the same as analogWrite(led1, 255)
If you make brightness1 a byte instead of an int the output will match your expected LED brightness because byte overflows to 0 after 255, just like the PWM.
The reason it doesn’t reset is because nothing above the loop upon looping over is reset to the numbers you have up top.
Basically after the loop ends it retains the value after the fade and what you would need to do is when the fade is done you can add the delay and then set your brightness to 0 or whatever it needs to be so when it loops back up, the value will be ready for another fade.