Push button skips value

Hi, I have an Arduino UNO and I'm trying to create a LED wave that will change its pattern when a push button is pressed (at pin 12), when it's pressed it will increase the mode variable and use the pattern for that value, but when I try to reset it to 1 (after reaching more than 3) it just skips 1 and goth to 2

I'm using a push button on DPIN 12
And I'm using a variable resistor on APIN 0 to get the delay time

And this is my code (for a led wave), but whenever mode reaches 3 and try to reset it to 1 it just skips to 2

const byte firstPin = 2;
const byte lastPin = 9;
byte mode = 1;
int delayValue;
byte currentPin = firstPin;
bool dir = true;

void setup()
{
  Serial.begin(300);
  pinMode(12, INPUT);
  for (byte i = firstPin; i <= lastPin; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop()
{
  if (digitalRead(12) == HIGH)
  {
    mode++;
    if (mode > 3)
    {
      mode = 1;
    }
    Serial.println(mode);
    currentPin = firstPin;
    dir = true;
    for (byte i = firstPin; i <= lastPin; i++)
    {
      digitalWrite(i, LOW);
    }
  }
  delayValue = analogRead(0) + 100;
  if (mode == 1)
  {
    if (dir)
    {
      digitalWrite(currentPin, HIGH);
    }
    else
    {
      digitalWrite(currentPin, LOW);
    }
  }
  else if (mode == 2)
  {
    digitalWrite(currentPin, HIGH);
    delay(delayValue);
    digitalWrite(currentPin, LOW);
  }
  else if (mode == 3)
  {
    if (dir)
    {
      digitalWrite(currentPin, HIGH);
      digitalWrite(lastPin - currentPin + firstPin, HIGH);
    }
    else
    {
      digitalWrite(currentPin, LOW);
      digitalWrite(lastPin - currentPin + firstPin, LOW);
    }
    delay(delayValue);
  }
  currentPin++;
  if (currentPin > lastPin)
  {
    currentPin = firstPin;
    dir = !dir;
    if (mode == 1)
    {
      delay(delayValue);
    }
  }
}

Check out the "Debounce" example in the Arduino IDE and base your mode counter on the state of the button, not on the digital read of the button Pin. Also check out the "Blink without delay" example as the delays in your code will block any reading of the button pin.

2 Likes

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

1 Like

My tutorial for detecting the state change on an active low input will dectect when the pin becomes low as oppsed to when the pin is low.

Hi, you asked why your sketch skips mode number 1, and it's right there, in the code:

  1. You start asigning 1 to your mode variable
  2. After entering the loop one of the first things you do is incrementing it without using it yet... now your mode variable holds a value of 2.
  3. After that you use it for the first time... with a value of 2!.

Several solutions to solve the problem:

  1. Start asigning a value of 0 to the variable in the setup, then it'll work just fine
  2. Rework all the increment and value checking in a proper order, it might be something like this:
    a. Start the loop with mode initialized to 1
    b. Execute your tasks
    c. After executing your task, increment the mode value
    d. Check if the value went out of range (3) and reseted to the first valid value (1) if that happened.
    e. Here your main loop() code ends and restarts with the new value...

Solution 1) seems easier in this case... but it'll make a goog practice to set it by the second sugestion.

Good Luck.
Gaby.//

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