🔍

Processing2Arduino – optimized firmata setup – UPDATED!

basic input/output setup with firmata and Processing

This setup offers a basic in- and output communication for Arduino to Processing with a minimalistic firmata interface.

Tested and works with under Windows with Arduino UNO | LEONARDO | MICRO PRO

PROCESSING CODE

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
 
int led_pin = 11; // testLED on pin 10!
int analog_pin = 0;  // analog input from poti on A0
 
 
void setup() {
   
  size(400,400); // setup the stage size
  background(22);
  
  // Prints out the available serial ports.
  println(Arduino.list());
  
  // initalize Arduino (  pick first COM device > need to be changed if u have more! )
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  
  // set the LED pin to output mode
  arduino.pinMode(led_pin,Arduino.PWM);
  
  for (int i = 0; i <= 13; i++){
  //  arduino.pinMode(i, Arduino.PWM);
   // arduino.digitalWrite(i, Arduino.HIGH);
  }
  
  print(" >>> --- Arduino says Hello ;) -- >>> ");
  println(arduino);
  
  
  }
 
 
void draw() {
 
    background(#FFC505); // draw background
  
   // Potentiometer INPUT --------------------------
    int potiIn = arduino.analogRead(analog_pin); // read poti value > outputs 0 - 1024
    fill( potiIn*.25 ); //map poti value from 1024 to 255 as fill color
    circle(width/2,height/2,potiIn*.25); // draw circle with dynamic radius 
  
  
    
  

  
   // LED OUTPUT --------------------------
     float r_mouse_pos_x = float(mouseX)/float(width); // get the relative mouse x position on stage
     //arduino.digitalWrite(led_pin, Arduino.HIGH); // change led brightness along mouseX
       arduino.analogWrite(led_pin, int(r_mouse_pos_x*255));
     
     println(int(r_mouse_pos_x*255));
     
}

UPDATED! Analog & Digital Communication via Firmata for Arduino/Processing

UPDATED FIRMATA CODE FOR ARDUINO



#include <Firmata.h>

byte previousPIN[TOTAL_PORTS];  // PIN means PORT for input
byte previousPORT[TOTAL_PORTS];

void outputPort(byte portNumber, byte portValue)
{
  // only send the data when it changes, otherwise you get too many messages!
  if (previousPIN[portNumber] != portValue) {
    Firmata.sendDigitalPort(portNumber, portValue);
    previousPIN[portNumber] = portValue;
  }
}

void setPinModeCallback(byte pin, int mode) {
  if (IS_PIN_DIGITAL(pin)) {
    pinMode(PIN_TO_DIGITAL(pin), mode);
  }
}

void digitalWriteCallback(byte port, int value)
{
  byte i;
  byte currentPinValue, previousPinValue;

  if (port < TOTAL_PORTS && value != previousPORT[port]) {
    for (i = 0; i < 8; i++) {
      currentPinValue = (byte) value & (1 << i);
      previousPinValue = previousPORT[port] & (1 << i);
      if (currentPinValue != previousPinValue) {
        digitalWrite(i + (port * 8), currentPinValue);
      }
    }
    previousPORT[port] = value;
  }
}


// init ANALOG inputs

byte analogPin = 0;

void analogWriteCallback(byte pin, int value)
{
  if (IS_PIN_PWM(pin)) {
    pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
    analogWrite(PIN_TO_PWM(pin), value);
  }
}




void setup()
{
  Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
 // Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.begin(57600);
}

void loop()
{

/*
  byte i;

  for (i = 0; i < TOTAL_PORTS; i++) {
    outputPort(i, readPort(i, 0xff));
  }
*/
  while (Firmata.available()) {
    Firmata.processInput();
  }

  // do one analogRead per loop, so if PC is sending a lot of
  // analog write messages, we will only delay 1 analogRead
  Firmata.sendAnalog(analogPin, analogRead(analogPin));
  analogPin = analogPin + 1;
  if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0;
}