yet I faced another smaller problem in my current project. I created a function called CheckBACKbutton() which obviously checks if the BACK button of my UI is pressed or not. If so, the state of my finite state machine is set to the default state and clears the LCD.
So far so good, this always worked in all of my states as in the next loop cycle I called the default state and everything worked well. Not so in a specific state where I call a while loop which should repeat over and over as long as no changes to the other settings are done which can be modified by some potentiometers. This gives me the problem that even if my CheckBACKbutton() function changes the FSM state, I still keep executing my while loop.
Leading to the simple question: is there a possibility of returning a 'return' by my CheckBACKbutton() function?
Maybe someons has an idea how I can use my generic CheckBACKbutton() function even in the FSM state where I have by while loop.
Thanks in advance!
Best,
orPoG
Code of the CheckBACKbutton() function:
void CheckBACKbutton() {
iBtt_Back_status_g = digitalRead(Btt_Back);
if (iBtt_Back_status_g == HIGH) {
iV_Cursor_g = 1;
lcd.clear(); // cleares the complete display
bV_SimulationCounter_g = 0; // resets the simulation counter for next cycle
sV_actualState_g = s_startScreen;
return;
}
}
pseudo code of the rest of my project:
void f_otherState1(){
while(nothing changes at potentiometers){
CheckBACKbutton() ;
execute tasks
}
}
void loop() {
// -------- FSM --------
switch (sV_actualState_g) {
case s_startScreen:
f_startScreen(); // calls the function 'f_startScreen()'
break;
case s_otherState1:
f_otherState1();
break;
...
}
Yes, I do not return a value as I did not need one so far. I just change the FSM state so within the next loop cycle the default state is called.
But do you have any suggestions which value I should return? Even if I return a number or what else, I have to check within the while loop if that value was returned or another value is still stored in a variable. In that case it wouldn't make any sense (in my point of view) to check the BACK button by the function - just copy and paste the content of my function inside the while loop as on that position the return would really jump out of my while loop.
I thought about a possibility of simply returning a return as then the current while loop would stop.
I hope I explained it as understandable as possible
EDIT:
I checked my idea and it works: if I c&p the content of my funtion inside of thw while loop, the code behavior is as intended.
I am confused as to what you want to do, but don't use a while loop when in switch/state. There is no need for it because effectively the switch/state allows you to execute code while the state variable is a certain value.
Don't forget that you can have more than one exit condition for a state and that the target state does not need to be the same for each of them
You are totally right! But in my case I have a very time sensitive topic which is already hard to reach with my Due even by using the while loop. I am totally aware that this is on the first hand not the nicest implementation but I think it is the only possible. Additionally I just need to end the while loop if pressing the BACK button.
I just hoped that there is an easy way to return a 'return' by a function.
The while loop should be replaced with an if (condition) that either changes the value of state (exiting the repeat) or not (repeats the state/case just run) so that the state machine exits to run again for every case.
Consider when a case runs, that was the state value case. If it doesn't change, the case runs again and again like waiting for text to arrive with serial available. 1st char arrives, change the state, return and Next Time the new state runs. The state variable is like a GOTO within the machine.
We need to understand the function of the return statement before we use it to get services.
We use the return statement at the end of a user-defined (UDF) (called program) function to supply a single-valued parameter (if there is any) to the calling program. If there is nothing to give to the calling program, then why should we place the return statement at the end of UDF?
Oh yes, you are right! My bad, I copy and pasted it in a function to reuse it and did not remove the (useless) return in my function. You are right, at that point that doesn't make any sense as either way the function would be ended at that point.
This doesn't work but my intention is to do something like this with the focus on return (return); :
void CheckBACKbutton() {
iBtt_Back_status_g = digitalRead(Btt_Back);
if (iBtt_Back_status_g == HIGH) {
iV_Cursor_g = 1;
lcd.clear(); // cleares the complete display
bV_SimulationCounter_g = 0; // resets the simulation counter for next cycle
sV_actualState_g = s_startScreen;
return (return);
}
}
HLL codes are converted into assembly codes and then into machine codes. For ATmega328P, the assembly code to retrieve the return address is ret and not return.
And thats basically my problem: I know that this code does not compile. I tried it and also was aware before my try that it will probably not compile. But that is exactly what I was searching for. This is the question this thread is all about.
I do not want to store a value which is returned by my function as then it would be easier to just use the code of my function and paste it in the while loop.
So if there is no chance to return a return, I guess my question is already answered
I said in post #10 and #15 that it doesn't work and will never
I just asked for the possibility if I could do so in another way. My intention of the code line return (return); was simply to describe my intention as it seemed to be unclear and not well described by me before.
But I sum up the discussion to the simple result: it is not possible to return a return (keyword). Thats all I wanted to know
I still have no idea what you are trying to do by "returning a return". Whatever it is, replace the while with an if so that the code for the case is repeated until the condition become true