Input does one thing then stops

OK, I know that my topic name is strange but...

I am new to Arduino, understand a few bits in programming but not that much.

What I am trying to do is to have an input (pushbutton) to do a few thing JUST ONCE, for example, I have 4 buttons, 4 LEDs, so I need something like push button1 and the first LED blinks 4 times and stops - then the Arduino waits for me to do something else like pressing the button2 for LED2 or maybe pressing button3 for blinking LED1 and LED4 for 4 times and stops. I want to press one button do to several things but just once, not to repeat again and again...

As in example, if the code is set like that i just keeps on and on

const int buttonPin2 = 2;     
const int ledPin13 =  13;     
const int ledPin12 =  12;
const int buttonPin3 = 3;

int buttonState1 = 0;  
int buttonState2 = 0;

void setup() {
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop() {
  buttonState1 = digitalRead(buttonPin2);
  buttonState2 = digitalRead(buttonPin3);
    if (buttonState1 == HIGH) {
    digitalWrite(ledPin13, HIGH);
    //I want here to be something like this:
    delay(1000);
    digitalWrite(ledPin13, LOW);
    delay(1000);
    digitalWrite(ledPin13, HIGH);
    delay(1000);
    digitalWrite(ledPin13, LOW);
    //And to stop when this is done
}   else {
    digitalWrite(ledPin13, LOW);
    }  
    
    if (buttonState2 == HIGH) {
    digitalWrite(ledPin12, HIGH);
}   else {
    digitalWrite(ledPin12, LOW);
  }
}

You need a variable that will be the memory of what you have already done and use that to decide what you want to do next. here is an example

the variable nextSequenceNumber defines what I want to do as a sequence. I created a small function that blinks your LED a number of time for a variable period. and I cycle through 3 blink patterns with the switch case.

(untested, juste typed it in)

const int buttonPin2 = 2;
const int ledPin13 =  13;
const int ledPin12 =  12;
const int buttonPin3 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int nextSequenceNumber;


void setup() {
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  nextSequenceNumber = 0;
}

void sequenceflash(int flashCount, unsigned long blinkRate)
{
  for (int i = 0; i < flashCount; i++) {
    digitalWrite(ledPin13, HIGH);
    delay(blinkRate);
    digitalWrite(ledPin13, LOW);
    delay(blinkRate);
  }
}

void loop() {
  buttonState1 = digitalRead(buttonPin2);

  if (buttonState1 == HIGH) {
    switch (nextSequenceNumber) {
      case 0:
        sequenceflash(3, 1000);
        nextSequenceNumber = 1;
        break;
      case 1:
        sequenceflash(1, 500);
        nextSequenceNumber = 2;
        break;
      case 2:
        sequenceflash(10, 100);
        nextSequenceNumber = 0;
        break;
    }
  }

  buttonState2 = digitalRead(buttonPin3);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin12, HIGH);
  }   else {
    digitalWrite(ledPin12, LOW);
  }
}

OK, this changes something but not quite what I had in mind…I am making something that…let’s use relays…when I press button it needs to make one relay on and off a few times with delay of 500 ms in between and another relay to just go on and off, this has to be done just once. After that I press the second button and it does all this again but with different delays?

Do you mean only once "while" the button is pressed? In that case, you'll need to store the previous state so you can detect the moment the button is "just" pressed.

if (buttonState1 == HIGH && previousButtonState1 == LOW) {
//your code (condition is true just once per button press)
}

delay(1000) means that your Arduino does very little for 1 second. For example, you cannot use digitalRead during this time to read the state of pushbuttons.

Use millis() and restructure your program instead.

If you want to change what you are doing because a button was pushed, then the button should be debounced and you must keep track of the previous state and the current state so that you do something once when the button goes from unpressed to pressed.

when I press button it needs to make one relay on and off a few times with delay of 500 ms in between and another relay to just go on and off, this has to be done just once. After that I press the second button and it does all this again but with different delays?

Your spec is incomplete:

you press button1 and the relay thing happens (first sequence)
you press button2 and something else happens (second sequence)

what happens if button1 is still pressed at the end of the first sequence?
what happens if I press button1 again and not button2.
what happens if you have done both sequences? can this start again or it's finished?

J-M-L:
Your spec is incomplete:

you press button1 and the relay thing happens (first sequence)
you press button2 and something else happens (second sequence)

what happens if button1 is still pressed at the end of the first sequence?
what happens if I press button1 again and not button2.
what happens if you have done both sequences? can this start again or it's finished?

OK, maybe the best thing is to tell you what I am making...I want to make a central lock system for my car, this is something that could be done with after market one, I know, but I want to learn from you folks over this project and also want to try to make lock/unlock with bluetooth over a smartphone.

When I press LOCK:

  1. one relay is activated (to be as a switch for door LOCK)
  2. second relay for the hazard lights on for a second and then blinks 8 times
  3. third relay is activated to maybe close the windows or turn the lights on for some time (I have a different circuit for this, I just need +12v to activate the circuit)

When I press UNLOCK:

  1. one relay is activated (to be as a switch for door UNLOCK)
  2. second relay for the hazard lights on for a two seconds and then blinks 4 times
  3. third relay is activated to turn the lights on for some time

Maybe I could make a code/statement for sequence 1 and just to call it when ever I need it? Because on the remote there are 4 buttons, maybe LOCK; UNLOCK; TRUNK RELEASE; CAR FIND SIREN. Also I want to make when I press LOCK button for 3 seconds it should close windows, UNLOCK for 3 seconds to open windows. I know that there is a lot to be done, but I have in my mind what I want and I am not on some scheduled time to do this, when I have time I work on it, in the meanwhile I will learn about Arduino and programming microcontrollers. So two birds with one stone :slight_smile: