Send data from processing to arduino

Processing is an open source processing language and IDE tool, based on visual arts with computer programming.

For electronic purposes, the processing tool can be used to communicate with  Arduino boards, Raspberry Pi, etc., from a computer device.

arduino processing interfaceSerial Communication between the Arduino and processing can be performed with the firmata or without firmata. Basically used to display or send data from Arduino in a graphical or visual context.

Arduino and Processing communication without Firmata 

Without a firmata the Arduino and processing will just communicate each other and processing will not have any Arduino control instructions and no Arduino library will be added. In programming without firmata, programs need to be written for both Arduino and Processing. Only serial communication will be performed between the Arduino and processing.

Below code, controls an LED with a mouse click.

Arduino code

char state;
void setup() {
 pinMode(13, OUTPUT);
 Serial.begin(9600);
}
void loop() {
 if (Serial.available() > 0) {
   state = Serial.read();
   if (state == 'H') {
     digitalWrite(13, HIGH);
   }
   if (state == 'L') {
     digitalWrite(13, LOW);
   }
 }
 delay(50);
}

Processing Code

import processing.serial.*;
Serial port;
void setup() {
//print the available serial ports.
  printArray(Serial.list());
//Select port from the listed array.
//replace [0] to [1],[2]...for selecting a usable open port.
port = new Serial(this,Serial.list()[0], 9600); } 
void draw() 
{   
if (mousePressed==true) { 
    port.write('H');   
} else { 
    port.write('L');   
}   
delay(50); }

Arduino and Processing communication with Firmata 

Using firmata the Arduino board can be controlled by the processing itself, by using an Arduino Processing serial library. We only have to just upload the Firmata sketch to the Arduino board. The arduino programs will be written with the processing.

Note, using firmata the baud rate is 57600.

Upload Firmata : File –> Examples –> Firmata –> StandardFirmata sketch

Processing Code

//LED Blink using processing.
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
void setup()
{
  //print the available serial ports.
  printArray(Serial.list());
  //Select port from the listed array.
  //replace [0] to [1],[2]...for selecting a usable open port.
  arduino = new Arduino(this,Serial.list()[0], 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
}
void draw()
{
  arduino.digitalWrite(ledPin, Arduino.HIGH);
  delay(1000);
  arduino.digitalWrite(ledPin, Arduino.LOW);
  delay(1000);
}

Advantages of using this method are, we can overcome many limitations of Arduino such as,

We can ignore the size limit; any complex or large sized programs can be written in processing since no any additional sketch need to be uploaded to the Arduino. For any programs of any file size, only a fixed standard firmata is needed to be uploaded.

We can run a number of programs without uploading to the Arduino, as the Arduino is controlled by the processing tool. For any program, we just need to upload the firmata once. Further programming and Run can be done without upload. So we can perform trail and error on the program without uploading the Arduino sketch each time. For projects with frequent changes and modifications, Arduino + Processing with Firmata is most effective. Because instantaneous outputs can be obtained by just play and stop.

Mouse control LED – Sample project

In this project, the processing code generates a button on the output window, with a text indicating the ON and OFF.
When the mouse pointer placed over the button the LED connected to the Arduino gets ON and the button colour changes to GREEN and text change to “ON”. On removing the pointer out of the button LED OFFs and the colour again change back to RED and text to “OFF”.

Mouse controlled LED – Arduino code

int state;
void setup() {
 pinMode(13, OUTPUT);
 Serial.begin(9600);
}
void loop() {
 if (Serial.available() > 0) {
   state = Serial.read();
   if (state == '1') {
     digitalWrite(13, HIGH);
   }
   if (state == '0') {
     digitalWrite(13, LOW);
   }
 }
 delay(50);
}

Mouse controlled LED – Processing code

import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, "COM4", 9600); 
  size(200, 200);
  background(255);
}
void draw() {    
  background(255);
  ellipse(100, 100, 50, 50);
  if ( dist( mouseX, mouseY, 100, 100 ) < 25 ) {
    port.write('1');
    fill( 0, 255, 0 );
    text("ON", 10, 30);
  } else {
    port.write('0');
    fill( 255, 0, 0 );
    text("OFF", 10, 30);
  }
  delay(50);
}
1 Star2 Stars3 Stars4 Stars5 Stars Rate Us!
Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *