Go Down

Topic: buffer for digitalRead? (Read 2005 times) previous topic - next topic


if my arduino is reading some pulses  from a pin from a device, for example 10 pulses. then it prints them on serial monitor .

if i put some delay between each reading the pin in void-loop, i am losing many data, is there any way to have some buffer like rx/tx?


Well, not in a simple way.

Two ways to solve your problem
1) Do not use the delay(), keep sampling the pin as often as possible in your main void-loop. You dont explain why you put the delay there, but in general you replace the "logic" of your delay with the method shown in http://arduino.cc/en/Tutorial/BlinkWithoutDelay

2) Use attachInterrupt(). Your interrupt routine can then "buffer" the state changes. This requires a bit more knowledge about interrupts in general to be used safely.


Aug 07, 2010, 01:28 pm Last Edit: Aug 07, 2010, 01:34 pm by tkbyd Reason: 1
Or you could use an external "counter" chip.

See the bottom part of...



i am using the delay becouse i read the pulse and i am changing the width of the pulse, then i am exporting the pulse,

so i am reading 4 pulses at a time and then i am exprorting them, i think the problem is somewhere there, maybe i need to read one by one


Sounds like you are doing something similar to detecting button presses. To let the arduino run as fast as it wants to, you probably need to set a flag in a variable to track a change in pin state. When a pin is high, set a flag to 1. As the program loops, a comparison is made between the pin state and the flag. If they both match, the pin state has not changed, and no state change is recorded. If the pin goes low and the flag does not match, record a pulse and change the flag state to 0. When the pin goes high and does not match the 0 flag, change the flag back to 1. When the pin goes low and the flag does not match, record a pulse and change the flag state to 0. yada..., yada..., yada...
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up