Run loop as many times button is pressed

Hello! First post :slight_smile: I am completely lost with how I would go about doing this

I have a code that runs a few different solenoids in a sequence.

I'm trying to create a counter that will track how many times a button is pressed, and then another button that will run the loop as many times as the counter says. I'm not sure if that makes sense

There is probably a much quicker and simpler way to make this loop but I'm just learning and the amount of information out there is a little overwhelming lol thank you all for any help. Some examples or tutorials pointing me in the direction of something similar would be great

const int RELAY_1_C = 5;
const int RELAY_1_A = 2;
const int RELAY_1_B = 3;
const int RELAY_1_D = 4;
const int RELAY_1_E = 1;



void setup() {
   pinMode(RELAY_1_C, OUTPUT);
   pinMode(RELAY_1_A, OUTPUT);
   pinMode(RELAY_1_B, OUTPUT);
   pinMode(RELAY_1_D, OUTPUT);
   pinMode(RELAY_1_E, OUTPUT);
}

void loop() { 

  digitalWrite(RELAY_1_E, HIGH); //Relay On
  delay(300);
  digitalWrite(RELAY_1_C, HIGH); //Relay On
  delay(200);
  digitalWrite(RELAY_1_A, LOW); // Relay Off
  digitalWrite(RELAY_1_B, HIGH); //Relay On
  delay(400); 
  digitalWrite(RELAY_1_A, LOW); // Relay Off
  digitalWrite(RELAY_1_B, LOW); //Relay Off
  delay(150); 
  digitalWrite(RELAY_1_D, HIGH); //Relay On
  delay(105);
  digitalWrite(RELAY_1_D, LOW); //Relay Off
  delay(100);
  digitalWrite(RELAY_1_C, LOW); //Relay Off
  delay(100);
  digitalWrite(RELAY_1_E, LOW); //Relay Off
  delay(100);
  digitalWrite(RELAY_1_A, HIGH); // Relay On
  digitalWrite(RELAY_1_B, LOW); //Relay Off
  delay(250);

}

i am also new to arduino so take my advice with grain of salt.
i would make all the loop and make it in seperate function(), and than in my main code it will be something like: if(pushbutton==HIGH) than counter++; this will just count. and if(pushbutton2==HIGH) while(i<counter) function() i++; it can be written in the main loop as well withot the seperation.

You'll need something like:

int counter;
bool start;

loop() {
  if (digitalRead(COUNTERBUTTON, HIGH) counter++;

  if (digitalRead(STARTBUTTON, HIGH) start = true;

  if (counter>0 && start) {
    operateRelays();
    counter--;
  }
}

For you to do:

  • get rid of those delay()s.
  • make sure a button press is counted once and only once no matter how long it's pressed or if there's bounce.
  • maybe stop counting after the start button has been pushed.

No reason whatsoever to get rid of delay.

Start with two separate projects.
First make a counter. Push a button, a variable increases and prints to serial so you can see it in serial monitor.
This is simple, but not so simple that you can overlook it. You need to make the button only count new presses, and not count up as long as it's pressed, etc. You'll want to get very familiar with state change detection and debounce. You will need this information for every single button you program for, for every project you ever do.

After you know how to control button behavior, make a project where pushing a button runs your sequence once.

Then combine the two projects, using a for loop and the counting variable. Make sure to reset the counting variable at the right time.