typestar

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

  1. Serial.available() > 0 guards against reading nothing.
  2. Serial.read() returns a single char from the buffer.
  3. Echoing input back is the first debugging tool to build.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Serial, step 13 of 27 in Language basics.

← Previous Next →