help with loop / if nested function

Hi all,

I skip the noob part and go straight to the point. I am trying to write a code that will open/close a few switches (connected to pins 1-4). This will be cycled by a set number of times (100 in this case). This part of the code is working fine, but when I try to nest it inside another function I get the “function-definition is not allowed here before ‘{’ token” error.

Here is the code

int reading = 0; // define pins for temp sensor
int sensorPin = A0;
int valvebub = 1; // define valves connected to digital pins
int valvepur = 2;
int valvehyd = 3;
int pwrplasm = 4;

void setup(){
pinMode(valvebub, OUTPUT); // sets the digital pins as output. Every pin outputs +5V
pinMode(valvepur, OUTPUT);
pinMode(valvehyd, OUTPUT);
pinMode(pwrplasm, OUTPUT);
}

void loop(){
delay(5000); //wait before starting the cycle
reading = analogRead(sensorPin);
int celsius = reading / 2;
if (celsius < 250) {
void loop () {
for (int i=0; i<=100; i++) {
digitalWrite(valvebub, HIGH); // sets the bubbler valve on
delay(500); // waits for 500 milliseconds
digitalWrite(valvebub, LOW); // sets the bubbler valve off
delay(1000); // waits for a second
digitalWrite(valvepur, HIGH); // sets the purge valve on
delay(3000); // waits for 3 seconds
digitalWrite(valvepur, LOW); // sets the purge valve off
delay(1000); // waits for a second
digitalWrite(valvehyd, HIGH); // sets the hydrogen valve on
delay(5000); // waits for 5 seconds
digitalWrite(pwrplasm, HIGH); // sets the plasma on
delay(3000); // waits for 3 seconds
digitalWrite(pwrplasm, LOW); // sets the plasma off
digitalWrite(valvehyd, LOW); // sets the hydrogen valve off
delay(1000); // waits for a second
digitalWrite(valvepur, HIGH); // sets the purge valve on
delay(5000); // waits for 5 seconds
digitalWrite(valvepur, LOW); // sets the purge valve off
delay (500);
}
}
}
else {
delay (500);
}
}

Ideally once the thermometer connected to A0 reads at least 250 C the loop will start, and repeat for the set number of times. Before that the code will just keep on reading the temp until the conditions are met ("else delay).

Without the second “void loop” the program just finishes one cycle and starts again from the reading = analogread, and so the “switching cycle” repeats but without the increment of “i” being registered. Therefore the program continues endlessly…

Is it possible for the program, once the if condition is satisfied, to repeat the for cycle without exiting/restarting from the top?

Thanks for your help :slight_smile:

By the way, when I try to compile I get:

control02.ino: In function ‘void loop()’:
control02:20: error: a function-definition is not allowed here before ‘{’ token
control02:47: error: expected }' at end of input control02:47: error: expected }’ at end of input

U cannot use another “void loop” inside the loop.

write your functions separate from eachother. let on call the other

int reading = 0;                  // define pins for temp sensor
int sensorPin = A0;
int valvebub = 1;                 // define valves connected to digital pins
int valvepur = 2;
int valvehyd = 3;
int pwrplasm = 4;

void setup(){
  pinMode(valvebub, OUTPUT);      // sets the digital pins as output. Every pin outputs +5V
  pinMode(valvepur, OUTPUT);
  pinMode(valvehyd, OUTPUT);
  pinMode(pwrplasm, OUTPUT);
}

void loop()
{
  delay(5000);                      //wait before starting the cycle
  reading = analogRead(sensorPin);
  int celsius = reading / 2;
  if (celsius < 250) {
    // void loop () {
    for (int i=0; i<=100; i++) {
      digitalWrite(valvebub, HIGH);     // sets the bubbler valve on
      delay(500);                       // waits for 500 milliseconds
      digitalWrite(valvebub, LOW);      // sets the bubbler valve off
      delay(1000);                      // waits for a second
      digitalWrite(valvepur, HIGH);     // sets the purge valve on
      delay(3000);                      // waits for 3 seconds
      digitalWrite(valvepur, LOW);      // sets the purge valve off
      delay(1000);                      // waits for a second
      digitalWrite(valvehyd, HIGH);     // sets the hydrogen valve on
      delay(5000);                      // waits for 5 seconds
      digitalWrite(pwrplasm, HIGH);     // sets the plasma on
      delay(3000);                      // waits for 3 seconds
      digitalWrite(pwrplasm, LOW);      // sets the plasma off
      digitalWrite(valvehyd, LOW);      // sets the hydrogen valve off
      delay(1000);                      // waits for a second
      digitalWrite(valvepur, HIGH);     // sets the purge valve on
      delay(5000);                      // waits for 5 seconds
      digitalWrite(valvepur, LOW);      // sets the purge valve off
      delay (500);
      //     }
    }
  }
  else {
    delay (500);
  }
}