I want to send a variable from Processing to Arduino using the Firmata library. The technique I'm trying is to use arduino.analogWrite in Processing to send a data, then intercept that pin on the Arduino, and pass that value to a function. Seems simple, but it doesn't seem to work - I'm not sure if the Firmata library supports this type of thing.
Any tips would be appreciated!
Kim
Arduino code snippet
void analogWriteCallback(byte pin, int value)
{
if(pin == 6) {
setPosition(value); // If pin N is used, tell the scanner to go to position value. Don't send to any pins.
}
else {
pinMode(pin,OUTPUT);
analogWrite(pin, value);
}
}
Sometimes asking the question is enough to provide clarity on a problem. I wrote a test case from fresh code, and it seems to work. The problem must have been somewhere else in my project code.
This isn't terribly complex, but seems to do the trick if you need to use Firmata to write to Arduino pins from Processing while at the same time sending data using the same library.
This example uses mouse position in a Processing sketch to control RGB LED brightness on the Arduino.
Processing
// Send numeric data to Arduino through Firmata library using a reserved pin.
// The data received on the reserved pin is handled as needed
// In this example, the mouse position is sent to the Arduino and controls the brightness of an RGB LED
import processing.serial.*;
import cc.arduino.*;
int reservedPin = 6; // reserve a pin for sending data to Arduino
Arduino arduino; // Setup an arduino object to communicate with Firmata firmware
void setup()
{
size(127,127);
//println(Serial.list());
// Using Firmata library
arduino = new Arduino(this, Arduino.list()[0], 57600);
}
void draw() {
}
void mouseReleased() {
arduino.analogWrite(reservedPin, mouseX); // Send mouse X value 1-127, to Arduino using reserved pin
}
Arduino
// Send numeric data to Arduino through Firmata library using a reserved pin.
// The data received on the reserved pin is handled as needed
// In this example, the brightness of an RGB LED is controlled by the value received through the pin
#include <Firmata.h>
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int reservedPin = 6; // Incoming Firmata data on this pin is handled specially.
void setup()
{
Firmata.setFirmwareVersion(0, 1);
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); // Call this function when analog writes are received
Firmata.begin(57600);
pinMode(redPin, OUTPUT); // Setup this pin for output
pinMode(greenPin, OUTPUT); // Setup this pin for output
pinMode(bluePin, OUTPUT); // Setup this pin for output
}
void loop()
{
while(Firmata.available()) { // Handles Firmata serial input
Firmata.processInput();
}
}
// Called whenever Arduino receives an analog msg thru Firmata
void analogWriteCallback(byte pin, int value)
{
// If data is sent to reserved pin, execute code
if(pin == reservedPin) {
analogWrite(redPin, value);
analogWrite(greenPin, value);
analogWrite(bluePin, value);
}
// Otherwise, just send the pin value to the appropriate pin on the Arduino
else {
pinMode(pin,OUTPUT);
analogWrite(pin, value);
}
}