Switch delay variable with push button

Could I potentially make the delay in my code a variable that could be switched based on button presses?

IE =

one button press = delay 1000
Two button press = delay 2000

I've looked for a while and I can't find an example of anyone doing this, is it possible?

You certainly can. Have you tried? What did your try look like.

int delayTime = 1000;

// some code to adjust the value of delayTime however you want

delay(delayTime);

What happens if you press a button once, but twice? :slight_smile:

Delta_G:
You certainly can. Have you tried? What did your try look like.

int delayTime = 1000;

// some code to adjust the value of delayTime however you want

delay(delayTime);

I have not tried as i thought it wasnt possible…
what do you mean by (some code to adjust value of delaytime)

I mean write some code that reads a button press and changes the value of delayTime variable, or possibly reads a pot and adjusts it or whatever you want to do to change the value in that variable.

If that is beyond you, then you need to go back and work through a few tutorials and examples and try to familiarize yourself with the language.

#include <Manchester.h>

  #define TX_PIN 0 //pin where your transmitter is connected
  const int hallPin = 4; // define pin hall effect
  int hallState = 1; // state of hall effect
  
  const int TILT = 2; //define pin tilt switch
  int TILTState = 1;// state of tilt switch

  
  int delayTime = 1000;// int for delay
  const int buttonPin = 3;//Define pin Button
  int buttonPushCounter = 0;
  int buttonState = 0;
  int lastButtonState = 0;
  

  uint16_t transmit_data = 2761;
  uint16_t transmit_t = 2000;
  uint16_t transmit_y = 4000;
  uint16_t transmit_g = 5000;
  
  boolean onState = false;
  boolean onStatetwo = false;
  void setup() {
    
  man.setupTransmit(TX_PIN, MAN_1200);
  pinMode(hallPin, INPUT);
  pinMode(TILT, INPUT);
  pinMode(button, INPUT);
  
} 
  
   void loop() {

    
    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  [b]// turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}
[u]Here instead of digitalWrite to turn led on what would i use to change a variable?[/u]
[/b]

    
    
    int newHallState = digitalRead(hallPin);
    
    // if different state and in proximity
    if( (newHallState != hallState) && (newHallState == LOW))
    {
     
      // flip on/off state
      onState = !onState;
      
    if (onState)
    {
      man.transmit(transmit_data);
      man.transmit(transmit_t); 
    } else {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
      }
    }
    
     // save new state
      hallState = newHallState;

      /////////////////////////////

      int newTiltState = digitalRead(TILT);
    
    // if different state and in proximity
    if( (newTiltState != TILTState) && (newTiltState == LOW))
    {
     
      // flip on/off state
      onStatetwo = !onStatetwo;
      
    if (onStatetwo)
    {
      man.transmit(transmit_data);
      man.transmit(transmit_t); 
    } else {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
      }
    }
    
     // save new state
      TILTState = newTiltState;
  }

Delta_G:
I mean write some code that reads a button press and changes the value of delayTime variable, or possibly reads a pot and adjusts it or whatever you want to do to change the value in that variable.

If that is beyond you, then you need to go back and work through a few tutorials and examples and try to familiarize yourself with the language.

What examples could i read in order to familiarize myself with this?

There are a number of examples that come with the IDE. The State Change example has a counter being incremented in response to a button being pushed.

Google “C++ tutorial” and you will find literally thousands of pages to read that will help you familiarize yourself with the language.

ive read that example lots of times
would this work?

if (buttonPushCounter % 1 == 0) {
    int delayTime = 1000;
  }
  if (buttonPushCounter % 2 == 0) {
    int delayTime = 2000;
  }
  if (buttonPushCounter % 3 == 0) {
    int delayTime = 3000;
  }

is it really that simple?

Every time you put the word "int" there, you are creating a new variable with the same name as one you already have. It only exists between the { and } where it was created. So lose the "int" in front of those and yes, something like that would work.

An alternative to replace all of that would be:

delayTime = buttonPushCounter * 1000;

Delta_G:
Every time you put the word "int" there, you are creating a new variable with the same name as one you already have. It only exists between the { and } where it was created. So lose the "int" in front of those and yes, something like that would work.

An alternative to replace all of that would be:

delayTime = buttonPushCounter * 1000;

So i tried taking the int off

if (buttonPushCounter % 1 == 0) {
     delayTime = 1000;
  }
  if (buttonPushCounter % 2 == 0) {
     delayTime = 3000;
  }
  if (buttonPushCounter % 3 == 0) {
     delayTime = 5000;
  }
  if (buttonPushCounter % 4 == 0) {
     delayTime = 10;
  }

it doesnt work though.
what doesnt make sense is where would i place
delayTime = buttonPushCounter * 1000;?

If it doesn’t work maybe it has something to do with the rest of the code you didn’t show us. Maybe if you showed the rest of the code and explain what you mean by “doesn’t work” we could help you sort it out.

You would put that line in the place in the code where you want to calculate a new delay time. Code runs from top to bottom. Think about what you want to do. Write down the steps. Simple steps that a computer can do. Then you translate that into code. Code works from top to bottom just how you write it. First this thing happens then that thing happens then the next thing. So if you want that calculation to happen right before you call delay then you put that line right before the line that calls delay. If you want that calculation to happen right before the servo starts moving, then you put that line right before the line that starts the servo moving. If you want that calculation to happen right after you read the button, then you put that line after the code that reads the button. It’s all sequential and where you put things is all about the order you want them to happen.

Here is my full code.

#include <Manchester.h>

  #define TX_PIN 0 //pin where your transmitter is connected
  const int hallPin = 4; // define pin hall effect
  int hallState = 1; // state of hall effect
  
  const int TILT = 2; //define pin tilt switch
  int TILTState = 1;// state of tilt switch

  
  int delayTime = 5;// int for delay
  const int buttonPin = 3;//Define pin Button
  int buttonPushCounter = 0;
  int buttonState = 0;
  int lastButtonState = 0;
  

  uint16_t transmit_data = 2761;
  uint16_t transmit_t = 2000;
  uint16_t transmit_y = 4000;
  uint16_t transmit_g = 5000;
  
  boolean onState = false;
  boolean onStatetwo = false;
  void setup() {
    
  man.setupTransmit(TX_PIN, MAN_1200);
  pinMode(hallPin, INPUT);
  pinMode(TILT, INPUT);
  pinMode(buttonPin, INPUT);
  
} 
  
   void loop() {

    
    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 1 == 0) {
     delayTime = 1000;
  }
  if (buttonPushCounter % 2 == 0) {
     delayTime = 3000;
  }
  if (buttonPushCounter % 3 == 0) {
     delayTime = 5000;
  }
  if (buttonPushCounter % 4 == 0) {
     delayTime = 10;
  }

    
    int newHallState = digitalRead(hallPin);
    
    // if different state and in proximity
    if( (newHallState != hallState) && (newHallState == LOW) )
    {
     
      // flip on/off state
      onState = !onState;
      
    if (onState)
    
    { delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t); 
    } else {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
      }
    }
    
     // save new state
      hallState = newHallState;

      /////////////////////////////

      int newTiltState = digitalRead(TILT);
    
    // if different state and in proximity
    if( (newTiltState != TILTState) && (newTiltState == LOW))
    {
     
      // flip on/off state
      onStatetwo = !onStatetwo;
      
    if (onStatetwo)
    
    { delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t); 
    } else {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
      }
    }
    
     // save new state
      TILTState = newTiltState;
  }

You say that code doesn't work. What does it do and how does that compare to what you want / expect?

well i guess i should explain what my project does first.

I have 2 atttiny85's one is the tx and the latter is rx (they both are connected via manchester encoding and 433 mhz modules)

the tx has a hall effect sensor and a mercury switch
when there is a magnet present it turns led on rx and stays on till magnet is present again (state change detection)
same thing for the mercury switch.
I coded in a delay for my project before and liked it.
when a magnet was present it would wait about 3 seconds then send the data and then rx would turn on led. I loved it but once im done with this project i want to be able to click a button that changes the delay time and i will probably hook up an RGB led that lights up a certain color for a second to match the certain delay time. so i'll know when i use the mercury switch or hall effect i'll know how long the delay will be for.

right now my code does the same thing it did before i added the delay code which is have no delay no matter how many times the button is pressed so the code did nothing because i prob messed up and did something wrong. lol

When the code is in a delay, it can’t read button presses. It can’t do anything. delay is like putting your chip into a coma for a period of time. If you have a 3 seconds delay in your code and you press your button during that time, nothing will happen because the chip cannot see the button press. It is busy delaying.

If you want to be able to respond to button presses, then you have to get rid of the delay and embrace the technique used in the Blink Without Delay example to handle timing of things by recording the time something happens and calculating later if it is time to do it again or not. That way the rest of your code can continue running and responding to things like button presses.

Delta_G:
When the code is in a delay, it can't read button presses. It can't do anything. delay is like putting your chip into a coma for a period of time. If you have a 3 seconds delay in your code and you press your button during that time, nothing will happen because the chip cannot see the button press. It is busy delaying.

If you want to be able to respond to button presses, then you have to get rid of the delay and embrace the technique used in the Blink Without Delay example to handle timing of things by recording the time something happens and calculating later if it is time to do it again or not. That way the rest of your code can continue running and responding to things like button presses.

Delta_G:
When the code is in a delay, it can't read button presses. It can't do anything. delay is like putting your chip into a coma for a period of time. If you have a 3 seconds delay in your code and you press your button during that time, nothing will happen because the chip cannot see the button press. It is busy delaying.

If you want to be able to respond to button presses, then you have to get rid of the delay and embrace the technique used in the Blink Without Delay example to handle timing of things by recording the time something happens and calculating later if it is time to do it again or not. That way the rest of your code can continue running and responding to things like button presses.

its not going to delay while im pressing the button. the whole idea was to change the variable of the delay when i press the button not actually delay while pressing the button. if you understood my sketch and idea you would then know that delay would only be activated while the mercury switch or hall effect sensor were activated. the button is merely a tool used to change the delay time for it.

so to recap its not working, not because delay is blocking ( i understand that delay causes the ic to freeze)
but its not working because i wrote the code wrong or placed it in the wrong spot.

Could the code be locking because of the many uses of Serial.print without a Serial.begin? I don't know but it is an error anyway.

Or the fact that you're trying to send serial while you've got a radio hooked to the serial tx line?

Wesley5n1p35:
its not going to delay while im pressing the button. the whole idea was to change the variable of the delay when i press the button not actually delay while pressing the button. if you understood my sketch and idea you would then know that delay would only be activated while the mercury switch or hall effect sensor were activated. the button is merely a tool used to change the delay time for it.

so to recap its not working, not because delay is blocking ( i understand that delay causes the ic to freeze)
but its not working because i wrote the code wrong or placed it in the wrong spot.

I think you are saying that you want to advance the delay with each button push… a sort of State Machine. Here is your code that will do that (untested) but again, the button press will not be ‘noticed’ by the program during your blocking delays… only when that delay is not active.

#include <Manchester.h>

#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect

const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch


int delayTime = 5;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;


uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;

boolean onState = false;
boolean onStatetwo = false

byte state = 0;

void setup()
{
  Serial.begin(9600); //<<<<<<<<<<<<<<< added in edit
  man.setupTransmit(TX_PIN, MAN_1200);
  pinMode(hallPin, INPUT);
  pinMode(TILT, INPUT);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      state++;
      if (state > 3)
      {
        state = 0;
      }
    }
    switch (state)
    {
      case 0:
        delayTime = 1000;
        break;
      case 1:
        delayTime = 3000;
        break;
      case 2:
        delayTime = 5000;
        break;
      case 3;
        delayTime = 10;
        break
    }
    Serial.print("new delay time= ");
    Serial.println(delayTime);
    delay(50);
  }
  lastButtonState = buttonState;
  
  int newHallState = digitalRead(hallPin);
  if (newHallState != hallState && newHallState == LOW)
  {
    onState = !onState;
    if (onState)
    {
      delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t);
    }
    else
    {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
    }
  }
  hallState = newHallState;

  int newTiltState = digitalRead(TILT);
  if (newTiltState != TILTState && newTiltState == LOW)
  {
    onStatetwo = !onStatetwo;
    if (onStatetwo)
    {
      delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t);
    }
    else
    {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
    }
  }
  TILTState = newTiltState;
}

T
he next step of improving your code would be to make all this happen without the delay() function…

#include <Manchester.h>

#define TX_PIN 0 //pin where your transmitter is connected
const int hallPin = 4; // define pin hall effect
int hallState = 1; // state of hall effect

const int TILT = 2; //define pin tilt switch
int TILTState = 1;// state of tilt switch


int delayTime = 5;// int for delay
const int buttonPin = 3;//Define pin Button
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;


uint16_t transmit_data = 2761;
uint16_t transmit_t = 2000;
uint16_t transmit_y = 4000;
uint16_t transmit_g = 5000;

boolean onState = false;
boolean onStatetwo = false;

byte state = 0;

void setup()
{
  man.setupTransmit(TX_PIN, MAN_1200);
  pinMode(hallPin, INPUT);
  pinMode(TILT, INPUT);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      state++;
      if (state > 3)
      {
        state = 0;
      }
    }
    switch (state)
    {
      case 0:
        delayTime = 1000;
        break;
      case 1:
        delayTime = 3000;
        break;
      case 2:
        delayTime = 5000;
        break;
      case 3:
        delayTime = 10;
        break;
    }
    Serial.print("new delay time= ");
    Serial.println(delayTime);
    delay(50);
  }
  lastButtonState = buttonState;
  
  int newHallState = digitalRead(hallPin);
  if (newHallState != hallState && newHallState == LOW)
  {
    onState = !onState;
    if (onState)
    {
      delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t);
    }
    else
    {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
    }
  }
  hallState = newHallState;

  int newTiltState = digitalRead(TILT);
  if (newTiltState != TILTState && newTiltState == LOW)
  {
    onStatetwo = !onStatetwo;
    if (onStatetwo)
    {
      delay(delayTime);
      man.transmit(transmit_data);
      man.transmit(transmit_t);
    }
    else
    {
      man.transmit(transmit_y);
      man.transmit(transmit_g);
    }
  }
  TILTState = newTiltState;
}

cleaned up a few spots that had were wrong and were not compiling, compiles just fine now. however no matter how many button presses it gives me about 3 second delay.

how would i go about doing this without delay? i’ve looked at the examples but they dont make sense to me…

arghhh i wish this made more sense!

How have you got the button wired? Are the presses even registering?

PS. You're still doing serial prints without calling Serial.begin and with a radio hooked to your serial tx line. That in itself may be causing issues.