Hi there, can someone fix my code? so i want GreenLed will blinking for a while then it will stays on if button is pressed, if button not pressed, RedLight will blinking forever. How can i fix my code?
My Code : GreenLed always blinking when button pressed
i think you need to build a little state machine, a switch statement
in the idle state, you call a code to flash the red LED.
when you press the button (while you hold the button down), you set a flashing green state
while in the flash green state, you either wait for time to pass or count the number of flashes and transition to a final state where the green LED is held on.
while in that final state, recognizing a button press, not that it is held down, resets the state to idle
gcjr:
i think you need to build a little state machine, a switch statement
in the idle state, you call a code to flash the red LED.
when you press the button (while you hold the button down), you set a flashing green state
while in the flash green state, you either wait for time to pass or count the number of flashes and transition to a final state where the green LED is held on.
while in that final state, recognizing a button press, not that it is held down, resets the state to idle
How can i make that? can you tell me how to do it? is there any reference?
It's not so complicated that you need any reference. Just make a variable, call it 'flashing' for example. Then you construct two branches that you execute depending on the state of 'flashing':
Inside these blocks you do the things that you should do when flashing and not flashing. You change from one to the other by changing the value of 'flashing' to true or false..
aarg:
It's not so complicated that you need any reference. Just make a variable, call it 'flashing' for example. Then you construct two branches that you execute depending on the state of 'flashing':
Inside these blocks you do the things that you should do when flashing and not flashing. You change from one to the other by changing the value of 'flashing' to true or false..
Can you give an example? i'm a newbie so i don't understand it
adrian12000:
Hi there, can someone fix my code? so i want GreenLed will blinking for a while then it will stays on if button is pressed, if button not pressed, RedLight will blinking forever. How can i fix my code?
Make your statement clear:
1. "GreenLed will (be) blinking for a while" -- for how long? You need to give the time duration.
2. If you press the button within that duration, the GreenLED will stay ON. Is it correct?
3. If you don't press the button within that duration, the GreenLED will be eventually OFF.
4. After that the RedLight will start blinking for ever. Is it correct?
If you answer the above questions, you may expect some help.
Assuming button (K1) is at open condition as per following diagram (Fig-1):
Figure-1:
1. GreenLed starts blinking at 200 ms interval (ON-100 ms delay-OFF-100 ms delay). Make the interval for 1000 ms so that you can clearly observe that the GreenLed and RedLight blinks.
2. If you press the button within that duration (20 sec = 20 blinks), the GreenLED will stay ON. Is it correct?
3. If you don't press the button within that duration (20 sec = 20 blinks), the GreenLED will be
eventually OFF.
4. After that the RedLight will start blinking for ever. Is it correct?
The above four tasks could be accomplished by the following codes: Create sketch and insert these codes at the appropriate place; upload the sketch and report the result. Don't forgrt to initialize variables as needed like pin directions, counter etc.
bool n = digitalRead(12); //check button's open/close status
if(n != LOW) //button L1 is not closed
{
digitalWrite(3, HIGH);
delay(500);
digitalWrite(3, LOW);
delay(500);
counter++; //increment counter
if(counter == 20) //GreenLed has finished blinking for 20 times
{
digitalWrite(3, LOW);
while(1); //wait for ever
}
}
else
{
digitalWrite(3, LOW); //button is closed; GreenLed stays ON
//------------------------------------------------------------------
while(1) //keep blinking RedLight for ever at 1-sec interval
{
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
}