Sunday, July 15, 2012

Using Arduino Firmata with Processing

Attempted to use the Standard Firmata on the Arduino Uno R2 to control 2 servos on pin 9 & 10 however it didsn't work.   It was replaced by the Analog Firmata and only the following Processing program worked.


import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int servoPinX = 10;
int servoPinY = 9;
int sensorPin = 0;
float sensorValue = 0;
int posX=0, posY=0; //servo position in degrees (0..180)

void setup()
{
 size(360, 360);

 arduino = new Arduino(this, Arduino.list()[1], 57600); //your offset may vary
 arduino.pinMode(servoPinX,4);
 arduino.pinMode(servoPinY,4);
 arduino.pinMode(sensorPin,0);
}

void draw()
{
 sensorValue = arduino.analogRead(0);
 println(sensorValue);

// read mouseX coordinate
 int newPosX = constrain(mouseX/2,40,140); // update bg & servo position if mouseX changed
// read mouseY coordinate
 int newPosY = constrain(mouseY/2,40,140); // update bg & servo position if mouseX changed

 if(newPosX != posX ||newPosY != posY)
 {
   background(newPosX);
      arduino.analogWrite(servoPinX, newPosX);
      arduino.analogWrite(servoPinY, newPosY);
   println (" newPos = " + newPosX );
   posX=newPosX; //update servo position storage variable
   posY=newPosY; //update servo position storage variable
 
 }
}

No comments:

Post a Comment