I've used this method before, but it doesn't work now. (Toggling a Button)

I've used this method before in my code in order to detect the switch from HIGH to LOW in my button, but now it doesn't work. I've tried multiple things included adding resistors, making it INPUT_PULLUP, and changing the code around.

Whenever the button is LOW, v_press should be 0. Whenever it is HIGH, it should set it to 1 and wait for it to turn LOW before it sets it to 0 again. However, when I press the button it somehow triggers the last code of "digitalRead(variable) == LOW && v_press == 1" in every "if statement", making it true, setting it back to 0, and repeating the process over again, endlessly cycling the "if" blocks entirely.

How can I fix this? May someone please look over my code?

void loop(){
  //Cycle Chars
  if (digitalRead(v_cycle) == HIGH && (v_mode == 0 || v_mode == 1) && v_press == 0)
  {
    v_press = 1;
    v_abc += 1;
    if (v_abc > 26)
    {
      v_abc = 1;
    }
    msg();
  }
  else if (digitalRead(v_cycle) == HIGH && v_mode == 2 && v_press == 0)
  {
    v_press = 1;
    v_chr += 1;
    if (v_chr > 18)
    {
      v_chr = 1;
    }
    msg();
  }
  else if (digitalRead(v_cycle) == LOW && v_press == 1);
  {
    v_press = 0;
  }
  
  //Toggle Caps
  if (digitalRead(v_caps) == HIGH && v_mode == 0 && v_press == 0)
  {
    v_press = 1;
    v_mode = 1;
    msg();
  }
  else if (digitalRead(v_caps) == HIGH && v_mode == 1 && v_press == 0)
  {
    v_press = 1;
    v_mode = 0;
    msg();
  }
  else if (digitalRead(v_caps) == LOW && v_press == 1)
  {
    v_press = 0;
  }
  
  //Cycle Mode
  if (digitalRead(v_chars) == HIGH && v_press == 0)
  {
    v_press = 1;
    v_mode ++;
    if (v_mode > 2)
    {
      v_mode = 0;
    }
    msg();
  }
  else if (digitalRead(v_chars) == LOW && v_press == 1)
  {
    v_press = 0;
  } 
  
  //Print Character
  if (digitalRead(v_outp) == HIGH && v_mode == 0 && v_press == 0)
  {
    v_press = 1;
    s_msg = s_msg + s_chr[0].substring(v_abc - 1, v_abc);
    msg();
  }
  else if (digitalRead(v_outp) == HIGH && v_mode == 1 && v_press == 0)
  {
    v_press = 1;
    s_msg = s_msg + s_chr[1].substring(v_abc - 1, v_abc);
    msg();
  }
  else if (digitalRead(v_outp) == HIGH && v_mode == 2 && v_press == 0)
  {
    v_press = 1;
    s_msg = s_msg + s_chr[2].substring(v_chr - 1, v_chr);
    msg();
  }
  else if (digitalRead(v_outp) == LOW && v_press == 1)
  {
    v_press = 0;
  }
}

else if (digitalRead(v_cycle) == LOW && v_press == 1);

;

:wink:

NO I just KNEW it'd be a stupid error!

Holy crap, thank you so much. It was 4am when I was making this.

LarryD:
else if (digitalRead(v_cycle) == LOW && v_press == 1);

;

:wink:

Well crap. I got all excited. It still infinitely loops the if blocks, cycling through the variables (like v_abc) at lightning speed.

My last one actually had two or three buttons.

But thank you! I'll use that and try to fix it. I think I know what to do!

That's exactly what was wrong. I created an individual v_press for every function, and used them all separately. Thank you so much!

Break your problem down into sections.
Put some Serial.print statements in at strategic locations to prove your assumptions.
Toggle a led to see if you get to a location you think it is getting to.