system
February 5, 2012, 4:53pm
1
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;
}
system
February 5, 2012, 5:01pm
2
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.
system
February 5, 2012, 5:14pm
3
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;
}
system
February 5, 2012, 5:14pm
4
Because the input pin is low?
How is it wired?
Pull up?
Pull down?
system
February 5, 2012, 5:18pm
5
What stops it from simply reaching the end of the function, and returning from there?
system
February 5, 2012, 5:18pm
6
As follows...
int sw1 = 3;
void setup()
{
pinMode(sw1, INPUT);
digitalWrite(sw1, HIGH);
}
system
February 5, 2012, 5:25pm
7
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...
system
February 6, 2012, 10:31am
8
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
}