Strange behaviour when reading digital pins


hello,

i have encoder connected 6 digital inputs. encorder outputs greycode signal, 6 bits.

during development have used several serial.print-statements debug conversion greycode binary. , when happy result removed serial.print-statements. when went wrong. grecode-to-binary conversion yielded different result. added couple of serial.print-statements , result of sudden correct again. baffeled me bit :)

i scratched head , tried search diferent keywords. came accross thread here suggested using delay(1).

i added delay(1) without serial.print-statements, , result correct. tried moving delay(1) around see need occoured, not matter put it, long apears after read digital inputs. putting in front gives wrong result. can explain why, og where? :)

i understand serial.print creates delay, understand why delay(1) works serial.print, wonder why?

code: [select]
byte greycode;

if (digitalread(pin_sensor_wavc)) { bitset(greycode, 5); } else { bitclear(greycode, 5); }
if (digitalread(pin_sensor_wavd)) { bitset(greycode, 4); } else { bitclear(greycode, 4); }
if (digitalread(pin_sensor_wave)) { bitset(greycode, 3); } else { bitclear(greycode, 3); }
if (digitalread(pin_sensor_wavf)) { bitset(greycode, 2); } else { bitclear(greycode, 2); }
if (digitalread(pin_sensor_wavg)) { bitset(greycode, 1); } else { bitclear(greycode, 1); }
if (digitalread(pin_sensor_wavh)) { bitset(greycode, 0); } else { bitclear(greycode, 0); }
delay(1);


the code above runs 2 times pr second. suggestions apreciated. :)

here thread got delay(1) tips from: http://www.arduino.cc/cgi-bin/yabb2/yabb.pl?num=1272024322


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Strange behaviour when reading digital pins


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums