Go Down

Topic: ¿Arduino store button push in buffer? (Read 551 times) previous topic - next topic

jfrubio

Hi.

I want to know if I can store a button push in a buffer to read afther loop code executio or I need to read the button push in the same moment that it pushing.

Example:

loop{
read button and is 0
turn led on
wait 1000
turn led off
}

If the user push the button while the code is in the "wait 1000" line. ¿What I will read in the next "read button" line 0 or 1? Its a problem if I have to read the button in the moment that the user in pressing it.

I have similar question with the RF communication using nrf24l01... ¿I have to read rf just while emissor is emiting or the module store the comunication in buffer?

Thanks and regards.

Grumpy_Mike

Quote
If the user push the button while the code is in the "wait 1000" line. ¿What I will read in the next "read button" line 0 or 1?
You will read the value at the time you do your read.

There are two ways round this.

1) Have the button generate an ISR ( Interrupt service routine ) and let that store a pushed value in a variable. Then read that variable in the loop.

2) The proper method is not to use the wait ( actually delay ) in the first place and implement your code as a state machine.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0
 

Go Up