Interaction Design WikiSpatial Interaction

Serial Communication (en)

German Version

Since the Arduino has a serial interface, we can also use this to see current values from the Arduino or to communicate directly with a computer. To use the serial interface, we only need to call the Serial.begin () function in setup (). Then we can use the Serial.print () or Serial.println () function to output any values. The Arduino IDE also has its own monitor. An example of serial communication looks like this:

Example "Hello World" Expand source
void setup() 
{
  Serial.begin(9600);
}
void loop() 
{
  Serial.println("Hallo Welt");
}

Serial Monitor

The corresponding baud rate must be selected in the serial monitor to display information and then the data is displayed. The monitor looks like this.

 

Call Response

This example will send back over serial everything it receives until it finds a line break symbol. 

Example "Call response" Expand source
String inData;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0)
  {
    char recieved = Serial.read();
    inData += recieved;  // Process message when new line character is recieved
    if (recieved == '\n')
    {
            Serial.print("I got : ");
            Serial.println(inData);
            inData = ""; // Clear recieved buffer
            delay(1000);
    }
  } 
}

Further Information

Serial - The Arduino reference page