Skip to content

Processing and Arduino with Serial communication

FA25 Kannai Dev Board
alt text alt text

Step1: Arduino > Processing

// Step1

const int buttonPin = 4; // Button on pin 4

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);               // Start USB serial communication
}

void loop() {
  // Send button state to Processing
  if (digitalRead(buttonPin) == LOW) {
    Serial.println("P"); // Send "P" = Pressed
  } else {
    Serial.println("R"); // Send "R" = Released
  }

  delay(100); // Wait to avoid flooding serial port
}
// Step1

import processing.serial.*;

Serial myPort;
String buttonLabel = ""; // Default label

void setup() {
  size(300, 200);
  println(Serial.list());

  String portName = "/dev/tty.usbmodem1101";
  myPort = new Serial(this, portName, 9600); // Set your port name
  myPort.bufferUntil('\n'); // Read input until newline
}

void draw() {
  background(220);
  fill(0);
  textAlign(CENTER, CENTER);
  textSize(20);
  text(buttonLabel, width/2, height/2); // Show current label
}

// Receive data from Arduino
void serialEvent(Serial myPort) {
  String inData = myPort.readStringUntil('\n'); // Read line from Arduino
  if (inData != null) {
    inData = inData.trim(); // Remove whitespace
    if (inData.equals("P")) {
      buttonLabel = "Arduino Button Pressed";
    } else if (inData.equals("R")) {
      buttonLabel = "";
    }
    println("Received: " + inData);
  }
}

Step2: Processing > Arduino

// Step2

import processing.serial.*;

Serial myPort;
Button ledButton;
String buttonLabel = "LED"; // Default label text in Step1 was ""


void setup() {
  size(300, 200);
  println(Serial.list());

  String portName = "/dev/tty.usbmodem1101";
  myPort = new Serial(this, portName, 9600); // Set your port name
  ledButton = new Button(buttonLabel, 100, 80, 100, 40); // Create button
}

void draw() {
  background(220);
  ledButton.display();
}

// Sending data to Arduino
void mousePressed() {
  if (ledButton.isClicked(mouseX, mouseY)) {
    myPort.write('1'); // Send '1' to Arduino to turn on LED
  }
}

// Button class
class Button {
  String label;
  int x, y, w, h;

  Button(String label, int x, int y, int w, int h) {
    this.label = label;
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }

  void display() {
    fill(200);
    rect(x, y, w, h);
    fill(0);
    textAlign(CENTER, CENTER);
    text(label, x + w/2, y + h/2);
  }

  boolean isClicked(int mx, int my) {
    return (mx > x && mx < x + w && my > y && my < y + h);
  }
}
// Step2

const int ledPin = 3;// LED on pin 3

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);            // Start USB serial communication
}

void loop() {
  // Receive LED control from Processing
  if (Serial.available()) {      // Check if data received from Processing
    char c = Serial.read();
    if (c == '1') {              // If character '1' received
      digitalWrite(ledPin, HIGH); // Turn LED on
      delay(500);                 // Keep it on for 0.5 seconds
      digitalWrite(ledPin, LOW);  // Turn LED off
    }
  }
}

Step3: Arduino < > Processing

// Step3

const int buttonPin = 4;  // Button on pin 4
const int ledPin = 3;     // LED on pin 3

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);     // Start USB serial communication
}

void loop() {
  // Send button state to Processing (Step1)
  if (digitalRead(buttonPin) == LOW) {
    Serial.println("P"); // Send "P" = Pressed
  } else {
    Serial.println("R"); // Send "R" = Released
  }

  // Receive LED control from Processing (Step2)
  if (Serial.available()) {      // Check if data received from Processing
    char c = Serial.read();
    if (c == '1') {              // If character '1' received
      digitalWrite(ledPin, HIGH); // Turn LED on
      delay(500);                 // Keep it on for 0.5 seconds
      digitalWrite(ledPin, LOW);  // Turn LED off
    }
  }

  delay(100); // Wait to avoid flooding serial
}
// Step3

import processing.serial.*;

Serial myPort;
Button ledButton;
String buttonLabel = "LED"; // Default label text in Step1 was ""

void setup() {
  size(300, 200);
  println(Serial.list());

  String portName = "/dev/tty.usbmodem1101";
  myPort = new Serial(this, portName, 9600); // Set your port name
  myPort.bufferUntil('\n'); // Read input until newline
  ledButton = new Button(buttonLabel, 100, 80, 100, 40); // Create button
}

void draw() {
  background(220);
  ledButton.label = buttonLabel; // Update label from Arduino input
  ledButton.display();
}

// Receive data from Arduino (Step1)
void serialEvent(Serial myPort) {
  String inData = myPort.readStringUntil('\n');
  if (inData != null) {
    inData = inData.trim();
    if (inData.equals("P")) {
      buttonLabel = "Arduino Button Pressed";
    } else if (inData.equals("R")) {
      buttonLabel = "LED";
    }
    println("Received: " + inData);
  }
}

// Sending data to Arduino (Step2)
void mousePressed() {
  // Only allow LED control when not physically pressed
  if (buttonLabel.equals("LED") && ledButton.isClicked(mouseX, mouseY)) {
    myPort.write('1'); // Send command to Arduino to light LED
  }
}

// Button class
class Button {
  String label;
  int x, y, w, h;

  Button(String label, int x, int y, int w, int h) {
    this.label = label;
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }

  void display() {
    fill(200);
    rect(x, y, w, h);
    fill(0);
    textAlign(CENTER, CENTER);
    text(label, x + w/2, y + h/2);
  }

  boolean isClicked(int mx, int my) {
    return (mx > x && mx < x + w && my > y && my < y + h);
  }
}