i am trying to make leds fade in by pressing a button. the leds are supposed to stay fully lid until you press the reset button.
i have tryed to write some code for this but i am kinda lost right now.
the fading in is working but the reset and letting the leds stay on isnt working.
here is the layout i made on the breadboard
and the code i have right now.
/*
what i am trying to do is to let the leds fade in when there is an input on pin 2 from the alarm.
when the leds are fully faded in they should stay like that until you press the reset button.
*/
int brightness = 0; // how bright the LED is
int fadeAmount = 2; // how many points to fade the LED by
const int alarm = 2; //aansluiting voor de wekker
const int resetknop = 4;
int alarmoutput = 0;
int Reset =0;
void setup() {
Serial.begin(9600);
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
}
void loop() {
begin: ;
alarmoutput = digitalRead(alarm);
Reset = digitalRead(resetknop);
if (alarmoutput == HIGH)
{
leds:
// set the brightness of pin 9:
analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
delay(40);
if (Reset == HIGH)
{
goto einde;
}
//this loops the code for letting the leds fade in i wanted the leds to stay on when they are faded in but this didnt work :P
goto leds;
}
einde: ;
}
anyone have some suggestions on how to make this work
ps: i dont want to use the reset button on the arduino, because i want learn how you can skip code by pressing a button.
Depending on how your buttons are oriented the inputs are either HIGH all the time (shorted to +5) or they are floating because the pull-down resistors are on the wrong side of the button. The pull-down should be on the same side of the button as the input.
You forgot to stop raising the brightness when you got to 255, the maximum PWM value.
johnwasser:
Depending on how your buttons are oriented the inputs are either HIGH all the time (shorted to +5) or they are floating because the pull-down resistors are on the wrong side of the button. The pull-down should be on the same side of the button as the input.
You forgot to stop raising the brightness when you got to 255, the maximum PWM value.
made a little mistake with the drawning in fritzz. the buttons do work correct but the reset button does not work like how i want it.
can you use the "if" inside another "if" command ?
Re-written into modern vernacular with comments pointing to logic problems
/*
what i am trying to do is to let the leds fade in when there is an input on pin 2 from the alarm.
when the leds are fully faded in they should stay like that until you press the reset button.
*/
int brightness = 0; // how bright the LED is
int fadeAmount = 2; // how many points to fade the LED by
const int alarm = 2; //aansluiting voor de wekker
const int resetknop = 4;
int alarmoutput = 0;
int Reset =0;
void setup() {
Serial.begin(9600);
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(alarm, INPUT);
pinMode(resetknop, INPUT);
}
void loop() {
alarmoutput = digitalRead(alarm);
Reset = digitalRead(resetknop);
if (alarmoutput == HIGH)
{
while (true==true)
{
// set the brightness of pin 9:
analogWrite(9, brightness);
analogWrite(10, brightness);
analogWrite(11, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount; ////// This should check for brightness going over 255
delay(40);
if (Reset == HIGH) ///// This won't ever change because you only read the pin once before the loop
{
break;
}
//this loops the code for letting the leds fade in i wanted the leds to stay on when they are faded in but this didnt work :P
} // End of while loop
///////// Shouldn't you set the brightness back to 0 when you reset?
} // End of if statement
}