. Advertisement .
..3..
. Advertisement .
..4..
Many of our readers do not know how to set the Arduino wait for input and ask us for our help. This guide will put a stop to your dilemma by offering several feasible approaches. Keep scrolling for more helpful tips!
How to Set Arduino Wait for Input
Method 1. Use The Function “Serial.Available()”
Suppose you want to decode your input from serial ports. Then we suggest using the function “Serial.available()
to await the input. Such functions produce the byte quantities available in that serial port.
But what if no input is present? Then it will send back zero.
void setup() {
Serial.begin(9500);
}
void loop() {
while(Serial.available() == 0) {
}
int mydata = Serial.read();
}
Method 2. Use The Function DigitalRead()
Let’s say this time you prefer reading the input from digital pins. In such cases, it’s advisable to use the function digitalRead()
to await the input. Such functions will help you read digital pins’ values that either fall into the HIGH or LOW categories.
int valPin = 1;
int inputPin = 6;
void setup() {
pinMode(inputPin, INPUT);
}
void loop() {
while(digitalRead(inputPin) != LOW){
}
valPin = digitalRead(inputPin);
}
Method 3. Start Your Serial Monitor
A common problem that many Arduino users (or anyone who often uses microcontroller boards) face is their tendency to forget to kickstart Serial Monitors before proceeding with their board programs. Hence, once they open Serial Monitor, several print statements will be overlooked.
One great solution to tackle this issue is to open the sketches only after you have received the input via Serial Monitors. Such moves ensure you will not overlook any Serial Monitor print due to your delay in beginning Serial Monitors.
void setup() {
// put your setup code here, to run once:
Serial.begin(9500);
Serial.println();
Serial.println("Please send a character to start the sketch");
while (Serial.available() == 0) {
Serial.print(".");
delay(400);
}
while (Serial.available() > 0) {
Serial.read(); //Empty the read buffer
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Looping!");
delay(999);
}
Hence, your sketches will await the user input first. Once the input arrives, those sketches will clear all read buffers and proceed with their remaining tasks.
Your Serial Monitor will show the following message:
Please send a character to start the sketch
—-------------------------------
or
Please send a character to start the sketch
—-------------------------------
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
Looping!
There’s one thing to remember: you should only wait for the user input in your test codes (instead of the production codes). Otherwise, getting user inputs is simply impossible.
Conclusion
This article has shown you three different methods for setting Arduino wait for input. Each method is straightforward and simple to follow, so feel free to choose one that seems the most comprehensible to you. ITtutoria hopes that this tutorial will be of some great help for you!For similar issues in Arduino (such as converting char to int), feel free to browse all other ITtutoria guidelines.
Leave a comment