code ignoring need for digitalRead to be HIGH

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.

int rollRandom;

void setup() {               
pinMode(8, INPUT);
randomSeed(analogRead(0));
Serial.begin(9600);
}

void loop() {

 
if (digitalRead(8) == HIGH);
  {
  rollRandom = random(1, 7);

  if (rollRandom == 6) 
  {
   // write '6'
  Serial.print(6);
  delay(4000);
  }
  if (rollRandom == 5) 
  {
   // write '5'
  Serial.print(5);
  delay(4000);
  }
  if (rollRandom == 4) 
  {
   // write '4'
  Serial.print(4);
  delay(4000);
  }
  if (rollRandom == 3) 
  {
   // write '3'
  Serial.print(3);
  delay(4000);
  }
  if (rollRandom == 2) 
  {
   // write '2'
  Serial.print(2);
  delay(4000);
  }
  if (rollRandom == 1) 
  {
   // write '1'
  Serial.print(1);
  delay(4000);
  }
 }

 }

Lose the ;

if (digitalRead(8) == HIGH);

manor_royal:
Lose the ;

if (digitalRead(8) == HIGH);

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.

Lose the semicolon.

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:

if (rollRandom == 6) 
  {
   // write '6'
  Serial.print(6);
  delay(4000);
  }

There, the print and delay in the {} "belong" to the if since the if's not prematurely terminated by a spurious ;

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 :slight_smile:

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) == 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.

Note my edit in previous post which I forgot first time: changed the HIGH to LOW.

bwords:
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

For an IF the matched pair of braces is the equivalent of the colon plus the indentation in Python

...R

jurs:
MY PROBLEM IS: HUNDREDS OF NEWBIES ARE FLOODING THIS FORUM WITH NEW TOPICS ABOUT NON-WORKING PUSH BUTTONS EACH YEAR!

Your knickers are in an unnecessary knot.

OP actually had his button nicely managed with a pull down resistor- problem was in his code, and his button read was being missed.

An apology is in order.

jurs:
That might be YOUR PROBLEM.

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:

                                             +--------------- o/o--------  +5V

|                                            |              Switch
|                                            |
|                                            |
|                                            |
|                                            |
|                                            |
|                                            |
|                                            |
|                                            |
                                        INPUT(Arduino)





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.

  if (rollRandom == 6)
  {
   // write '6'
  Serial.print(6);
  delay(4000);
  }
  if (rollRandom == 5)
  {
   // write '5'
  Serial.print(5);
  delay(4000);
  }
  if (rollRandom == 4)
  {
   // write '4'
  Serial.print(4);
  delay(4000);
  }
  if (rollRandom == 3)
  {
   // write '3'
  Serial.print(3);
  delay(4000);
  }
  if (rollRandom == 2)
  {
   // write '2'
  Serial.print(2);
  delay(4000);
  }
  if (rollRandom == 1)
  {
   // write '1'
  Serial.print(1);
  delay(4000);
  }

Do you realize that you could have just written the following: :confused:

  Serial.print(rollRandom);
  delay(4000);

stuart0:

  if (rollRandom == 6)

{
  // write '6'
  Serial.print(6);
  delay(4000);
  }
  if (rollRandom == 5)
  {
  // write '5'
  Serial.print(5);
  delay(4000);
  }
  if (rollRandom == 4)
  {
  // write '4'
  Serial.print(4);
  delay(4000);
  }
  if (rollRandom == 3)
  {
  // write '3'
  Serial.print(3);
  delay(4000);
  }
  if (rollRandom == 2)
  {
  // write '2'
  Serial.print(2);
  delay(4000);
  }
  if (rollRandom == 1)
  {
  // write '1'
  Serial.print(1);
  delay(4000);
  }




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.