How can i use 2 buttons/2 pole switch to have two different values for a set (timer) 1 or 2 min code for game i have is ok need timer to be fixed

Very big noob here , stupid question how do i set a button to have a different variable if on or off?
on value of 60000 millis one minute or off 120000 millis two minutes

code works as well as i need for now just need the timer to work for the end of loop

Thank you for the assistance ...


int relay1=2;
int relay2=3;
int relay3=4;
int relay4=5;
int relay5=6;
int relay6=7;
int relay7=8;
int relay8=9;
int input1=13;
int reset=12;
int timerstate=10;
int relay =-1;


int buzzer =11;
#include <ezButton.h>
//button for 1 or two minutes
// define button for timer 1 or two minutes
#define timer1min 60000
#define timer2min 120000

//button for start
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1

ezButton button(13);  // create ezButton object that attach to pin 13;
int loopState = LOOP_STATE_STOPPED;

void setup() {
  // put your setup code here, to run once:
 
  pinMode (timerstate,INPUT);
  pinMode (relay1,OUTPUT);
  pinMode (relay2,OUTPUT);
  pinMode (relay3,OUTPUT);
  pinMode (relay4,OUTPUT);
  pinMode (relay5,OUTPUT);
  pinMode (relay6,OUTPUT);
  pinMode (relay7,OUTPUT);
  pinMode (relay8,OUTPUT);
  pinMode (input1,INPUT);
   pinMode (reset,OUTPUT);
   
  pinMode (buzzer,OUTPUT);
 button.setDebounceTime(50); // set debounce time to 50 milliseconds




  


digitalWrite  (relay1,HIGH);
digitalWrite (relay2,HIGH);
digitalWrite  (relay3,HIGH);
digitalWrite (relay4,HIGH);
digitalWrite (relay5,HIGH);
digitalWrite (relay6,HIGH);
digitalWrite  (relay7,HIGH);
digitalWrite (relay8,HIGH);











//  beep setup game


 digitalWrite(buzzer,HIGH);
delay (300);
digitalWrite (buzzer,LOW);
delay (3000);
// waiting for start game on input1




digitalWrite(buzzer,HIGH);
delay (300);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (300);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (300);
digitalWrite (buzzer,LOW);

}




void loop (){
 
// start loop on button 13
button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {
    if (loopState == LOOP_STATE_STOPPED)
      loopState = LOOP_STATE_STARTED;
    else // if(loopState == LOOP_STATE_STARTED)
      loopState = LOOP_STATE_STOPPED;
  }

  if (loopState == LOOP_STATE_STARTED) {



// Randomly decide which relay  to turn on
delay(random(1000, 5000));// random timer for loop
relay =-1;


  relay = rand() % 8+1;
  if (relay==1) {
    digitalWrite(relay1, LOW); 
    delay (200);
    digitalWrite(relay1,HIGH);
  }
  else if (relay==2) {
    digitalWrite(relay2, LOW);
    delay (200);
    digitalWrite(relay2,HIGH);
  }
  else if (relay==3) {
    digitalWrite(relay3,LOW); 
    delay (200);
    digitalWrite(relay3,HIGH);
  } 
  else if (relay==4) {
    digitalWrite(relay4,LOW);
    delay (200);
    digitalWrite(relay4,HIGH);
  }
  else if (relay==5) {
    digitalWrite(relay5, LOW);
    delay (200);
    digitalWrite(relay5,HIGH);
    }
    else if (relay==6) {
    digitalWrite(relay6, LOW); 
    delay (200);
    digitalWrite(relay6,HIGH);
  }
  else if (relay==7) {
    digitalWrite(relay7, LOW);
    delay (200);
    digitalWrite(relay7,HIGH);
  }
  else if (relay==8) {
    digitalWrite(relay8, LOW);
    delay (200);
    digitalWrite(relay8,HIGH);
  }

// finish timer stop game need timer state to be 60000 or 120000 according to input timer pin 12 high or low)

//if timerstate on timerstate = timer1min or 60000 if timerstate off timerstate = timer2min or 120000


if (millis() >= (timerstate)) {
  digitalWrite(buzzer,HIGH);
delay (500);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (500);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (900);
digitalWrite (buzzer,LOW);
delay (100);
exit(0); 

  }
}}
  


one approach is to detect state changes and timestamps

  • capture a timestamp when the button is pressed
  • recognize the longer button press if the time (2 min) expires
  • recognize the shorter button press if the button is released before the time expires

don't know what you expect if the button is released before 1 minute

Thanks for the reply ! just a better description of what i am looking for .
the loop runs for the defined time 1 or two minutes depending on the button / toggle switch .
if button/switch position 1 then timer must equal 1 minute and loop ends after one minute.
if button/switch position two then timer must equal 2 minutes and loop ends after one minute.

wanted to have the timer a single in the code with a variable of two options to exit loop. depending on switch position.

have the option of each position having a seperate pin as it is common ground 3 position switch with a 0 1 2 as options.

simple terms...

if switch in position 1 timer = 60000 millis
if switch in position 2 timer = 120000 millis

if total (timer) end loop

code works fine if i put a constant 60000 or 120000 but would like it in code from switch position.

aren't you looking for an event indicating how long a button was pressed for? processing doesn't start until the event is recognized

or do you want to do something specific until the button is released?

consider

// recognizing short and long button presses

#undef MyHW
#ifdef MyHW
byte pinsBut [] = { A1, A2, A3 };
#else
byte pinsBut [] = { 6, 13 };
#endif

#define N_BUT   sizeof(pinsBut)

enum { Off = HIGH, On = LOW };

byte          butState  [N_BUT];
byte          butReport [N_BUT];

unsigned long butMsec  [N_BUT];
unsigned long ButLong  = 2000;

unsigned long msec;

enum { NoPress, Press, PressLong };

// -----------------------------------------------------------------------------
int
chkButton (
    int  butID)
{
    byte but = digitalRead (pinsBut [butID]);

    if (butState [butID] != but)  {
        butState [butID] = but;
        delay (10);     // debounce

        if (LOW == but)  {
            butMsec   [butID] = msec;
            butReport [butID] = false;
        }
        else if (! butReport [butID])
            return Press;
    }

    else if (LOW == but)  {
        if (! butReport [butID])
            if ( (msec - butMsec [butID]) > ButLong)  {
                butReport [butID] = true;
                return PressLong;
            }
    }

    return NoPress;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    msec = millis ();

    switch (chkButton (0))  {
    case Press:
        Serial.println ("  button-0 pressed");
        break;

    case PressLong:
        Serial.println ("  button-0 pressed-long");
        break;
    }

    switch (chkButton (1))  {
    case Press:
        Serial.println ("  button-1 pressed");
        break;

    case PressLong:
        Serial.println ("  button-1 pressed-long");
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }
}

why not

        relay = rand() % 8+1;
        digitalWrite(relay, LOW);
        delay (200);
        digitalWrite(relay,HIGH);

why not

void setup ()
{
    for (int n = 2; n <= 9; n++)  {
        pinMode (n, OUTPUT);
        digitalWrite (n, HIGH);
    }
    ...
}

LOOKS MUCH SIMPLER THAN MY BASICS JUST LEARNING TO START CODING NOW..

what i need is the loop to run again and again in a loop until a fixed time.

either 1 min or two minutes. depending on what position the switch is in.

simple game where it runs random until time is up.
i have a nice switch that will be easy to understand for the cosmetics with a big 1 and 2 and would be nice to use to give a total loop time of 1minute if toggled to 1 or two minutes if toggled to 2.

just about everyone is.

it's very unclear what you're trying to do.

a single switch, either pressed or not pressed ???
a switch pressed for a short or longer time ?
or are there 2 switches?

so it either runs for 1 or 2 mins, and then runs for 1 or 2 mins, .... when does it not run??

looks like when the button is pressed the state is toggled: Start or Stop

isn't timerState a pin #?
won't millis() be > 10 almost all the time

looks like you what you main code does is turn on then off a randomly selected output (e.g. relay7).
do you want to do this for a specified amount of time: 1 or 2 minutes?
do you have 2 buttons to select the amount of time?

thats exactly what i am trying to do , i have tried one switch with 2 positions which is actually 2 switches ,, my fault there.

the loop will randomly select the outputs at a random time and exit the loop at the selected time in the last code

where i need (timer) to be value of 1 or two minutes according to switch position.
then beep and end loop.

  digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (600);

digitalWrite (buzzer,LOW);
delay (10);
digitalWrite(buzzer,HIGH);
delay (700);
digitalWrite (buzzer,LOW);
delay (20);
digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
exit(0); }

sorry copy pasted a bit wrong.

if (millis() >= (timer)) {  
  digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
 digitalWrite(buzzer,HIGH);
delay (600);

digitalWrite (buzzer,LOW);
delay (10);
digitalWrite(buzzer,HIGH);
delay (700);
digitalWrite (buzzer,LOW);
delay (20);
digitalWrite(buzzer,HIGH);
delay (100);
digitalWrite (buzzer,LOW);
delay (100);
exit(0); }

}

this button pin 13 starts the loop or ends it before the timer is up.
once started i wanted it to loop for the duration of (timer) depending on switch 1 or 2 (pin 1 or pin 10) one minute or two minutes and then exit loop. waiting for reset and restart.

i also see now the millis timer starts at the beginning of reset and not start of loop....

consider (for single button)

#undef MyHW
#ifdef MyHW
const byte RelayPins [] = { 10, 11, 12 };
const byte ButPin       = A1;
const byte BuzzerPin    = 13;

# define OneMinute  4000

#else
const byte RelayPins [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const byte ButPin       = 13;
const byte BuzzerPin    = 11;

# define OneMinute  60000
# define TwoMinute 120000
#endif

// -------------------------------------
#define N_RELAYS    sizeof(RelayPins)

enum { Off = HIGH, On = LOW };

int  run = 0;
byte butState;

unsigned long msecLst;
unsigned long period;

// -----------------------------------------------------------------------------
void
beep (void)
{
    digitalWrite (BuzzerPin, On);
    delay (300);
    digitalWrite (BuzzerPin, Off);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if (run && (msec - msecLst) > period)  {
        Serial.println ("timeout");
        run = false;
    }

    byte but = digitalRead (ButPin);
    if (butState != but)  {         // state change
        butState = but;

        if (LOW == but)  {          // pressed
            Serial.println ("but");
            period  = OneMinute;    // would depend on button
            msecLst = msec;
            run     = ! run;        // toggle run on/off
        }

        // turn all relays off
        if (! run)  {
            for (unsigned n = 0; n < N_RELAYS; n++)
                digitalWrite (RelayPins [n], Off);
            beep ();
        }
    }

    if (run)  {
        Serial.println ("run");
        byte relay = rand() % N_RELAYS;

        digitalWrite (RelayPins [relay], On);
        delay (200);
        digitalWrite (RelayPins [relay], Off);
    }
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);

    for (unsigned n = 0; n < N_RELAYS; n++)  {
        digitalWrite (RelayPins [n], Off);
        pinMode      (RelayPins [n], OUTPUT);
    }

    digitalWrite (BuzzerPin, Off);
    pinMode      (BuzzerPin, OUTPUT);

    pinMode      (ButPin, INPUT_PULLUP);
    butState = digitalRead (ButPin);

    beep ();
}

Thanks for all the help so far , getting some better switches and buttons and trying to get it all to work. will let you know how it goes.

Thank you again , you have been a real help.

I am not at the level i need to be to understand your timer code.
I got a few momentary push switches to try get my project working. all works great.

added a few small tweaks to your code to get the beeps and lights to help understand it all works great.
trying to now get the push button pin 12 to make it 2 minutes.

here is the code where all the timing and beeps work out .

#undef MyHW
#ifdef MyHW
//const byte ButPin       = A1;
//const byte BuzzerPin    = 13;

# define OneMinute  4000
# define TwoMinute  8000
#else
const byte RelayPins [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const byte ButPin       = 13; // green button
const byte ButPinTwo    = 12; // blue button // 
const byte BuzzerPin    = 11;
int greenLed = 15; // 1minute
int blueLed = 16; //2 minutes
# define OneMinute  60000
# define TwoMinute 120000
#endif

// -------------------------------------
#define N_RELAYS    sizeof(RelayPins)

enum { Off = HIGH, On = LOW };

int  run = 0;
byte butState;


unsigned long msecLst;

unsigned long period;

// -----------------------------------------------------------------------------
void
beep (void)
{
    digitalWrite (BuzzerPin,Off);
    delay (400);
    digitalWrite (BuzzerPin, On);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if (run && (msec - msecLst) > period)  {
        Serial.println ("timeout");
        digitalWrite (greenLed,LOW);  // light off when timeout
        
        
        beep();
        delay(50);
        beep();
        delay(50);
        beep();
        beep();
        delay(50);
        beep();
      run = false;
        
    }

    byte but = digitalRead (ButPin);
    if (butState != but)  {         // state change
        butState = but;

         if (LOW == but)  {          // pressed
            Serial.println ("GREEN BUTTON");
            period  = OneMinute;    // would depend on button
            msecLst = msec;
            digitalWrite(greenLed,HIGH);
            digitalWrite (blueLed,LOW);
            run     = ! run;        // toggle run on/off
             beep ();           //1 beep for start or stop
             
            
        }

      
        

        // turn all relays off
        if (! run)  {
            for (unsigned n = 0; n < N_RELAYS; n++)
                digitalWrite (RelayPins [n], Off);
          Serial.println ("STOP");
     beep();
     beep();
     beep();
       
           
      
        }
    }

    if (run)  {
        Serial.println ("run");
        byte relay = rand() % N_RELAYS;
delay(random(750, 5000));     //random times that the relays fire between 7/10 and 5 sec.
        digitalWrite (RelayPins [relay], On);
        delay(200);
        
        digitalWrite (RelayPins [relay], Off);
    }
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);

    for (unsigned n = 0; n < N_RELAYS; n++)  {
        digitalWrite (RelayPins [n], Off);
        pinMode      (RelayPins [n], OUTPUT);
    }

    digitalWrite (BuzzerPin, Off);
    pinMode  (greenLed ,OUTPUT);
    pinMode  (blueLed ,OUTPUT);
    pinMode      (BuzzerPin, OUTPUT);
    pinMode      (ButPinTwo, INPUT_PULLUP);
    pinMode      (ButPin, INPUT_PULLUP);
    butState = digitalRead (ButPin);
   // butstateTwo = digitalRead (ButPinTwo);


   beep ();         
   delay(100);
   beep();
   delay (100);
   beep ();
}

thanks got it right for two buttons.... could not have done it without your help..

#undef MyHW
#ifdef MyHW
//const byte ButPin       = A1;
//const byte BuzzerPin    = 13;

# define OneMinute  4000
# define TwoMinute  8000
#else
const byte RelayPins [] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const byte ButPin       = 13;
const byte ButPintwo    = 12;
const byte BuzzerPin    = 11;
int greenLed = 15; // 1minute
int blueLed = 16; //2 minutes
# define OneMinute  60000
# define TwoMinute 120000
#endif

// -------------------------------------
#define N_RELAYS    sizeof(RelayPins)

enum { Off = HIGH, On = LOW };

int  run = 0;
byte butState;
byte butStatetwo ;

unsigned long msecLst;

unsigned long period;
unsigned long msecLsttwo;

unsigned long periodtwo;

// -----------------------------------------------------------------------------
void
beep (void)
{
    digitalWrite (BuzzerPin,Off);
    delay (400);
    digitalWrite (BuzzerPin, On);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if (run && (msec - msecLst) > period)  {
        Serial.println ("timeout");
        digitalWrite (greenLed,LOW);
         digitalWrite (blueLed,LOW);
        
        beep();
        delay(50);
        beep();
        delay(50);
        beep();
        beep();
        delay(50);
        beep();
      run = false;
        
    }

    byte but = digitalRead (ButPin);
    byte buttwo = digitalRead(ButPintwo);
    
    
    if (butState != but)  {         // state change
        butState = but;

         if (LOW == but)  {          // pressed
            Serial.println ("GREEN BUTTON");
            period  = OneMinute;    // would depend on button
            msecLst = msec;
            digitalWrite(greenLed,HIGH);
            digitalWrite (blueLed,LOW);
            run     = ! run;        // toggle run on/off
             beep ();           //1 beep for start or stop
             
            
        }}
 if (butStatetwo != buttwo)  {         // state change
        butStatetwo = buttwo;

         if (LOW == buttwo)  {          // pressed
            Serial.println ("BLUE BUTTON");
            period  = TwoMinute;    // would depend on button
            msecLst = msec;
            digitalWrite(greenLed,LOW);
            digitalWrite (blueLed,HIGH);
            run     = ! run;        // toggle run on/off
             beep (); 
             delay(300);
             beep();      //2 beep for start or stop
 
      
         }}

        // turn all relays off
        if (! run)  {
            for (unsigned n = 0; n < N_RELAYS; n++)
                digitalWrite (RelayPins [n], Off);
          Serial.println ("NOT RUN");
    
       
           
      
        }


    if (run)  {
        Serial.println ("run");
        byte relay = rand() % N_RELAYS;
delay(random(750, 5000));
        digitalWrite (RelayPins [relay], On);
        delay(200);
        
        digitalWrite (RelayPins [relay], Off);
    }
    }

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);

    for (unsigned n = 0; n < N_RELAYS; n++)  {
        digitalWrite (RelayPins [n], Off);
        pinMode      (RelayPins [n], OUTPUT);
    }

    digitalWrite (BuzzerPin, Off);
    pinMode  (greenLed ,OUTPUT);
    pinMode  (blueLed ,OUTPUT);
    pinMode      (BuzzerPin, OUTPUT);
    pinMode      (ButPintwo, INPUT_PULLUP);
    pinMode      (ButPin, INPUT_PULLUP);
    butState = digitalRead (ButPin);
   // butstateTwo = digitalRead (ButPinTwo);


   beep ();         
   delay(100);
   beep();
   delay (100);
   beep ();
}
``````````````````

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