digitalWrite doesn't write pin to LOW (Arduino MEGA 2560)

Hi,

I'm working on a code for a project and I noticed that one piece of my code doesn't work as it should.
I'd like to write pin to LOW, connect push button between that pin and reset pin, but Arduino just doesn't write the pin LOW..

Writing the pin LOW is taking place in a function, after the loop. I've tried to write it LOW in setup, but that doesn't work either?

The interesting part is that everything is working when I use the function just on its own in a new code (in the loop), but not when I integrate it to the code where it's supposed to work.

The code as a function:

void SystemReset() {
    systemreset:
    if (digitalRead(RedButton) == HIGH) {
      Serial.println("Checkpoint 1");
        if (buttonActive == false) {
            buttonActive = true;
            buttonTimer = millis();
        }

        if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
            longPressActive = true;
            lcd.setCursor(0, 0);
            lcd.clear();  
            delay(100);
            digitalWrite(51, LOW);
            Serial.println("Checkpoint 2");
            delay(100);
            Serial.println("Long Press");
            lcd.setCursor(0, 0);
            lcd.print("ORANGE = RESET");
            lcd.setCursor(0, 2);
            lcd.print("GREEN = RETURN");
            reset = 1;    
        }
        Serial.println("Checkpoint 3");
    }
    
    else {
        if (buttonActive == true) {
          if (longPressActive == true) {
            longPressActive = false;
          }
        else {
          Serial.println("Short press");
        }
      buttonActive = false;
        }
    } 
if((reset == 1) && (digitalRead(STARTbutton) == HIGH)){
      digitalWrite(51, HIGH);
      lcd.clear();
      Serial.println("Green pressed");
      reset = 0;
      lcd.clear();
      return;
    }

    goto systemreset;
}

As you can see I've set some Checkpoints up for Serial monitor and it passes the digitalWrite(51, LOW), but doesn't do anything..

And here's to code in a loop:

void loop() {
  
  if (digitalRead(RedButton) == HIGH) {
    if (buttonActive == false) {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
      longPressActive = true;
      lcd.setCursor(0, 0);
      lcd.clear();  
      delay(100);
      digitalWrite(51, LOW);
      Serial.println("Long Press");
      lcd.setCursor(0, 0);
      lcd.print("Orange = Reset");
      lcd.setCursor(0, 2);
      lcd.println("Green = Return");
      reset = 1;
      
    
    }

    }
    else {
    if (buttonActive == true) {
      if (longPressActive == true) {
        longPressActive = false;
      } else {
        Serial.println("Short press");
      }
      buttonActive = false;
    }
  }
if((reset == 1) && (digitalRead(GreenButton) == HIGH)){
      digitalWrite(51, HIGH);
      lcd.clear();
      Serial.println("Green pressed");
      reset = 0;
      lcd.clear();
    }

}

The bottom version is working fine, but the top version not at all..

Any help would be appreciated.

Thanks :slight_smile:

it passes the digitalWrite(51, LOW), but doesn't do anything..Are we certain it's an output?

systemreset:Uh-oh

AWOL:

it passes the digitalWrite(51, LOW), but doesn't do anything..

Are we certain it's an output?

systemreset:

Uh-oh

Yes, I'm setting it as an output in setup:

void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  Serial.begin(9600);
  pinMode(51, OUTPUT);
  pinMode(RedButton, INPUT);
  digitalWrite(51, HIGH);
}

I don't understand your comment about "systemreset:"? It's just a label to keep it going in circle, might that cause an issue?

renekopso:
Yes, I'm setting it as an output in setup:

Wouldn't it have saved time to have posted that straight off?

I don't understand your comment about "systemreset:"? It's just a label to keep it going in circle, might that cause an issue?

Its presence implies the presence of a "goto".

We hates "goto"s

AWOL:
Wouldn't it have saved time to have posted that staright off?
Its presence implies the presence of a "goto".

We hates "goto"s

About saving time - I'm not sure, since when I posted that info now, I still didn't get any relevant answer about the topic.
About goto - what's the better alternative?

And back to the original problem - what might be the issue?

It's just a label to keep it going in circle,

About goto - what's the better alternative?

Have you thought about letting the loop() function do what its name implies ?

Is it possible that this section of code is running when you don't want it to ?

  if ((reset == 1) && (digitalRead(GreenButton) == HIGH))
  {
    digitalWrite(51, HIGH);
    lcd.clear();
    Serial.println("Green pressed");
    reset = 0;
    lcd.clear();
  }

What output do you see on the Serial monitor ?
Do you set pin 51 HIGH anywhere else in the program ?

UKHeliBob:
Have you thought about letting the loop() function do what its name implies ?

Is it possible that this section of code is running when you don't want it to ?

  if ((reset == 1) && (digitalRead(GreenButton) == HIGH))

{
    digitalWrite(51, HIGH);
    lcd.clear();
    Serial.println("Green pressed");
    reset = 0;
    lcd.clear();
  }




What output do you see on the Serial monitor ?
Do you set pin 51 HIGH anywhere else in the program ?

The reason why I don't want it to be in the loop() is that I'd like to use it multiple times and there are functions in the loop that needs to be stopped once this the reset menu pops up.

The section that you pointed out will run only when green button is pressed. Once it runs, it will display "Green pressed" in the serial monitor - that hasn't happened without green has been pressed.

Output on the Serial monitor:

Checkpoint 1
Checkpoint 3
Checkpoint 1
Checkpoint 3
Checkpoint 1
Checkpoint 2
Long Press
Checkpoint 3

Checkpoints 1 and 3 are repeating because the button is being held down. Checkpoint 2 shows it went past digitalWrite(51, LOW);

digitalWrite(51, HIGH); is only used in the setup.

Even if I set it LOW in the setup, it won't change...

The reason why I don't want it to be in the loop() is that I'd like to use it multiple times and there are functions in the loop that needs to be stopped once this the reset menu pops up.

Sounds like a state machine would fit the bill

Even if I set it LOW in the setup, it won't change...

That makes it sound like the pin is toast. Can you control the state of other pins ?

UKHeliBob:
Sounds like a state machine would fit the bill
That makes it sound like the pin is toast. Can you control the state of other pins ?

The same pin works fine when I test the same piece of code in the loop();.

Okay, I just tested pin 53 and that one works good.

How come the pin is working okay in one code and it doesn't do anything in another?

renekopso:
About saving time - I'm not sure, since when I posted that info now, I still didn't get any relevant answer about the topic.
About goto - what's the better alternative?

And back to the original problem - what might be the issue?

I don't know - I still can't see all your code or your schematic.

About goto - what's the better alternative?

Just about any structured loop construct.

do you have some SPI device attached? some shield? pins s 50, 51, 52 are SPI pins. the same pins are on the ICSP header used by shields

Juraj:
do you have some SPI device attached? some shield? pins s 50, 51, 52 are SPI pins. the same pins are on the ICSP header used by shields

Thanks for that, yes I've got a shield and I'm using SPI.h library. I was just thinking if some libraries or shields might cause it.

This issue is now solved and I got the answers. Thanks everyone who helped me!

renekopso:
Thanks for that, yes I've got a shield and I'm using SPI.h library. I was just thinking if some libraries or shields might cause it.

This issue is now solved and I got the answers. Thanks everyone who helped me!

...and that's why we ask you to post as much information as possible, as soon as possible.

1 Like