Car insurance

Basic Processing to Arduino Communications: part 2

Continuing from the last sketch, I’ve done the same thing but in a different manner.  I still have Processing generate a square and when the mouse moves over the square it lights a LED on the Arduino, just as before.  Although this time, the only code on the Arduino is the “Standard Firmata” sketch.

Processing interfaces with Firmata to control the pins as one would in the Arduino IDE.  So instead of sending for “H” or “L” on the serial port, we send arduino.digitalWrite(ledPin, Arduino.HIGH) or arduino.digitalWrite(ledPin, Arduino.LOW) on the serial port.  See the video below for examples, and code at the bottom.


Basic Processing to Arduino Communications part 02 from Morgellon on Vimeo.

My first impressions of Firmata and why one would use it:

  • All code is stored in Processing, commands are sent via serial, an Arduino running Firmata interprets commands.
  • With the Arduino running Firmata, Processing can change the different values, variables, or even “load” different “sketches”… this could eliminate the need to flash different sketches to the Arduino, and use Processing to “simulate” this when needed.
  • Perhaps by keeping the code in Processing and sending commands via serial to be handled by the Firmata on the Arduino, would mean that one would not be restricted by the memory size of the Arduino chip. (This is just a guess on my part).

First impressions of Firmata and why one would NOT use it.

  • To have functionality with the Arduino after unplugging it from USB, the code would need to be running on the Arduino.  Once the Arduino is unplugged from USB, there is nothing for Firmata to interpret, so the Arduino will do nothing.

Processing Code

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

Arduino arduino;
int ledPin = 12;

void setup()
{
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0]); // v2
//arduino = new Arduino(this, Arduino.list()[0], 57600); // v1
arduino.pinMode(ledPin, Arduino.OUTPUT);

size(200, 200);
noStroke();
frameRate(10);
}

void draw()
{
background (255);
if (mouseOverRect() == true) {  //if mouse over square
fill(242, 204, 47);  //yellow color
arduino.digitalWrite(ledPin, Arduino.HIGH);  //LED on
} else {
fill(0);  //black color
arduino.digitalWrite(ledPin, Arduino.LOW);  //LED off
}
rect(50, 50, 100, 100);  //draws the square
}

boolean mouseOverRect() {
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

One Comment zu “Basic Processing to Arduino Communications: part 2”

  1. Nachlese zum ersten Arduino-Basteln « Bischofs Blog

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



Leave a Reply