Pages: [1]   Go Down
Author Topic: buffer for digitalRead?  (Read 906 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 26
Posts: 1148
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or you could use an external "counter" chip.

See the bottom part of...

http://www.play-hookey.com/digital/experiments/counter_ic_4029.html
« Last Edit: August 07, 2010, 06:34:33 am by tkbyd » Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8966
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: