Return to main loop from a function

Hi Guys,

How do I exit from the following function, back to loop() ? The condition for return would be sw1 == LOW.

void setTrigger()
{
  int swval = digitalRead(sw1);
  boolean encoderA=digitalRead(encoderPinA);
  if((encoderALast == HIGH) && (encoderA == LOW))
  {
    if (digitalRead(encoderPinB) == LOW)
    {
      encoderPos--;
    }
    else
    {
      encoderPos++;
    }
    angle=(encoderPos % encoderStepsPerRevolution)*360/encoderStepsPerRevolution;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Trigger Delay uS");
    lcd.setCursor(0, 1);
    lcd.print(encoderPos);
    lcd.print(" ");
    lcd.print(angle);
    int swval = digitalRead(sw1);
  }
  encoderALast = encoderA;
  }

Put return; at the point you want it to return.

For a function returning void, there is an implicit return at the end of the function body.

Thanks PeterH,

Ok, so I try like the following and the function exits before I press sw1. Any ideas why?

void setTrigger()
{
  int swval = digitalRead(sw1);
  if(swval == LOW)
  {
    return;
  }
  boolean encoderA=digitalRead(encoderPinA);
  if((encoderALast == HIGH) && (encoderA == LOW))
  {
    if (digitalRead(encoderPinB) == LOW)
    {
      encoderPos--;
    }
    else
    {
      encoderPos++;
    }
    angle=(encoderPos % encoderStepsPerRevolution)*360/encoderStepsPerRevolution;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Trigger Delay uS");
    lcd.setCursor(0, 1);
    lcd.print(encoderPos);
    lcd.print(" ");
    lcd.print(angle);
    int swval = digitalRead(sw1);
  }
  encoderALast = encoderA;
  }

Because the input pin is low?
How is it wired?
Pull up?
Pull down?

What stops it from simply reaching the end of the function, and returning from there?

As follows...

int sw1 = 3;

void setup()
{
pinMode(sw1, INPUT);
digitalWrite(sw1, HIGH);
}

dxw00d:
What stops it from simply reaching the end of the function, and returning from there?

Ok, screwed up my initial code post. as follows...

void setTrigger()
{
  int swval = digitalRead(sw1);
  while(swval == HIGH)
  {
  boolean encoderA=digitalRead(encoderPinA);
  if((encoderALast == HIGH) && (encoderA == LOW))
  {
    if (digitalRead(encoderPinB) == LOW)
    {
      encoderPos--;
    }
    else
    {
      encoderPos++;
    }
    angle=(encoderPos % encoderStepsPerRevolution)*360/encoderStepsPerRevolution;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Trigger Delay uS");
    lcd.setCursor(0, 1);
    lcd.print(encoderPos);
    lcd.print(" ");
    lcd.print(angle);
    int swval = digitalRead(sw1);
  }
  encoderALast = encoderA;
  //end encoder stuff
  //swcount++;
  }
}

Noob with too many windoze open...

The problem is here:

    lcd.print(" ");
    lcd.print(angle);
    int swval = digitalRead(sw1);   // a new swval declared inside a code block
  }

You probably copy-n-pasted the first declaration.

That swval has the same name as the first one, but is totally different variable, which ends its life as soon as the code block it’s into ends. Therefore the first swval is never updated with the pin value and the loops never ends.

Take away that “int” and it should work (haven’t checked the whole logic though):

    lcd.print(" ");
    lcd.print(angle);
    swval = digitalRead(sw1);   // a new swval declared inside a code block
  }