I connected two FSR sensors and a push-button switch to a breadboard and Arduino, for the purpose of seeing how to read and send multiple serial outputs.
multiple serial output

First, using the Arduino and pressing on just one of the FSR sensors, I wrote code that allowed me to view the changing analog values for that sensor in the Serial Monitor. I made five columns in the Serial Monitor (BYTE, BIN, DEC, HEX, OCT) and printed the sensor data in many different formats to understand the difference between the raw binary value, the ASCII encoded binary value, and the ASCII encoded decimal / hexadecimal / octal values. You can see this happening in my video capture.

Next, I used all three sensors to send multiple serial values to Arduino and Processing. In order to visualize this, I created a sketch that would use the sensor values to move a ball on the screen, and change its color from black to white. I wrote code employing both the “Punctuation” method (sending values repeatedly) and the “Call-and-Response” method (sending one set of values at a time) for sending multiple serial values.

Multiple Serial Output with Arduino from Suzanne Kirkpatrick on Vimeo.

Here’s my Arduino code for the “Call-and-Response” method:

int analogOne = 0;       // analog input
int analogTwo = 1;       // analog input
int digitalOne = 2;      // digital input

int sensorValue = 0;     // reading from the sensor

void setup() {
  // configure the serial connection:
  Serial.begin(9600);
  // configure the digital input:
  pinMode(digitalOne, INPUT);
  establishContact();
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    int inByte = Serial.read();
    // read the sensor:
    sensorValue = analogRead(analogOne);
    // print the results:
    Serial.print(sensorValue, DEC);
    Serial.print(",");

    // read the sensor:
    sensorValue = analogRead(analogTwo);
    // print the results:
    Serial.print(sensorValue, DEC);
    Serial.print(",");

    // read the sensor:
    sensorValue = digitalRead(digitalOne);
    // print the last sensor value with a println() so that
    // each set of four readings prints on a line by itself:
    Serial.println(sensorValue, DEC);
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("hello");   // send a starting message
    delay(300);
  }
}

Here’s my Processing code for the “Call-and-Response” method:

import processing.serial.*;
Serial myPort;

float bgcolor = 65;
float fgcolor;
float xpos, ypos; // starting position of the ball

boolean firstContact = false;

void setup() {
  size(640,480);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);

 // read bytes into a buffer until you get a linefeed (ASCII 10):
  myPort.bufferUntil('\n');
}

void draw() {
  background(bgcolor);
  fill(fgcolor);
 // Draw the shape
  ellipse(xpos, ypos, 50, 50);
}

void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  if (myString != null) {

    myString = trim(myString);

    // if you haven't heard from the microcontroller yet, listen:
    if (firstContact == false) {
      if (myString.equals("hello")) {
        myPort.clear();          // clear the serial port buffer
        firstContact = true;     // you've had first contact from the microcontroller
        myPort.write('A');       // ask for more
      }
    }
    // if you have heard from the microcontroller, proceed:
    else {
      // split the string at the commas
      // and convert the sections into integers:
      int sensors[] = int(split(myString, ','));

      // print out the values you got:
      for (int sensorNum = 0; sensorNum  1) {
        xpos = map(sensors[0], 0,600,0,width);
        ypos = map(sensors[1], 0,600,0,height);
        fgcolor = sensors[2] * 255;
      }
    }
    // when you've parsed the data you have, ask for more:
    myPort.write("A");
  }
}

NOTE: I noticed that when I used the “Call-and-Response” method, the Processing sketch was drawn much smoother and without any lag in response time.


One Response to “Lab – Multiple Serial Output”  

  1. 1 lee smith

    Please show setup [ pin connections ] of your Arduino with 2 FSRs.

    I would like to make a multiple FSR project which adds the pressure changes and then depending on the total lights a LED or external light. Do you know of any tutorials which do this?

    Thanks in advance.

Leave a Reply