2chanelRelay 3buttons Timer and reset PROBLEM

Hi,
I have 2Chanell-Relay and 3 buttons, I manage to make this… When I press btn1 chanell1 is ON with timer like 10min. When the timer is done (after 10min) the Chanell2 is OFF…

Same with button2, when I press btn2, chanell2 is ON with timer of 10min, When the timer is done (after 10min) the Chanell2 is OFF

I want to make btn3 to be like when I press it during those 10min. channel1 and chanel2 to be OFF, as well as the timer…

Untill now I manege to do this: wen I press button3 Chanels are OFF but until the timer is done (I’m waiting for 10min) I cant start ON any chanels…I have to wait 10 minits so I can turn any Chanel ON

I need thiis btn3 to turn OFF the timmer as well so I can turn ON chanels again whithout waiting
Here is my Code:

const int button1 = 8; // the number of the pushbutton pin
const int button2 = 9;
const int button3 = 10;

const int CHANEL1 = 2; //Chanels of the Relay
const int CHANEL2 = 3;

unsigned long off_time;

boolean CHANEL1State=false;
boolean CHANEL2State=false;

int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button3State = 0;

void setup() {
// initialize the CHANEL pin as an output:
pinMode(CHANEL1, OUTPUT);
pinMode(CHANEL2, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(button1, INPUT);
pinMode(button2, INPUT);
//// initialize the state of the relay chanels (OFF):
digitalWrite(CHANEL1, HIGH);
digitalWrite(CHANEL2, HIGH);
resetChanells();
}

void loop(){
button3State = digitalRead(button3);

///////////// CHANEL1 BUTTON 1 ////////
if ((CHANEL1State) && (millis()>=off_time)) /* is it on and is it later or equal to off_time /
{
digitalWrite(CHANEL1,HIGH);
CHANEL1State = false;
}
else if (!CHANEL1State) /
is it off? /
{
button1State = digitalRead(button1);
if(button1State == HIGH)
{
digitalWrite(CHANEL1, LOW);
CHANEL1State = true;
off_time = millis() + 5000;
}
}
///////////// CHANEL2 BUTTON 2 ////////
if ((CHANEL2State) && (millis()>=off_time)) /
is it on and is it later or equal to off_time /
{
digitalWrite(CHANEL2,HIGH);
CHANEL2State = false;
}
else if (!CHANEL2State) /
is it off? */
{
button2State = digitalRead(button2);
if(button2State == HIGH)
{
digitalWrite(CHANEL2, LOW);
CHANEL2State = true;
off_time = millis() + 5000;
}
}

///////////////////// BUTTON 3 TURN OFF CHANELS //////
if (button3State == HIGH) {
resetChanells();
}

}

void resetChanells()
{

digitalWrite(CHANEL1, HIGH);
digitalWrite(CHANEL2, HIGH);

}

Please add code tags to your code to be more readable.

Note: Since there is only one "startTime" for the two timers the timer gets reset if the second button is pushed during the first relay's interval. If you push button2 and five minutes later push button1 then relay2 will run 15 minutes before both relays turn off. Use a separate startTime for the two timers if the relays should turn off ten minutes after their button was first pressed.

const int button1Pin = 8;  // the number of the pushbutton pin
const int button2Pin = 9;
const int button3Pin = 10;

// Relay pins (Active LOW)
const int CHANEL1Pin = 2;
const int CHANEL2Pin = 3;

boolean CHANEL1State = false;  // OFF
boolean CHANEL2State = false;  // OFF

const unsigned long onInterval = 10UL * 60UL * 1000UL; // 10 minutes
unsigned long startTime;

void setup() {
  // initialize the CHANEL pin as an output:
  digitalWrite(CHANEL1Pin, HIGH);  // OFF (active low)
  pinMode(CHANEL1Pin, OUTPUT);
  digitalWrite(CHANEL2Pin, HIGH);  // OFF (active low)
  pinMode(CHANEL2Pin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
}

void loop() {

  ///////////// CHANEL1 BUTTON 1 ////////
  if (CHANEL1State) {
    // Relay is ON
    if (millis() - startTime >= onInterval) {
      // Time to turn the relay off
      digitalWrite(CHANEL1Pin, HIGH);  // OFF
      CHANEL1State = false;
    }
  } else {
    // Relay 1 is OFF
    if (digitalRead(button1Pin)) {
      // Button 1 pressed, turn Relay 1 on
      digitalWrite(CHANEL1Pin, LOW);
      CHANEL1State = true;
      startTime = millis();
    }
  }

  ///////////// CHANEL2 BUTTON 2 ////////
  if (CHANEL2State) {
    // Relay is ON
    if (millis() - startTime >= onInterval) {
      // Time to turn the relay off
      digitalWrite(CHANEL2Pin, HIGH);  // OFF
      CHANEL2State = false;
    }
  } else {
    // Relay 2 is OFF
    if (digitalRead(button2Pin)) {
      // Button 2 pressed, turn Relay 2 on
      digitalWrite(CHANEL2Pin, LOW);  // ON
      CHANEL2State = true;
      startTime = millis();
    }
  }


  ///////////////////// BUTTON 3 TURN OFF BOTH CHANELS //////
  if (digitalRead(button3Pin)) {
    // Time to turn the relays off
    digitalWrite(CHANEL1Pin, HIGH);  // OFF
    CHANEL1State = false;
    digitalWrite(CHANEL2Pin, HIGH);  // OFF
    CHANEL2State = false;
  }
}

johnwasser: Note: Since there is only one "startTime" for the two timers the timer gets reset if the second button is pushed during the first relay's interval. If you push button2 and five minutes later push button1 then relay2 will run 15 minutes before both relays turn off. Use a separate startTime for the two timers if the relays should turn off ten minutes after their button was first pressed.

const int button1Pin = 8;  // the number of the pushbutton pin
const int button2Pin = 9;
const int button3Pin = 10;

// Relay pins (Active LOW) const int CHANEL1Pin = 2; const int CHANEL2Pin = 3;

boolean CHANEL1State = false;  // OFF boolean CHANEL2State = false;  // OFF

const unsigned long onInterval = 10UL * 60UL * 1000UL; // 10 minutes unsigned long startTime;

void setup() {   // initialize the CHANEL pin as an output:   digitalWrite(CHANEL1Pin, HIGH);  // OFF (active low)   pinMode(CHANEL1Pin, OUTPUT);   digitalWrite(CHANEL2Pin, HIGH);  // OFF (active low)   pinMode(CHANEL2Pin, OUTPUT);

  // initialize the pushbutton pin as an input:   pinMode(button1Pin, INPUT);   pinMode(button2Pin, INPUT); }

void loop() {

  ///////////// CHANEL1 BUTTON 1 ////////   if (CHANEL1State) {     // Relay is ON     if (millis() - startTime >= onInterval) {       // Time to turn the relay off       digitalWrite(CHANEL1Pin, HIGH);  // OFF       CHANEL1State = false;     }   } else {     // Relay 1 is OFF     if (digitalRead(button1Pin)) {       // Button 1 pressed, turn Relay 1 on       digitalWrite(CHANEL1Pin, LOW);       CHANEL1State = true;       startTime = millis();     }   }

  ///////////// CHANEL2 BUTTON 2 ////////   if (CHANEL2State) {     // Relay is ON     if (millis() - startTime >= onInterval) {       // Time to turn the relay off       digitalWrite(CHANEL2Pin, HIGH);  // OFF       CHANEL2State = false;     }   } else {     // Relay 2 is OFF     if (digitalRead(button2Pin)) {       // Button 2 pressed, turn Relay 2 on       digitalWrite(CHANEL2Pin, LOW);  // ON       CHANEL2State = true;       startTime = millis();     }   }

  ///////////////////// BUTTON 3 TURN OFF BOTH CHANELS //////   if (digitalRead(button3Pin)) {     // Time to turn the relays off     digitalWrite(CHANEL1Pin, HIGH);  // OFF     CHANEL1State = false;     digitalWrite(CHANEL2Pin, HIGH);  // OFF     CHANEL2State = false;   } }

Thank you johnwasser You saved me :)

tanjamaya: Thank you johnwasser You saved me :)

I'm a Software Engineer. That's what we do. :)