How to start sketch when button pressed.

I'm new to programming and Arduino, but struggling along.

I'm attempting to fire a relay, or a bank of relays, for a set time period, after a button is pushed.

So.

power up Arduino...

Nothing happens.

Push button for 1/2 second,

relays for for pre-determined time,

then go back to nothing until button is pressed again.

What is happening, is I power up the Arduino, my relays power up, and stay powered up, until I hold the button. ONce I hold the button, it runs my program, and from then on it works as I intend, until I power down again.

Here is my code.

int ledPin = A5;
int switchPin = A0;
boolean lastButton = LOW;
boolean currentButton = LOW;
int switch2Pin = A1;
boolean last2Button = LOW;
boolean current2Button = LOW;

void setup ()
{
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    (delay(2));
  }
  return current;
}

boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(switch2Pin);
  if (last2 != current2)
  {
    (delay(2));
  }
  return current2;
}

void loop ()
{
  current2Button = debounce2(last2Button);
  if (last2Button == LOW && current2Button == HIGH)
  {
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(5000);
    digitalWrite(ledPin, HIGH);
  }
  
 
}

Delays are about the worst possible approach you can take, because the computer can do nothing else while delaying.

Study the "BlinkWithoutDelay" example that comes with the Arduino development software to see how to use the system timer (the millis() function) to accomplish what you want.

For a more advanced tutorial, see "how to do multiple things at once".

I only need the computer to do one thing though lol.

The milli function and tutorial in the link are above my head at the moment.

I’m attempting to accomplish what I want, which is do nothing until the button is pushed, then run the sketch, with an IF statement, but my code is not compiling.

int ledPin = A5;
int switchPin = A0;
boolean lastButton = LOW;
boolean currentButton = LOW;
int switch2Pin = A1;
boolean last2Button = LOW;
boolean current2Button = LOW;

void setup ()
{
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    (delay(5));
  }
  return current;
}

boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(switch2Pin);
  if (last2 != current2)
  {
    (delay(5));
  }
  return current2;
}

void loop ()
{
  current2Button = debounce2(last2Button);
  if(digitalRead(switchpin) == LOW) //funcitons based off of button pulling input pin LOW
  {
     if(run == 0)
     {
         run = 255;
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(10000);
    digitalWrite(ledPin, LOW);
  }
  
}

Compiler tells you the problems.

switchpin is not the same as switchPin.. C++ is case sensitive.

byte run; is missing

You're missing a } at the end.

CrossRoads:
Compiler tells you the problems.

switchpin is not the same as switchPin.. C++ is case sensitive.

byte run; is missing

You're missing a } at the end.

Thank you, I didn't have my screen setup properly to see that.

I have reverted to a simpler code shown below. It does not revert back to "OFF" after the timer, where am I going wrong here?

int ledPin = A5;
int switchPin = A0;
boolean lastButton = LOW;
boolean currentButton = LOW;
int switch2Pin = A1;
boolean last2Button = LOW;
boolean current2Button = LOW;

void setup ()
{
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(switch2Pin, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    (delay(5));
  }
  return current;
}

boolean debounce2(boolean last2)
{
  boolean current2 = digitalRead(switch2Pin);
  if (last2 != current2)
  {
    (delay(5));
  }
  return current2;
}

void loop ()
{
  current2Button = debounce2(last2Button);
  if (last2Button == LOW && current2Button == HIGH)
  {
    /*
     * Here is where the static flow is. currently set to 
     * 10 seconds of on time. This will need changed for 
     * different flow rate injectors so you dont overfill
     * your graduated cylinders.
     */
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(2000);
    digitalWrite(ledPin, LOW);
  }
  
 
  
  
  
}

This is a photo of how I have everything wired, except the resistor the trigger wire goes to is actually a 5 pin relay, I just couldn’t find a phto of that to use.