how to return to the start of a loop whilst midway through

hi there i have a code that has a series of inputs to confirm objects are working if it comes back low it shuts down certain things and brings up a lock out LED and then waits for the reset button to be pressed to continue. the problem is it just carries on from where it left off obviously but i need it to go back to the start of the loop or designated point?

It really is much easier for us to help, if you post your code.

Not sure what you are trying to do, but to answer your question, the continue statement will skip the rest of a loop block and resume the loop at the next iteration.

-j

Depending on the place in the code continue will work. NB Continue will jump to the begin of the innermost loop it is in, that can be the begin of loop but not necessary.

The easiest way to jump to begin of loop() is the return; statement. It will leave loop() no matter where you place the command. As loop() is called (invisibly) in a while construct it will start over again.

The invisible part looks like (IDE 0.22) - C:\Program Files (x86)\arduino-0022\hardware\arduino\cores\arduino - (windows)

#include <WProgram.h>

int main(void)
{
	init();
	setup();
	for (;;)
		loop(); 
	return 0;
}

Note that you could change this file to behave differently...

hi thanks for your replies
here is a copy of my sketch not complete as of yet you can see where i need it to return to the start

/* Priva Gas / Oil control
   04/01/2012
*/
const int heatbutton = 2;
const int ventbutton = 3;
const int ignition = 4;
const int saleswitch = 5;
const int solenoid = 6;
const int photo_pressure = 7;
const int lockoutled = 8;
const int resetbutton = 9;
const int fan = 10;
const int map1_or2 = 11;

void setup()
{
  pinMode(heatbutton, INPUT);    
  pinMode(ventbutton, INPUT);
  pinMode(ignition, OUTPUT);
  pinMode(saleswitch, INPUT);
  pinMode(photo_pressure, INPUT);
  pinMode(lockoutled, OUTPUT);
  pinMode(resetbutton, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(map1_or2, INPUT);
  digitalWrite(heatbutton,HIGH); // turn on internal pull-up 
  digitalWrite(ventbutton,HIGH);
  digitalWrite(saleswitch,HIGH);
  digitalWrite(photo_pressure,HIGH);
  digitalWrite(map1_or2,HIGH);
  }
  
  void loop(){                              
  int val = digitalRead(ventbutton);
      if (val == HIGH) {                        // vent button
        digitalWrite(fan, HIGH); }
    digitalRead(map1_or2);            //    GAZ CONTROL
    if (val == LOW) {
      
      int val = digitalRead(heatbutton);      
      if (val == HIGH) {                          // heatbutton
      int val = digitalRead(photo_pressure);      
      if (val == HIGH) {                        
        digitalWrite(fan, HIGH); }
        delay(2000);
         digitalRead(saleswitch);      
      if (val == HIGH) {
        digitalWrite(solenoid, HIGH); } 
    else  {
    digitalWrite(lockoutled, HIGH);
    digitalWrite(solenoid, LOW);
    digitalWrite(fan, LOW);
  digitalRead(resetbutton);      
      if (val == HIGH) {
         digitalWrite(lockoutled, LOW); }
     }                                            // at this point it needs to go back to the start????
        delay(3000);
        digitalRead(photo_pressure);      
      if (val == LOW) { 
         digitalWrite(lockoutled, HIGH);
         digitalWrite(solenoid, LOW);
         digitalWrite(fan, LOW); }
         digitalRead(resetbutton);      
      if (val == HIGH) {
         digitalWrite(lockoutled, LOW); }
         
      }
    }
  }

what do we think?

Personally, I think doing digitalReads without assigning the result to a variable is not going to be much use, and that it's not necessary to create a new copy of val every time.

If it needs to go back to the start at the point shown, why is there code after it inside the if block? Everything from the delay(3000) to the end of the block shown here should either be deleted altogether, or moved out of the if block and possibly placed in an else block.

    if (val == HIGH)
    {                          // heatbutton
 // code remove for brevity
      }                                            // at this point it needs to go back to the start????
      delay(3000);
      digitalRead(photo_pressure);      
      if (val == LOW) 
      { 
        digitalWrite(lockoutled, HIGH);
        digitalWrite(solenoid, LOW);
        digitalWrite(fan, LOW); 
      }
      digitalRead(resetbutton);      
      if (val == HIGH) 
      {
        digitalWrite(lockoutled, LOW); 
      }

    }

Those multiple vals are indeed rather confusing. Consider that instead of this:

  int val = digitalRead(heatbutton);      
    if (val == HIGH)

You can do this:

    if (digitalRead(heatbutton)==HIGH)

And get rid of the vals altogether (maybe - it's hard to tell).

Can you explain what you're trying to do, particularly what you mean by go back to start. Your earlier post mentions waiting for a reset button too. Care to elaborate?

what do we think?

some coding style advice

use #define for PINNUMBERS as they take no memory and are const by definition.

#define HEATBUTTON 2
#define VENTBUTTON 3
#define IGNITION 4
etc

your indentation and use of variables is confusing, from the style I cannot see to which IF ssome ELSE belongs. By giving them the same indentation this is easier to see and yes, good indentation leads to more readable code and therefor lesser bugs.

statements like

digitalRead(saleswitch);
digitalRead(resetbutton);

does not assign a value to anything so they do nothing (except using some CPU cycles)..

The key combination CTRL-T in the IDE does restyle the code (to some extend)

How I would write your program - far from complete but just to get another styling idea ..

void loop()
{
  // DO MEASUREMENTS
  vent = digitalRead(VENTBUTTON);
  map = digitalRead(MAPBUTTON);
  heat = digitalRrad(HEATBUTTON);
  photo = digitalRead(PHOTOBUTTON);
  sales = digitalRead(SALESBUTTON);
  reset = digitalRead(RESETBUTTON);
  //etc

  // HANDLE FAN
  bool fanValue = (vent == HIGH || (heat == HIGH && photo == HIGH) );
  digitalWrite(fan, fanValue);
  
  // HANDLE SOLENOID ACTION
  bool solenoidValue = (sales == HIGH);
  digitalWrite(solenoid, solenoidValue);

   // HANDLE LOCKOUTLED
   bool lockoutValue = (sales == LOW);
   digitalWrite(lockoutled, lockoutValue);

   // etc...
}

Hope this helps

hi thanks for all your help

i have altered my digitalRead

i cant get rid of the last bit of the code as i dont think i will be able to get round not needing to go back to the start mid way through

and the code

void loop()
{
  // DO MEASUREMENTS
  vent = digitalRead(VENTBUTTON);
  map = digitalRead(MAPBUTTON);
  heat = digitalRrad(HEATBUTTON);
  photo = digitalRead(PHOTOBUTTON);
  sales = digitalRead(SALESBUTTON);
  reset = digitalRead(RESETBUTTON);
  //etc

  // HANDLE FAN
  bool fanValue = (vent == HIGH || (heat == HIGH && photo == HIGH) );
  digitalWrite(fan, fanValue);
  
  // HANDLE SOLENOID ACTION
  bool solenoidValue = (sales == HIGH);
  digitalWrite(solenoid, solenoidValue);

   // HANDLE LOCKOUTLED
   bool lockoutValue = (sales == LOW);
   digitalWrite(lockoutled, lockoutValue);

   // etc...
}

i would do it like this but i do find it confusing to understand

i cant get rid of the last bit of the code as i dont think i will be able to get round not needing to go back to the start mid way through

I can't see why you would need to. Suppose you are at an art museum. There is one entrance and one exit. The only way to get to the exit is to look at every picture on the way, if you are not wearing sunglasses. At any point, if you get tired of looking, you simply need to put your sunglasses on and head straight for the end. At each side door along the way, note the sign that says if(!wearingSunglasses) turnLeft(). Since you are, you needn't turn left.

In the same way, you make every block of code conditional, and you can bypass large chunks of code when the condition is not true.

Joes:
hi there i have a code that has a series of inputs to confirm objects are working if it comes back low it shuts down certain things and brings up a lock out LED and then waits for the reset button to be pressed to continue. the problem is it just carries on from where it left off obviously but i need it to go back to the start of the loop or designated point?

In answer to the question, does the operator 'continue;' not do what you asked...returns to the start of the loop?

In answer to the question, does the operator 'continue;' not do what you asked...returns to the start of the loop?

continue will jump back to the top of a loop. But, that has nothing to do with jumping to the start of loop().

The loop() function is a function. It can be ended at any point with a return statement. Then, main() will call loop() again.

ah yes, RTFQ simon!! I thought he meant to the start of a loop, not the crazy Arduino loop() that should be a main() lol

The loop() function is a function. It can be ended at any point with a return statement. Then, main() will call loop() again.

how do i write a return statement?

return;

ok that was a bit obvious lol sorry