Hi, good morning
Im a beginner in arduino
i understand a little about arduino,
i have some problem with looping
i want to loop some void (not a void loop ()
we can use 'return' in to get back to void loop ();
and how if i only want to repeat a non loop void ?
and the void will end if i press some button and return to loop void ?
any suggestion ?
this is the example sketch
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("Input 1 to Turn LED on and 2 to off");
}
void loop()
{
if (Serial.available())
{
int state = Serial.parseInt();
if (state == 1)
{
digitalWrite(13, HIGH);
Serial.println("Command completed LED turned ON");
}
if (state == 2)
{
digitalWrite(13, LOW);
Serial.println("Command completed LED turned OFF");
}
if (state == 3)
{
example();
}
}
}
void example ()
{
Serial.println("wait for you to press 4");
delay(100);
int state = Serial.parseInt();
if (state == 4)
{
return;
}
}
more precisely, I want to make this 'void example()' keep run like a 'void loop ()' until i press 4 in serial monitor..
your ideas and suggestions are always welcome
the question already answered
How to repeat a non loop void ?
Answer is
char state;
void example() {
while('4' != state) { //if you read input from serial monitor it will be char type
Serial.printlc("wait for you to press 4");
state = Serial.read();//assuming you read intput from serial
delay(100);
}
}
Thread clear!!
Thanks to sir nitrof
its ok if there's another idea
your ideas and suggestions are always welcome
char state;
void example() {
while('4' != state) { //if you read input from serial monitor it will be char type
Serial.printlc("wait for you to press 4");
state = Serial.read();//assuming you read intput from serial
delay(100);
}
}
Ond use code editor tag instead os screen shot.. It more easier to read.
The correct term is "function", not "void". "void" is the return type of the function, in this case indicating that the function doesn't return anything. You will encounter functions that have other return types so it makes absolutely no sense to refer to them by their return type.
Try to keep all of the serial reading inside one function. Having one function looking for options 1-3 and another looking for 4 means that most of the time you will be in the wrong one. What if a 4 arrives during the time that the first one is active? It reads the character and discards it because it's not looking for a 4. The second one then doesn't get the opportunity to read the 4.
Think of the program having its own internal state. It remembers what command it was asked to do last. Maybe it keeps doing that command or maybe it finishes the command and stops to wait for another. States like is-running and stopped-waiting-for-4 should not be mixed up with the values received from the serial port.
void example() {
while('4' != state) { //if you read input from serial monitor it will be char type
Serial.printlc("wait for you to press 4");
state = Serial.read();//assuming you read intput from serial
delay(100);
}
}
Ond use code editor tag instead os screen shot.. It more easier to read.
its work sir, thank you so much, i can continue programming to the next level