Help on programming with sequences - 4 buttons on input and one output relay

Hello,

I kindly ask your help on my program, as it doesn’t work how it should:

I used 4 buttons on 4 digital inputs and one relay on output.
I have 4 sequences to trigger, each sequence triggered by one button.
The sequence must be triggered only when one button of four is pressed.
1: button1 = 15 pulses of 4000 ms each with 4000 ms pause after each pulse.
2: button2 = 5 pulses of 16000 ms with 10000 ms pause after each pulse.
3: button3 = 3 pulses of 32000 ms with 12000 ms pause after each pulse.
4: button4 = one pulse of 120000 ms.

I attached the code if someone is interessed to help.

Thank you in advance.

alarms_sequences.txt (2.93 KB)

Inline and formated properly. Please do that yourself next time. See How to use the forum.

/*I used 4 buttons on 4 digital inputs and one relay on output.
  I have 4 sequences to trigger, each sequence triggered by one button
  The sequence must be triggered only when one button of four is pressed
  1: button1 = 15 pulses of 4000 ms each with 4000 ms pause after each pulse
  2: button2 = 5 pulses of 16000 ms with 10000 ms pause after each pulse
  3: button3 = 3 pulses of 32000 ms with 12000 ms pause after each pulse
  4: button4 = one pulse of 120000 ms

*/
int RelayPin = 12; // set output on pin 12
int buttonPin1 = 7; // set button 1 on pin 7
int buttonPin2 = 6; // button 2 on pin 6
int buttonPin3 = 5; // button 3 on pin 5
int buttonPin4 = 4; // button 4 on pin 4
int statPin1 = 0; // read state on button 1
int statPin2 = 0; // read state on button 2
int statPin3 = 0; // read state on button 3
int statPin4 = 0; // read state on button 4
int count1 = 0;
int count2 = 0;
int count3 = 0;
int timer1 = 4000;
int timer2 = 16000;
int timer3 = 10000;
int timer4 = 32000;
int timer5 = 12000;
int timer6 = 120000;
void setup() {
  // put your setup code here, to run once:
  pinMode(RelayPin, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  digitalWrite(RelayPin, LOW);
  //start codes
  for (int count1 = 0; count1 < 16; count1++) { // 15 pulses of each 4 seconds with pause 4 seconds between pulses
    int statPin1 = digitalRead(buttonPin1); // status button 1
    if (statPin1 == HIGH) {
      digitalWrite(RelayPin, LOW);
    }
    if (statPin1 == LOW) {
      digitalWrite(RelayPin, HIGH); // aeriana 1
      delay(timer1);
      digitalWrite(RelayPin, LOW);
      delay(timer1);
    }
  }
  for (int count2 = 0; count2 < 6; count2++) { //5 pulses
    int statPin2 = digitalRead(buttonPin2); // status button 2
    if (statPin2 == HIGH) {
      digitalWrite(RelayPin, LOW); //output relay off
    }
    if (statPin2 == LOW) {
      digitalWrite(RelayPin, HIGH);
      delay(timer2);
      digitalWrite(RelayPin, LOW); // sound 2
      delay(timer3);
    }
  }
  for (int count3 = 0; count3 < 4; count3++) { //3 pulses
    int statPin3 = digitalRead(buttonPin3); // status button 3
    if (statPin3 == HIGH) {
      digitalWrite(RelayPin, LOW); // relay off
    }
    if (statPin3 = LOW) {
      digitalWrite(RelayPin, HIGH); //sound 1
      delay(timer4);
      digitalWrite(RelayPin, LOW);
      delay(timer5);
    }
  }
  int statPin4 = digitalRead(buttonPin4); // status button 4
  if (statPin4 == HIGH) {
    digitalWrite(RelayPin, LOW); // end pre aeriana
  }
  if (statPin4 == LOW) {
    digitalWrite(RelayPin, HIGH); //sound stop alarm
    delay(timer6);
    digitalWrite(RelayPin, LOW); // end sound stop alarm
  }
  // end codes
}


void loop() {
  if (statPin1 == HIGH && statPin2 == HIGH && statPin3 == HIGH && statPin4 == HIGH) {
    digitalWrite(RelayPin, LOW);
  }
}

And there is an important piece missing in you post. What is actually happening? “Does not work” isn’t an answer :wink:

Alright, scanned though the code, couple or remarks / questions.

Why is everything in setup()?

Why are you programming backwards? You first try to do the sequence and while doing that you constantly read the button. Don't you want to read the button and based on that start (or not start) a sequence?

What should happen is a sequence is running and a button is pressed? Remember, the Arduino can only do nothing (but wait) while in a delay().

Thank You for your answer, I'll take care on the next post with formatting.

At first question: I put all the code in setup() because I thought that I need to run only once the sequence.
Second, I think that is another mistake of mine, I tried to use external pull up resistors, then pull down, then internal and I messed up.
While a sequence is triggered and another button is pressed, that button should be ignored.
What results I got until now: the relay module starts all the sequences, one after another, even if no button is pressed.

ryuken:
Thank You for your answer, I'll take care on the next post with formatting.

Thank you!

ryuken:
At first question: I put all the code in setup() because I thought that I need to run only once the sequence.

But you want to constantly check the button don't you? So all the sequencing should go in loop(). Only the setup of the pins (what's in de name) should go in setup()

ryuken:
Second, I think that is another mistake of mine, I tried to use external pull up resistors, then pull down, then internal and I messed up.

Using the internal pull ups is great! Only you need to act on a button press. Not start a sequence and then check to see if the button is pressed. that's just backwards :wink:

ryuken:
While a sequence is triggered and another button is pressed, that button should be ignored.

Alright, easiest form :slight_smile:

ryuken:
What results I got until now: the relay module starts all the sequences, one after another, even if no button is pressed.

Yep, because you do stuff without checking the button :wink: Start by correcting that. And a small tip, just go back to one button. And I would just start over instead of editing. I think the current code will work confusing instead of helping you. So start over and try to do it for one button. So check the button (in loop) and only do a sequence when the button is pressed.

And a final question, are you planning to do more with the Arduino after you finish this sequence thing?

Thanks a lot for your answers.

I am doing this from beginning, from one button and I take count of your advices.

Of course I am planning to do more with Arduino, it's a great platform and I like it! I have many projects on the "to do" list, but with baby steps.

Wanting to do more with Arduino and baby steps are both very very good. :smiley: But what I meant was, after you make this sequence, do you want THIS Arduino to do more besides the sequence?

septillion:
Wanting to do more with Arduino and baby steps are both very very good. :smiley: But what I meant was, after you make this sequence, do you want THIS Arduino to do more besides the sequence?

This board will stay at least 5 years from now on with this code if not more...

ryuken:
... if not more...

And that is what I'm after :wink: Do you want this Arduino, while it is used to sequence lights, to do anything besides that? For example sequencing lights and playing a sound or sequencing lights and moving a servo?

septillion:
And that is what I'm after :wink: Do you want this Arduino, while it is used to sequence lights, to do anything besides that? For example sequencing lights and playing a sound or sequencing lights and moving a servo?

Well, I think I will use a 16x2 LCD in 4 bit mode or a Nokia 5110 compatible LCD to show on the display the name of the current sequence and also a NTC sensor on analog INPUT to read temperatures from the room

Alright, so you want to do more then just a sequence. Then delay() is going to give you trouble :wink: A delay() does exactly that, it delays. And in that time NOTHING can happen. But, that we can work on after a working sketch with one button and delay()'s :wink: