Processing is an open-source programming language and environment for creating visual arts, graphics, and interactive projects. It simplifies graphics programming, making it ideal for beginners and artists.
Features:
Designed for visual art and multimedia projects.
Built on Java, but simpler to use.
Examples: Drawing shapes, animations, and creating interactive visuals.
Draws a line from a fixed point to the current position of the mouse cursor
The draw() function is called repeatedly (about 60 times per second by default). It updates the canvas
// Hello mouse.void setup() { size(400, 400); // Creates a 400x400 pixel canvas. stroke(255); // Sets the line color to white (255 = max brightness). background(192, 64, 0); // Fills the canvas with an orange-red background color.}// Draw a line between a fixed point (top-left) and the current mouse positionvoid draw() { line(150, 25, mouseX, mouseY);}
#include <Arduino.h>// Connect Hardware: If using a sensor, connect it to analog pin A0 and GND.// For testing without a sensor, connect A0 to 5V (reads ~1023) or GND (reads ~0)void setup(){ Serial.begin(9600); // Set baud rate to match the Processing sketch}void loop(){ int sensorValue = analogRead(A0); // Read a value (0-1023) from analog pin A0 Serial.println(sensorValue); // Send the value over the serial port delay(500); // Wait 500 milliseconds before sending again}
find_com_port_manual.pde
find_com_port_manual.pde
import processing.serial.*;Serial myPort;int port = 4;void setup() { size(800, 600); // List all available serial ports //println(Serial.list()); String[] ports = Serial.list(); println("Available ports:"); for (int i = 0; i < ports.length; i++) { println(i + ": " + ports[i]); } if (Serial.list().length > 0) { myPort = new Serial(this, Serial.list()[port], 9600); // Open the port println("Connected to: " + Serial.list()[port]); delay(2000); // Wait for 2 seconds before attempting to open the port println(myPort.available()); if (myPort != null && myPort.available() > 0) { String value = myPort.readStringUntil('\n'); // Read until newline if (value != null) { println(value); // Display the data from Arduino } } }}void draw() { if (myPort != null && myPort.available() > 0) { String value = myPort.readStringUntil('\n'); // Read until newline if (value != null) { //println(value); // Display the data from Arduino } }}
Available ports:
0: COM5
1: COM6
2: COM7
3: COM8
4: COM15
Connected to: COM15
73
10223
find_com_port.pde
find_com_port.pde
import processing.serial.*;Serial myPort;Serial arduinoPort;void setup() { size(800, 600); // List all available serial ports //println(Serial.list()); String[] ports = Serial.list(); println("Available ports:"); for (int i = 0; i < ports.length; i++) { println(i + ": " + ports[i]); } if (Serial.list().length > 0) { // Attempt to auto-detect the Arduino for (String port : ports) { try { // Try opening the port at 9600 baud rate myPort = new Serial(this, port, 9600); // Open the port println("Checking for Arduino on port: " + port); delay(1700); // Wait before attempting to read data if (myPort != null && myPort.available() > 0) { println("Found Arduino port: " + port); //String value = myPort.readStringUntil('\n'); // Read until newline //if (value != null) { // println("Received data from Arduino: " + value); // Display the data from Arduino //} return; } } catch (Exception e) { println("Could not open port: " + port); } } } // If no Arduino is found println("Arduino not found. Please check the connection.");}void draw() { if (myPort != null && myPort.available() > 0) { String value = myPort.readStringUntil('\n'); // Read until newline if (value != null) { //println(value); // Display the data from Arduino } }}
Available ports:
0: COM5
1: COM6
2: COM7
3: COM8
4: COM15
Could not open port: COM5
Checking for Arduino on port: COM6
Could not open port: COM7
Checking for Arduino on port: COM8
Checking for Arduino on port: COM15
Found Arduino port: COM15
Received data from Arduino: 102
processing_arduino_sonar.pde
processing_arduino_sonar.pde
import processing.serial.*;import java.awt.event.KeyEvent;import java.io.IOException;Serial port; // Serial port object for communication with external hardwareString angleData = ""; // Stores angle data received from serial inputString distanceData = ""; // Stores distance data received from serial inputString rawData = ""; // Raw data string from serial portString statusMessage; // Displays if the object is in or out of rangefloat distancePixels; // Distance in pixels, based on the real-world distanceint parsedAngle, parsedDistance; // Parsed angle and distance values as integersint commaIndex1 = 0; // Index for separating angle and distance in raw dataint commaIndex2 = 0;PFont fontType;void setup() { // Set up the display window and initialize serial communication size(1200, 700); // Adjust to screen size smooth(); port = new Serial(this, "COM10", 115200); // Set serial port and baud rate port.bufferUntil('.'); // Read data until a '.' character}void draw() { // Main draw function to update visuals each frame fill(98, 245, 31); // Set fill color noStroke(); // No border on shapes fill(0, 4); // Semi-transparent background for motion blur effect rect(0, 0, width, height - height * 0.065); // Slight fade in the top area fill(98, 245, 31); // Reset color for radar elements drawRadarDisplay(); // Draw the radar arcs and angle lines drawDirectionLine(); // Draw the line indicating current angle drawDetectedObject(); // Draw the object based on angle and distance displayText(); // Display additional data text on screen}void serialEvent(Serial port) { // Handles data reading and parsing from the serial port rawData = port.readStringUntil('.'); // Read data until the delimiter '.' rawData = rawData.substring(0, rawData.length() - 1); // Remove trailing '.' commaIndex1 = rawData.indexOf(","); // Find the position of ',' to separate angle and distance angleData = rawData.substring(0, commaIndex1); // Extract angle data distanceData = rawData.substring(commaIndex1 + 1, rawData.length()); // Extract distance data parsedAngle = int(angleData); // Convert angle data to integer parsedDistance = int(distanceData); // Convert distance data to integer}void drawRadarDisplay() { // Draws radar arcs and angle guide lines pushMatrix(); translate(width / 2, height - height * 0.074); // Set radar center position noFill(); strokeWeight(2); stroke(98, 245, 31); // Set color for radar lines // Draw concentric arcs representing distance zones arc(0, 0, width - width * 0.0625, width - width * 0.0625, PI, TWO_PI); arc(0, 0, width - width * 0.27, width - width * 0.27, PI, TWO_PI); arc(0, 0, width - width * 0.479, width - width * 0.479, PI, TWO_PI); arc(0, 0, width - width * 0.687, width - width * 0.687, PI, TWO_PI); // Draw angle guide lines every 30 degrees from 30° to 150° line(-width / 2, 0, width / 2, 0); for (int angle = 30; angle <= 150; angle += 30) { line(0, 0, (-width / 2) * cos(radians(angle)), (-width / 2) * sin(radians(angle))); } popMatrix();}void drawDetectedObject() { // Draws the detected object based on angle and distance pushMatrix(); translate(width / 2, height - height * 0.074); // Set radar center position strokeWeight(9); stroke(255, 10, 10); // Red color for detected object distancePixels = parsedDistance * ((height - height * 0.1666) * 0.025); // Convert distance to pixels if (parsedDistance < 40) { // Limit display range to 40 cm line( distancePixels * cos(radians(parsedAngle)), -distancePixels * sin(radians(parsedAngle)), (width - width * 0.505) * cos(radians(parsedAngle)), -(width - width * 0.505) * sin(radians(parsedAngle)) ); } popMatrix();}void drawDirectionLine() { // Draws a line showing the current angle on the radar pushMatrix(); strokeWeight(9); stroke(30, 250, 60); // Green line color translate(width / 2, height - height * 0.074); // Set radar center position line(0, 0, (height - height * 0.12) * cos(radians(parsedAngle)), -(height - height * 0.12) * sin(radians(parsedAngle))); popMatrix();}void displayText() { // Display distance and angle information on screen pushMatrix(); statusMessage = (parsedDistance > 40) ? "Out of Range" : "In Range"; // Set range message fill(0, 0, 0); noStroke(); rect(0, height - height * 0.0648, width, height); // Display background for text fill(98, 245, 31); // Text color textSize(25); // Display distance markers text("10cm", width - width * 0.3854, height - height * 0.0833); text("20cm", width - width * 0.281, height - height * 0.0833); text("30cm", width - width * 0.177, height - height * 0.0833); text("40cm", width - width * 0.0729, height - height * 0.0833); textSize(40); text("SciCraft", width - width * 0.875, height - height * 0.0277); // Display title text("Angle: " + parsedAngle + "°", width - width * 0.48, height - height * 0.0277); // Display angle text("Distance: ", width - width * 0.26, height - height * 0.0277); // Display distance if (parsedDistance < 40) { // Display only within range text(" " + parsedDistance + " cm", width - width * 0.225, height - height * 0.0277); } textSize(25); fill(98, 245, 60); // Color for angle labels // Display angle markers from 30° to 150° float[] angles = {30, 60, 90, 120, 150}; float[] angleTranslations = {0.4994, 0.503, 0.507, 0.513, 0.5104}; int[] rotations = {-60, -30, 0, -30, -60}; for (int i = 0; i < angles.length; i++) { resetMatrix(); translate( (width - width * angleTranslations[i]) + width / 2 * cos(radians(angles[i])), (height - height * 0.0907) - width / 2 * sin(radians(angles[i])) ); rotate(radians(rotations[i])); // Adjust rotation for each angle label text((int) angles[i] + "°", 0, 0); // Display angle label } popMatrix();}
modified processing_arduino_sonar.pde
Enable auto-detection of Arduino COM port
Added exception handling to prevent disabling serialEvent() from crashing programming
ARM Compatibility: Make sure you’re using the ARM version of Processing, as the standard x86 version will not work on the Raspberry Pi’s ARM architecture.
Performance: Processing runs best on a Raspberry Pi 4 or later, especially for graphical-intensive sketches. For earlier Pi versions, consider optimizing your sketches for performance.
Run Sonar sketch from command line
Use processing-java command to run sketches in the command line using syntax: