Pages: [1]   Go Down
Author Topic: Firmata problem with analogRead in Processing Library  (Read 1587 times)
0 Members and 1 Guest are viewing this topic.
Morecambe, UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 52
Laptop Shrimping and Free public Arduino workshops in Morecambe, UK
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've set up an accelerometer sensor circuit which works perfectly well when spitting analog values out to Serial (as observed with the Serial Monitor). The output looks like this as expected...

Quote
X: 521   Y: 503   Z: 601
X: 522   Y: 503   Z: 601
X: 522   Y: 502   Z: 601
X: 522   Y: 503   Z: 600

However, the moment I try to use Firmata to do the same thing (in order to establish a reliable protocol and with analogRead being called on demand) I get silly values, for example zeroes or values in the 400s which seem to wander a few points.

The buggy output looks like this...
Quote
0 0 413
0 0 413
0 0 412
0 0 413

I tried various values for baud rate although I'm pretty sure the default of 57600 is the one in use from looking at the source code for StandardFirmata.

I'm guessing there's either a substantial bug in the latest Firmata around the serial handshake or there's an interaction with the use of the digital pins to act as ground, power and self-test for the ADXL 335. Alternatively I'm totally confused as to the way the Arduino object in Processing's firmata support serves up typed values.

It's weird that essentially identical programs against the same circuit don't do the same thing. Does anyone know of other things to try? Is there a bombproof version of Firmata which I can get back to?

For the simple version I have code like this...
Code:
/*
This Code reads data from the  ADXL335 axis accelerometer
*/
int groundpin = 18;             // analog input pin 4 -- ground
int powerpin = 19;              // analog input pin 5 -- voltage
int xpin = 3;                  // x-axis of the accelerometer
int ypin = 2;                  // y-axis
int zpin = 1;                  // z-axis

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
  // Power and ground the necessary pins. Providing power to both
  // the analog and digital pins allow me to just use the breakout
  // board and not have to use the normal 5V and GND pins
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
}

void loop()
{
  // print values that are recieved from the sensors and put a tab between
  // the values
  Serial.print("X: ");
  Serial.print(analogRead(xpin));
  Serial.print("\t");
  Serial.print("Y: ");
  Serial.print(analogRead(ypin));
  Serial.print("\t");
  Serial.print("Z: ");
  Serial.print(analogRead(zpin));
  Serial.println();
  delay(200);
}


For the Firmata version I have code like this running against various versions including OldStandardFirmata and StandardFirmata as distributed with Arduino 0022 and including the latest Firmata 2.2 downloaded from firmata.org - all with the same results...

Code:
import cc.arduino.Arduino;
import processing.core.PApplet;

public class TestFirmataAdxl335 extends PApplet{

Arduino arduino;

int groundpin = 18;             // analog input pin 4 -- ground
int powerpin = 19;              // analog input pin 5 -- voltage

public void setup(){

//configure arduino
arduino = new Arduino(this, Arduino.list()[0], 57600);

arduino.pinMode(groundpin, Arduino.OUTPUT);
arduino.pinMode(powerpin, Arduino.OUTPUT);
arduino.digitalWrite(groundpin, Arduino.LOW);
arduino.digitalWrite(powerpin, Arduino.HIGH);

}

@Override
public void draw() {
int xPin=3,yPin=2,zPin=1;
System.out.print(arduino.analogRead(xPin) + " " + arduino.analogRead(yPin) + " " + arduino.analogRead(zPin));
System.out.println();
}

}

Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I had the same problem a couple of weeks ago.  I wasn't able to fix it, but here's a work around: instead of installing one of the standardFirmata programs on your arduino install either SimpleAnalogFirmata or AnalogFirmata from the examples folder. (Only one of these will work, but I can't remember which one it is.) I tried comparing the only working version to the standardFirmata, but couldn't figure out the crucial difference to make it work.

Let me know if trying this works for you too. This may be an issue we need to make the arduino team, or firmata developers aware of.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even with Firmata installed, the Processing application needs to define the pin mode for ALL the pins being referenced, telling the Arduino instance that xPin, yPin, and zPin are ANALOG pins.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Even with Firmata installed, the Processing application needs to define the pin mode for ALL the pins being referenced, telling the Arduino instance that xPin, yPin, and zPin are ANALOG pins.

Hmm, well that fixed mine today, though I swear I had those pins defined when I tried it last time.  I guess the simple analog firmatas have the pins automatically set. Thanks!

cefn, does that fix yours, too?
Logged

Pages: [1]   Go Up
Jump to: