Hello all, I am just getting started with arduino, I've been doing hobby electronics for a while but haven't got into arduino too much because I've always struggled to get to grips with C as a language (I have some experience with Python and Javascript)
I am trying to make a little dice roller using a 7 segment display, but for some reason no matter how I phrase the code the digitalRead executes with or without me pressing a button, I have since narrowed the code down to just printing to serial for the sake of debugging.
The hardware is just as follows:
a jumper from 5v --> Pushbutton IN
a 10k resistor from Pushbutton OUT --> GND
a jumper from Pushbutton OUT --> pin 8
the problem is when I open the serial terminal the code is running and rolling random regardless of whether I push the button, and in fact the push button does nothing. This persists whether I use (digitalRead(8) == HIGH) or (digitalRead(8) == LOW) so it seems the arduino is completely ignoring that and I can't see why. ( I have also tried == 1 and == 0 to no avail)
I have looked at other examples of the same kind of code, I even directly copy and pasted one of them, wired the arduino up as recommended to some LEDs and found exactly the same problem.
However when I followed the digitalRead Serial tutorial it all worked fine so I know it's just me being silly somewhere and messing up the code, I just can't see where.
But if I do that there will be no effect from the push button at all surely? it will just well.. do what it is currently doing, roll random regardless of how I interact with it.
The ; terminates that if line, so everything in the {} underneath it runs anyway, since it's got naff all to do with the if. Compare that if to your others like this one:
ahhh thank you! I'm too used to doing Python so I never know where to put these semi-colons and where to leave them out, I think I need to spend some time just looking through syntax in C so I can avoid these mistakes in future.
Thanks again that works great
Cool... now you can simplify things even more. The Arduino has built in pullup resistors, so if you use this syntax:
pinMode(8, INPUT_PULLUP);
.... you can do away with the need to fanny about with the external resistor.
Note though that it's a pullUP not DOWN like you have at the moment, so your pin will now be naturally high not low. Simply change the logic then, to look for a low.
manor_royal:
Cool... now you can simplify things even more. The Arduino has built in pullup resistors, so if you use this syntax:
pinMode(8, INPUT_PULLUP);
.... you can do away with the need to fanny about with the external resistor.
Note though that it's a pullUP not DOWN like you have at the moment, so your pin will now be naturally high not low. Simply change the logic then, to look for a low.
if (digitalRead(8) == HIGH)
ahh yes I've seen places saying there are internal pullups I didn't know how to activate them, will do, thanks for all your help.
MY PROBLEM IS: HUNDREDS OF NEWBIES ARE FLOODING THIS FORUM WITH NEW TOPICS ABOUT NON-WORKING PUSH BUTTONS EACH YEAR!
I believe, that many hundreds of those newbies just use wrong hardware circuit for the button and nonnect the button to a "floating" INPUT of the microcontroller while the button is not pressed.
Circuit schematics for "floating" (wrong!) button circuit as ASCII text:
Working button circuits will have to use instead:
- either external pull-down resistor
- or external pull-up resistor
- or at least "internal pull-up resistor" (activated in software setting pinmode to INPUT_PULLUP)
while I don't quite see how this relates to the issue I was having since my button was wired perfectly well, I respect that this might be useful to other people coming along with a similar issue and finding that it is in fact their button wiring and not their code that is the issue.
However it's a pretty easy thing to get wrong when you are first starting out with electronics, you could instead direct them to something like https://www.arduino.cc/en/Tutorial/InputPullupSerial <-- this, which outlines how to use the internal pullup or https://www.arduino.cc/en/Tutorial/DigitalReadSerial <-- this, which actually shows using an external pulldown. Or just.. I don't know, ignore those posts if they grind your gears.
Do you realize that you could have just written the following: :confused:
Serial.print(rollRandom);
delay(4000);
yes, but the serial was only there for debugging, the actual code required that I do that because it was setting specific output pins depending on the result (to a 7 seg display), I know there are more efficient ways of doing it then even so which I am working on now. but with what the code was going to end up being just " Serial.print(rollRandom);
delay(4000);" would have only been useful until I got it working, and I wanted to keep printing to serial while displaying on the 7seg to check afterwards so it doesn't make much difference.