Button not changing light interval.

Hello,

I have an Arduino UNO and I'm taking some snips of example code and code I've found on forums to try and make a button change the interval variable so that the light flashes time can be changed.

It flashes, but the button does not change anything.

Here's what I have:

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin
const int buttonPin = 2;// the number of the pushbutton pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
int interval = 2000;
// constants won't change:
//long interval = 2000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    if(interval == 500)
      interval = 500;
     else 
      interval = 2000;
  }

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Any help would be appreciated!

the code only sets "interval = 500;" if "interval == 500".
it's never going to be set to 500 if interval is 2000

gcjr:
the code only sets "interval = 500;" if "interval == 500".
it's never going to be set to 500 if interval is 2000

Could you give me an example of how I should change it?

Dmdtrain:
Could you give me an example of how I should change it?

The answer is so simple that you should spend some time thinking about it. How to toggle - If something is A, make it B. Otherwise, make it A.

You copy and pasted from a dumb button sketch. It only registers the state of the button before performing an action. That means it will perform that action multiple times while the button is held down. You need to detect a CHANGE OF STATE of the button.

Look at the example sketches, not "snippets from a forum".

const int ledPin =  LED_BUILTIN;// the number of the LED pin
const int buttonPin = 2;// the number of the pushbutton pin

int ledState = LOW;             
int buttonState = 0;         
int lastButtonState = 0;
int buttonPushCounter = 0;

unsigned long previousMillis = 0;
long int interval = 4000;


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
        buttonPushCounter++;

      delay(5);
    }
  lastButtonState = buttonState;
  
  if (buttonPushCounter = 1) {
   (interval=2000);
  }
  else
  if (buttonPushCounter = 2) 
    (interval=1000);
 else     
 if (buttonPushCounter = 3) 
    (interval=500);
 else
 if (buttonPushCounter>3) 
    (buttonPushCounter=0),(interval=4000);
 
 }
 
{ unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
}

This is what I have now. I'm trying to have the button counter change the interval variable, but something isn't working. The light consistently blinks at the 2000 interval which would be pressing it once, but it defaults to that even though it should start at 4000.

Do you see any difference between this

if (buttonPushCounter = 1)

And this, for example

if (ledState == LOW)

?

= is for assignment
== is for comparison

guix:
Do you see any difference between this

if (buttonPushCounter = 1)

And this, for example

if (ledState == LOW)

?

= is for assignment
== is for comparison

Thank you! Basically the last statement I had for setting the interval was constantly reading and changing it then?

It should be like this

if (buttonPushCounter == 1)
{
    interval = 2000;
}
else if (buttonPushCounter == 2)
{
    interval = 1000;
}
else if (buttonPushCounter == 3)
{
    interval = 500;
}
else if (buttonPushCounter > 3)
{
    buttonPushCounter = 0; 
    interval = 4000;
}

Or use switch-case which is designed for this:

switch (buttonPushCounter)
{
case 1:
  interval = 2000;
  break ;
case 2:
  interval = 1000;
  break ;
case 3:
  interval = 500;
  break ;
default:
  if (buttonPushCounter > 3)
  {
    buttonPushCounter = 0;
    interval = 4000;
  }
}

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