2 void loops

Hi

I'm trying to write a sketch to run a green light until it is triggered by a button to then flash a red light.
i also want an air solenoid to trigger every 5 mins for 10seconds without up setting the led and button function but am not sure how to add it into the code.

I'm sure its an easy fix for someone who knows what they are doing so any help would be appreciated.

also i apologize if this is not the correct way to post the code

// Pin 13 onboard red LED 
// Pin 9 onboard green LED
// Pin 2 button/switch.
// Pin 4 Air solenoid
int red = 13;
int green = 9;
int button = 2;
int Air = 4;
////////////////////////////////////////////////////
void setup() {
  // initialize the digital pin as an output.
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(Air, OUTPUT);
}

void loop() 
{
  if(digitalRead(button)==HIGH){
    digitalWrite(green, LOW);   
    delay(10);               
    digitalWrite(red, HIGH);    
                             
    delay(10);               
  }
  
  else {
    digitalWrite(red, LOW);  
    digitalWrite(green, HIGH);  
    delay(200);              
    digitalWrite(red, HIGH);     
    delay(100);
    
    digitalWrite(red, LOW);  
    digitalWrite(green, HIGH);  
    delay(200);              
    digitalWrite(red, HIGH);     
    delay(100);
    
    digitalWrite(red, LOW);  
    digitalWrite(green, HIGH);  
    delay(200);              
    digitalWrite(red, HIGH);     
    delay(100);
    
   
  
  }
}

Simply, you can't. A (single core) processor can only do one thing at the time even it that one this is wait and do nothing (delay()). So, don't use delay() to do the timing, switch to millis(). Have a look at Blink without delay and Several thing at the same time.

The way to write loop () is as a series of tests, one for each thing you need to check and potentially act on:

void loop ()
{
  if (condition1)
    action1 () ;
  if (condition2)
    action2 () ;
  if (condition3)
    action3 () ;
}

Then everything gets a chance - but you mustn't spend too long in any of the action functions, which
means using millis() rather than delay(), and managing a state-machine for each bit of functionality with
more than one state.

Your thread title reveals some gaps in your understanding of the C language and the Arduino environment in particular. Best to get this straight before going too far.

You do not have anything in your program called “void loop”. What you have is a function named loop preceded by a data type indicating what sort of data the function will return when called, if any. In the case of the loop() function in the Arduino environment it does not return a value so its return type is void. Incidentally, the empty brackets after the function name indicate that it does not take any parameters either. In general, function names must be unique within a program, but there are even exceptions to that in some circumstances.

One of the confusing things about the environment is that both the setup() and loop() functions, whilst generally mandatory (there are exceptions but you are not ready for that yet, if ever) appear not to be called from anywhere. In fact they are called from a program that you do not see which the IDE inserts into your program when you compile it. The setup() function is called once at startup or reset and the loop() function is called repeatedly.

You can, of course, write your own functions and doing so may help you understand the terminology and what happens when you call a function.

An example function that multiplies 2 integers together and returns the result

void setup()
{
  Serial.begin(115200);
  Serial.println(mult2(12, 45));
}

void loop()
{
}

int mult2(int number1, int number2)
{
  int result = number1 * number2;
  return result;
}

This program calls the mult2() function and passes 2 numbers to it. In the function the numbers are assigned to variables, multiplied together and returned to the calling program. Note the return type of the function is int. You would refer to this function as the mult2() function not “int mult2”. In the example above the return value is printed although it could be used by the program.

I suggest that you do some further reading on C as if you understand what is going on it will help and will also allow you to ask questions in a meaningful way.

Try this mod, I trust you know how to connect a solenoid to Arduino without blowing the output pin, if not absolutely sure, STOP and ask here. :slight_smile:

// Pin 13 onboard red LED
// Pin 9 onboard green LED
// Pin 2 button/switch.
// Pin 4 Air solenoid
unsigned long airStart, flashStart; // timers
const unsigned long airInterval = 300000, // 5 minutes
                    flashInterval = 300;
const int airEnd = 10000, // 10 seconds
          flashEnd = 100; // red ON time
bool oldButton = true;
byte counter; // flash counter
int red = 13;
int green = 9;
int button = 2;
int Air = 4;
////////////////////////////////////////////////////
void setup() {
  // initialize the digital pin as an output.
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(Air, OUTPUT);
}

void loop()
{
  digitalWrite(Air,millis() - airStart < airEnd);
  if(millis() - airStart > airInterval)
    airStart += airInterval; // reset Air timer
    
  digitalWrite(green,oldButton);
    
  if(digitalRead(button) == LOW && oldButton == true){
    oldButton = false;
    flashStart = millis();
  }
  if(oldButton == false){
    digitalWrite(red,millis() - flashStart < flashEnd);
    if(millis() - flashStart > flashInterval){
      flashStart += flashInterval; // reset flash timer
      if(++counter >= 3 ){ // number of flashes 
        counter = 0; // reset flash counter
        oldButton = true;
      }  
    }  
  }

Rather than think in terms of two loops have a look at the demo Several Things at a Time

And there is a more extensive example program in Planning and Implementing a Program

...R