Serial input in Arduino
available counts waiting bytes; read takes them one by one.
// available counts waiting bytes; read takes them one at a time.
void setup() {
Serial.begin(9600);
Serial.println("Echo ready: type at me");
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
Serial.print("Got: ");
Serial.println(c);
}
}
How it works
Serial.available() > 0guards against reading nothing.Serial.read()returns a single char from the buffer.- Echoing input back is the first debugging tool to build.
Keywords and builtins used here
Serialavailablebegincharifloopprintprintlnreadsetupvoid
The run, in numbers
- Lines
- 13
- Characters to type
- 259
- Tokens
- 66
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 57 seconds.
Step 1 of 3 in Serial, step 13 of 27 in Language basics.