// Read data from the serial port and assign it to a variable. Set the fill a // rectangle on the screen using the value read from a light sensor connected // to the Wiring or Arduino board import processing.serial.*; Serial port; // Create object from Serial class String val; // Data received from the serial port PImage img; // Background image PImage car; // car image float xpos1; float ypos1; float newx; void setup() { size(612, 433); noStroke(); frameRate(10); // Run 10 frames per second String portName = Serial.list()[1]; //retrieving data from COM10 // Open the port that the board is connected to and use the same speed (9600 bps) port = new Serial(this, portName, 115200); img = loadImage("racing_background.jpg"); //loading the background image car = loadImage("car.png"); //loading the background image xpos1 = 20; ypos1 = 280; image(car, xpos1, ypos1, width/5, height/5); } void draw() { background(img); if (0 < port.available()) { // If data is available to read, val = port.readString(); // read it and store it in val } if (val != null) { newx = xpos1 + float(val)/10; //update the car's position println(newx); image(car, newx, ypos1, width/5, height/5); //redraw the car at the new position fill(152,190,100); // Set fill color with the value read textSize(100); val = val.replace('\n', ' '); text(val + "mm", 150, 250); } else{ println("null");} }