Hi there,
just courious where was my mistake - i want to read a button, connected to a pin and GND with
pinMode(pin, INPUT_PULLUP);
if (!digitalRead(pin)) { … } // This did not work while
if (digitalRead(pin)==LOW) { … } // This works?
Hi there,
just courious where was my mistake - i want to read a button, connected to a pin and GND with
pinMode(pin, INPUT_PULLUP);
if (!digitalRead(pin)) { … } // This did not work while
if (digitalRead(pin)==LOW) { … } // This works?
I don't see a reason why the first shouldn't work but it's very easy to understand the intent of the second code and it doesn't make any assumptions about the value of LOW so I think it's better anyway.
void setup()
{
Serial.begin(115200);
pinMode(A1, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(A1))
{
Serial.println("true");
}
if (!digitalRead(A1))
{
Serial.println("false");
}
delay(1000);
}
Works fine for me.
Button not pressed prints true
Button pressed prints false
Please post a complete program showing that it does not work for you
Ah sorry, i just see i did not tell you the complete truth.
The complete line was
if (!digitalRead(pin) || intCnt > 36/8) { // blink LED .. }
vs. the functioning
if (digitalRead(pin)==LOW || intCnt > 36/8) { // blink LED .. }
intCnt is increased in a watchdog timer ISR - and i see the right action after 5 watchdog wakeups, so the right part of the expression works. I guess it has something to do with operation order and brackets.
Please have a look at How to use the forum. Especially the part about code snippets and code tags
Is intCnt declared volatile?
yes, is volatile. Sorry i was to blind to see very first formatting option shame on me i 've just seen link, image etc.
intCnt > 36/8
Is intCnt an int by any chance ?
UKHeliBob:
intCnt > 36/8
Is intCnt an int by any chance ?
Yes it is - is it a (the) problem?
It's not a problem if you understand that 36/8 equals 4 not 4.5 and you are all right with that.
UKHeliBob:
It's not a problem if you understand that 36/8 equals 4 not 4.5 and you are all right with that.
That's interesting yes i am aware of. originally there was 3600, because i want a timeout of 1 hour with an 8sek. watchdog timer and was too lazy to calculate myself. killing 2 "0"s to debug was again the laziest approach
See what
if ( ( !digitalRead(pin) ) || ( intCnt > 36/8 ) ) { // blink LED .. }
does.
Sometimes you have to force the compiler to do what makes sense to you. Clarity is your friend.
If your code is not delay-bound you might find it reading button bounces. If you don’t know about those, you may end up trying to debug the wrong problem.