Sunday, February 21, 2016

ARTICLE #17 : Using Arduino with Processing

Introduction :

For writing programs and loading them to the Arduino board, we used the Arduino IDE program. Now we will see another programming language and IDE to communicate with Arduino using serial port which is called Processing.

Processing is simply an open source computer programming language and IDE built for the electronic arts using a visual context. The processing language is based on the Java language. So the programs that we are going to write are similar to Java code.

In this tutorial we will see :

  •         How to send data from Android and receive it in Processing.
  •         How to send data from processing and receive it in Android.

To perform this tutorial you will have to have:

  •         Arduino IDE download it from here.
  •         Processing IDE download it from here.



Hello world :

We will write a “hello world” to the Arduino board using Arduino IDE and then manage to receive it in the Processing IDE.


Code Arduino :

Void setup() {
            Serial.begin(9600); //initializing serial communications
}
Void loop() {
            Serial.println(“hello world”); //wait 500 ms and write it again
}


Code Processing :

To receive data in Processing we have to create an object from serial class that will get the communication port that Arduino is hooked to.

import processing.serial.*;

Serial myPort;
String val; //Data received from the serial port

void setup() {

            String portName = Serial.list()[0];//depending on the port number of the device
            myPort = new Serial(this, portName, 9600);
}

void draw() {

            if(myPort.available() > 0) { //If data is available
                        val = myPort.readStringUntil(‘\n’);//read the string and put it in val
                        If(val != null)
                                    Println(val);
            }

}


Now that we learned how to receive data sent from Arduino to Processing. We will now learn how to do it the other way, from processing to Arduino.
What does the following code ? whenever we press the mouse, processing sends ‘1’ to Arduino. Whenever the Arduino IDE catches ‘1’ it turns the LED pin 13 on.


Code Processing :


import processing.serial.*;
Serial myPort; //Creating a Serial object

void setup() {

            size(200, 200);//The space for the mouse to click
            String portName = Serial.list()[0];
            myPort = new Serial(this, portName, 9600);

}
void draw() {

            if(mousePresses == true) {
                        myPort.write(‘1’);//send 1 over the serial port
                        println(“1”);//printing it to make sure we are sending 1
                        } else {
                        myPort.write(‘0’); //send 0 instead
                        }

}

Code Arduino :


char cal;
int ledPin = 13;

void setup() {

            pinMode(ledPin, OUTPUT);
            Serial.begin(9600);

}

void loop() {

            if(Serial.available()) {

                        val = Serial.read();

            }
if (val == ‘1’) {

            digitalWrite(ledPin, HIGH);
            } else {
            digitalWrite(ledPin, LOW);

}

delay(10);
}



When we load the code above to Arduino and run Processing sketch, you should be able to turn the LED on by simply clicking into the created processing canvas.

No comments:

Post a Comment