I have the below loop. When user types 1,function 1 is called. I would like the system to keep calling function1 until another input is received (e.g. 2) and then the operation is changed. How can I achieve this ?
The same functionality with a push button (instead of serial.read) would be appreciated.
void loop() {
control = Serial.read();
if (control == '1') function1();
else if (control =='2') function(2);
Please use code tags! Please read 'how to use this forum - please read', especially item #7, then modify your post.
You have not declared the type of variable that control is.
Have another variable that only changes if control is 1 or 2. Assign the value of control to this variable when control is 1 or 2, but not anything else. Us this variable in your if else statement.
Pleade #1 about the forum rules. For your question you can easly achive it with hardware interrupt. In first one, just set variable like temp1=1 and temp2=0, for secont one type temp1=0 temp2=1.
In main loop just set if condition for these two variables to call your functions. If you have any problems fell free to ask
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
You will see that it makes the variable newData = true; when a new message arrives. That will allow your program to continue with the old value until a new one is received.