You are currently viewing Serial communication with Arduino

Passing sensor data from the onboard sensors on the Nano to the serial monitor worked fine and I could see them in both the serial monitor and the plotter. Parsing the lines of data from the tab-separated stream proved more difficult than I thought it would be (You’re not surprised, I know).

At first, I thought I’d just send one axis of data to the Processing interface and work with that data – but I really wanted to be able to use all the axes to control a graphic on the screen – because of course I have to make it difficult using my novice skillset. See where it flatlines below? that’s my thought process.

x y and z readings from internal IMU plotted via serial connection while moving the board
x y and z data from the IMU
  import processing.serial.*;

  Serial myPort;     
  int xPos = 1;       
  float inByte = 0;

  void setup () {
    size(1200, 800);
    println(Serial.list());
    myPort = new Serial(this, Serial.list()[1], 9600);
    myPort.bufferUntil('\n');
    background(0);
  }

  void draw () {
    stroke(108, 226, 251);
    line(xPos, height, xPos, height - inByte);

    if (xPos >= width) {
      xPos = 0;
      background(16, 104, 48);
    } else {
      //counter advances
      xPos++;
    }
  }

  void serialEvent (Serial myPort) {
    String inString = myPort.readStringUntil('\n');
    if (inString != null) {
      inString = trim(inString);
      inByte = float(inString);
      println(inByte);
      inByte = map(inByte, 0, 1023, 0, height);
    }
  }

Useful resources:

Graph | Arduino

Sensor Data Streaming with Arduino – Arduino Project Hub

Data / Processing.org

Best color picker!!: Color wheel, a color palette generator | Adobe Color

Leave a Reply