Car insurance

Basic Processing to Arduino Communications

Finally sat down and figured out how to send some basic signals via serial from Processing to an Arduino.  For this example I was just looking for basic functionality, so I settled for a simple way to turn a LED on and off.  It’s also mostly sample code gathered from the Internet.

The Processing code creates a small black square on the screen.  When the mouse cursor is moved over the square, it changes color to yellow and sends a “H” over serial to the Arduino.  While the Arduino detects a “H” on serial it will light the LED.

Here’s a video of it in action and describing how to get started.

Basic Processing to Arduino Communications from Morgellon on Vimeo.

  • Download Processing

http://processing.org/download/index.html

  • Download Arduino to Processing Library

http://www.arduino.cc/playground/Interfacing/Processing

Now that you’ve gotten all the files installed and in their proper places, try out my test code I used in the video.

Arduino Code (Download here)

//  Read data from the serial port and turn ON or OFF a light depending on the value

char val;         //Data received from the serial port
int ledPin = 13;  //Set value ledPin to pin 13

void setup(){
pinMode(ledPin, OUTPUT);  //Sets pin as OUTPUT
Serial.begin(9600);    //Start serial communication at 9600bps
}

void loop(){
if (Serial.available()){  //If data is available to read,
val = Serial.read();    //read it and store it as val
}
if (val == ‘H’){              //If H is recieved
digitalWrite(ledPin, HIGH);  //turn ON light
} else {
digitalWrite(ledPin, LOW);  //If not leave light OFF
}
delay(25);
}

Processing Code(Download here)

//Check if the mouse is over a rectangle and write the status to the serial port

import processing.serial.*;
import cc.arduino.*;


Serial port;    //Create object from Serial class

void setup() {
size(200, 200);
noStroke();
frameRate(10);
//Open the port that the board is connected to and use the same speed (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
background (255);
if (mouseOverRect() == true) {  //If mouse if over square
fill(242, 204, 47);                    //change color
port.write(‘H’);              //send H to serial port
} else {            //If mouse is NOT over square
fill(0);          //change color
port.write(‘L’);  //send L to serial port
}
rect(50, 50, 100, 100);  //Draws the square
}

boolean mouseOverRect() {    //Tests if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

One Comment zu “Basic Processing to Arduino Communications”

  1. Nachlese zum ersten Arduino-Basteln « Bischofs Blog

    [...] und Processing: http://arduino.cc/en/Tutorial/Graph http://arduino.cc/en/Tutorial/Dimmer http://dailyduino.com/archives/483 http://dailyduino.com/archives/487 [...]



Leave a Reply