triggering relay at a certain time

Hello

I am a newbie with arduino

I am trying to get a push button to control about three relays(wich I figured out how to do) but what I can not figure out is how to get the relays to go at different times with just one push of the buttons.

for example
I push the button
relay1 turns on a light
2 seconds later relay2 turns on a light
5 seconds later relay3 turns on a light

Does anyone know how to do this?

Thanks!!

look for digitalWrite() and delay();

markbee

Post the code you've got so far, and we can suggest ways forward.

digitalRead the pin with the button on it
When you see it change, then
digitalWrite the pin that drives the transistor that turns on Relay1
simple approach is then delay(2000) (2000mS, or 2 seconds) and digitalWritet he pin that drives the transistor that turns on Relay2
then delay(5000) (2 seconds) and digitalWritet he pin that drives the transistor that turns on Relay3.

When do you want them turned off?

Welcome in the wonderful world of Arduino,

I am a newbie with arduino

Please spend some time at the tutorial section as there is so much info there. About the commands, how things can be done etc.

Thanks!

I guess i should have told you guys the bigger picture of what I am doing.

I need two push buttons to control six solenoids

first time you push button1 solenoid1 opens
second time solenoid 2 opens
third time solenoid 3 opens

and same thing for button two

I also need two noise makers to go off a couple seconds before and a couple of seconds after the all the solenoids opens
It would be very nice to be able to use the two push bottons at the same time.
and what i have read so far the delay functions does not allow that
Is there a way to make but a delay in somehow and be able to use both buttons?

I tried to change around a stopwatch code so that when you pushed the button it started counting the seconds and then at second 3 and 5 the noise makers went off and then at second 6 it stoped and reset.

but that failed because i did not know what i was doing.

here is the code i have so far

[code]
const int  buttonPin = 2;
const int  buttonPin2 = 3;
const int soleniod1Pin = 13;
const int soleniod2Pin = 12;
const int soleniod3Pin = 11;
const int soleniod4Pin = 10;
const int soleniod5Pin = 9;
const int soleniod6Pin = 8;
const int sound1Pin = 7;
const int sound2Pin = 6;      

int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0; 
int buttonPushCounter2 = 0;   
int buttonState2 = 0;         
int lastButtonState2 = 0; 

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(soleniod1Pin, OUTPUT);
  pinMode(soleniod2Pin, OUTPUT);
  pinMode(soleniod3Pin, OUTPUT);
  pinMode(soleniod4Pin, OUTPUT);
  pinMode(soleniod5Pin, OUTPUT);
  pinMode(soleniod6Pin, OUTPUT);
  pinMode(sound1Pin, OUTPUT);  
  pinMode(sound1Pin, OUTPUT);   
  Serial.begin(9600);
}


void loop() {
  
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState != lastButtonState) {  
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("button1:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      Serial.println("off"); 
    }
  }
  
  
 if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("button2 :  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
      Serial.println("off"); 
    }
  }
  lastButtonState = buttonState;
  lastButtonState2 = buttonState2;
  

   if (buttonPushCounter == 1) {
    digitalWrite(soleniod1Pin, HIGH);
   // need a delay with abilty to keep doing other stuff  
    digitalWrite(sound1Pin, HIGH);
   // need a delay with abilty to keep doing other stuff  
    digitalWrite(sound2Pin, HIGH);
  
  } else {
   digitalWrite(soleniod1Pin, LOW);
  }
   if (buttonPushCounter == 2) {
    digitalWrite(soleniod2Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound1Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound2Pin, HIGH);
    
  } else {
   digitalWrite(soleniod2Pin, LOW);
  }
   if (buttonPushCounter == 3) {
    digitalWrite(soleniod3Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound1Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound2Pin, HIGH);
    
  } else {
   digitalWrite(soleniod3Pin, LOW);
  }
   if (buttonPushCounter2 == 1) {
    digitalWrite(soleniod4Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound1Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound2Pin, HIGH);
    
  } else {
   digitalWrite(soleniod4Pin, LOW);
  }
   if (buttonPushCounter2 == 2) {
    digitalWrite(soleniod5Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound1Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound2Pin, HIGH);
    
  } else {
   digitalWrite(soleniod5Pin, LOW);
  }
   if (buttonPushCounter2 == 3) {
    digitalWrite(soleniod6Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound1Pin, HIGH);
    // need a delay with abilty to keep doing other stuff 
    digitalWrite(sound2Pin, HIGH);
    
  } else {
   digitalWrite(soleniod6Pin, LOW);
  }

   if (buttonPushCounter > 2)  {
    buttonPushCounter = 0;
  }
  
     if (buttonPushCounter2 > 2)  {
    buttonPushCounter2 = 0;
  }
  

 
}

[/code]

first time you push button1 solenoid1 opens

const int soleniod2Pin = 12;

Getting your spelling consistent will pay dividends in the future, when you stare at your code and can't see that elusive bug.
Learning about arrays will payback double.

"It would be very nice to be able to use the two push bottons at the same time.
and what i have read so far the delay functions does not allow that
Is there a way to make but a delay in somehow and be able to use both buttons?"

Yes, you can put your butto reading, writing, etc within a Blink without delay loop.
Basically you will be in a loop where you test if 1/10 of a second has gone by.
If it has, you will read your buttons, set flags high & low so you can keep track of which relays are open and for how many 1/10s they have been open, or closed, if PWM is running for your noise tone, etc.
Run it as fast as you want. Store the time an event started,
each pass thru the loop check if its been long enough to stop your action:
if (current_time - start_time ) >= event_duration ) {do end time action;}

All time variables must be type
unsigned long int
and the math format for calculating duration must be "later_time" minus "earlier_time"

Thanks!!!

I got it working!

and would you post the sketch?

markbee