Problem with buttons and digitalRead()

Hello, I'm trying to make a system that when starting, a led starts to turn on and off in 500ms until I hold a button (change to HIGH) and stop blinking, leaving the led off, displaying a message, and when I release this button, shows another message and exit.

Using this algorithm below, when starting, the led already starts on and stay on, and I get the message "OKOKOK" and "waiting". When I start with the button pressed, the LED really flashes, but when released, it remains on.

Is this algorithm right?

int led = 3;
int button = 8;

boolean isOk()
{
  byte pin = button;
  if (pin < 100)
  {
    return (digitalRead(pin) == HIGH);
  }
  delay(500);
}

boolean testOk()
{
  do{
    byte pin = led;
    if (pin < 100) {
      pinMode(pin, OUTPUT);
      digitalWrite(pin, HIGH);
      delay(500);
      digitalWrite(pin, LOW);
    }
    delay(500);
  } while ((!isOk));
  if (isOk){
    Serial.println("OKOKOK");
    while (isOk) {
      Serial.println("waiting...");
    }
    if (!isOk) {
      Serial.println("Finish");
      return true;
    }
    else {
      return false;
    }
  }
  else
  {
    return false;
  }
}

Thanks!

Where is your setup() and loop() ?

I am not 100% sure, but I think this:

if (isOk)

does not call the isOk() function. Replace all

 (isOk)

by

 (isOk() )

Sorry.

int led = 3;
int button = 8;

boolean isOk()
{
  byte pin = button;
  if (pin < 100)
  {
    return (digitalRead(pin) == HIGH);
  }
  delay(500);
}

boolean testOk()
{
  do{
    byte pin = led;
    if (pin < 100) {
      pinMode(pin, OUTPUT);
      digitalWrite(pin, HIGH);
      delay(500);
      digitalWrite(pin, LOW);
    }
    delay(500);
  } while ((!isOk()));
  if (isOk()){
    Serial.println("OKOKOK");
    while (isOk()) {
      Serial.println("waiting...");
    }
    if (!isOk()) {
      Serial.println("Finish");
      return true;
    }
    else {
      return false;
    }
  }
  else
  {
    return false;
  }
}

void setup()
{
      Serial.begin(9600);
}

void loop()
{
    if(testOk())
    {
         Serial.println("BUTTON OK");
    }
}

but the operation is still the same. If I change the:

digitalRead(pin) == HIGH

for:

digitalRead(pin) == LOW

It works, but the led is still on.

I wanted the button to start in the LOW state, while it is in LOW, the led lights up and goes out, when pressing it changes to HIGH and shows a message. When I release it, it shows another message and the led goes out.

Your first loop has 2 delays of 500ms, that means it takes at least 1s to complete that loop. In order to get out of that loop, you have to press the button for at least 1s to be sure that you get out of there. Also your first loop is evaluated at least once, so the LED is blinking at least one time, no matter what your button is doing.

The second part

  if (isOk()){
...

is entered if you press the button long enough, you then get "OKOKOK" and many times "waiting...". You are then stuck in that while loop until you release the button. Then you get "Finish" and your testOK() returns true.
The return false statements are never reached.

Then you are back in your main loop() and "BUTTON OK" and the blinking starts over again.

If you don't want to restart the blinking, you should remove your testOk() from the loop() and put it into setup().

If you change isOk() to from HIGH to LOW, you have to also change the conditions in testOk().

What do you mean by that? By looking at the code, your LED should always blink, unless you hold down a button.

Thanks for the answer.
My code should do exactly what you described, but that's not how it behaves.

When the process starts, the button starts with digitalRead as HIGH, and the led lights up without blinking, stays on, so it goes on to the second step of the process, when I press it, it changes to 0 and starts to flash, release, the process ends and the led remains on, without flashing, only on. As in the beginning.

Regardless of whether I put pinMode (pin, INPUT) or pinMode (pin, INPUT_PULLUP), the reading still remains. It always starts with digitalRead () as HIGH.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.