2560 problem with digitalread

Hello all.

I am writing a fairly lengthy program to control my 3d printer heater cabinet.

I have 4 buttons (up, down, enter and back). These are monitored using basically the following line:

if (digitalRead(EnterButton)==HIGH); { << do something >>; }

The buttons are wired how I usually do... 5v high when you push them, with the Arduino pin held low with a 10k resistor.

The entire thing works fine, except in certain circumstances, it reads this digitalRead button as high, even though the pin is obviously low?

For instance, I have a part of the program that runs through a shutdown sequence and I need the option to 'abort shutdown'.
This consists of sounder a piezo, a delay(1000); and printing text to a TFT screen.

If I put then put the digitalRead command after that, it reads it as high, when the pin is low?

Any suggestions?
Is there a known glitch or process on the Arduino that would cause this?

Any suggestions?

Post your whole program or a small but complete program that exhibits the problem. If/when you do, select the code in the forum editor and click the code tags icon (</>) top/left of the editor window to enclose the code in tags that make it easier to deal with in the forum.

Stevolution:
Is there a known glitch or process on the Arduino that would cause this?

Sounds like a hardware issue - is this control box remote from the ARduino? Do its
wires run near any other wiring?

Well just tried to upload the code and it said too many character (max 9000).
My code isn't pretty... I know that. Its also FAR from completed.

Its not hardware (pretty sure). Its on my development board, so wires are short.
Anyway, the buttons work fine elsewhere in the program.

I had this issue once before and had to program my way around it then.

Here is a snippet of the code...

This works fine. I jump here from the main 'loop', and it returns a variable in Checkbutton depending on the button pressed....

void Checkbuttons () {
  
    if (digitalRead(DownButton)==HIGH)  { Checkbutton=1; }                                          //Down button
  
    if (digitalRead(UpButton)==HIGH)    { Checkbutton=2; }                                          //Up button

    if (digitalRead(EnterButton)==HIGH) { Checkbutton=3; }                                          //If Enter is pressed during main menu
    
   delay(1);
  
    }

This however fails.... it instantly carries out the digitalWrite command after it (it does it whether I ask for digitalRead(EnterButton== High or Low)

void Shutdowncancel () {
      
          if (digitalRead(EnterButton)==HIGH); { digitalWrite(ResetIC, HIGH); }      
}

Change that to:

void Checkbuttons () {
    Checkbutton = 0 ;
    if (digitalRead(DownButton)==HIGH)  { Checkbutton=1; }                                          //Down button
 
    if (digitalRead(UpButton)==HIGH)    { Checkbutton=2; }                                          //Up button

    if (digitalRead(EnterButton)==HIGH) { Checkbutton=3; }                                          //If Enter is pressed during main menu
   
   delay(1);

Stevolution:
This however fails.... it instantly carries out the digitalWrite command after it (it does it whether I ask for digitalRead(EnterButton== High or Low)

void Shutdowncancel () {

if (digitalRead(EnterButton)==HIGH); { digitalWrite(ResetIC, HIGH); }     
}

Of course it fails :smiley: You have a semi-colon after the if statement. So the next statement (between the curlies) is not part of the if block and will therefore always be executed.

Stevolution:
Is there a known glitch or process on the Arduino that would cause this?

Why do people always suspect hardware errors :smiley: The answer to the question is yes and it's called the person behind the keyboard :smiley:

EBKAC - error between keyboard and chair 8)
or PEBKAC - problem exists between keyboard and chair

Looks like you need more debouncing, too.