Button control cycle

Hello,

Pretty new to Arduino. I'm trying to control relays and LEDs with a button. I am able to turn the relays on/off with a button using this code:

/*

*/

const int buttonPin = 3;
const int relay = 13;
const int relayMist = 12;
const int Greenled = 1;
const int Blueled = 2;

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {

pinMode(buttonPin, INPUT);
pinMode(Greenled, OUTPUT);
pinMode(Blueled, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(relayMist, OUTPUT);
Serial.begin(9600);
}

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {

buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {

Serial.println("off");
}
}

lastButtonState = buttonState;

if (buttonPushCounter % 2 == 0) {
digitalWrite(Greenled, LOW);
digitalWrite(relay, HIGH);
digitalWrite(relayMist, HIGH);
digitalWrite(Blueled, HIGH);

} else {
digitalWrite(Greenled, HIGH);
digitalWrite(relay, LOW);
digitalWrite(relayMist, LOW);
digitalWrite(Blueled, LOW);

}

What I want to do is have the relays turn on after the button is pressed then turn off after 15 seconds. Anyone know how I can do this? Any help would be great!

Anyone know how I can do this?

After the switch is pressed? Or after it is released?

Why are you counting presses?

Turning a pin on and off after a delay() is trivial. Turning it on, and then using the blink without delay philosophy (check whether it is time to do something) to turn it off is only slightly (very slightly) more challenging.

Thanks for the response. I have the button counter because it was the first way I was able to get the relays to turn on and off by the button. Ideally, I would like to push the button, release it, then have the cycle start and end after 15 seconds.

I think I need to change the code to something more like when button input goes from low to high to low, then:

digitalWrite(Greenled, HIGH);
digitalWrite(relay, LOW);
digitalWrite(relayMist, LOW);
digitalWrite(Blueled, LOW);
delay(15000)
digitalWrite(Greenled, LOW);
digitalWrite(relay, HIGH);
digitalWrite(relayMist, HIGH);
digitalWrite(Blueled, HIGH);

I'm unsure how to write the first part. Any samples of code similar to this would be great!

You must follow this tutorial for the button control:

I wrote this code, try it:

const int  buttonPin = 3;  
const int relay = 13; 
const int relayMist = 12;
const int Greenled = 1;
const int Blueled = 2;
  
int buttonState;             
int lastButtonState = LOW;   
unsigned long lastDebounceTime = 0;  
long debounceDelay = 50;  


void setup() {                

  pinMode(buttonPin, INPUT);
  pinMode(Greenled, OUTPUT); 
  pinMode(Blueled, OUTPUT); 
  pinMode(relay, OUTPUT); 
  pinMode(relayMist, OUTPUT); 
  Serial.begin(9600);
  digitalWrite(Greenled, LOW);   
  digitalWrite(relay, HIGH);
  digitalWrite(relayMist, HIGH);
  digitalWrite(Blueled, HIGH);
}

 void loop() {
   int reading = digitalRead(buttonPin); 
   if (reading != lastButtonState) 
    lastDebounceTime = millis();
    if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        while (digitalRead(buttonPin) == HIGH) {}
        Blinking();
      }
    }
  }
  lastButtonState = reading;
 }
  
void Blinking() {
  digitalWrite(Greenled, HIGH);   
  digitalWrite(relay, LOW); 
  digitalWrite(relayMist, LOW);  
  digitalWrite(Blueled, LOW);
  delay(15000);
  digitalWrite(Greenled, LOW);   
  digitalWrite(relay, HIGH);
  digitalWrite(relayMist, HIGH);
  digitalWrite(Blueled, HIGH);
}

Wow thank you! This is exactly what I needed. I really appreciate it!

You're welcome :wink: but you must study the tutorial of the debounce! It's important for the buttons control!

I definitely will. I was looking into that earlier. Thanks again!