AnalogRead not returning an int value

Hi,
I have to read the value from a flex sensor. Both hardware and software are OK cause they work perfectly on another Mac, the point is that on my computer the Arduino doesn’t return an int value when I use AnalogRead.
Instead, I get a lot of òé@#[å and other useless symbols: it looks like when your font doesn’t support some chars…).

I have Windows Vista btw, and Arduino Diecimila.

Here’s the code:

/* Copyright (C) 2008 Free Software Foundation.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * See file LICENSE.txt for further informations on licensing terms.
 */

/* Supports as many digital inputs and outputs as possible.
 *
 * Written by Hans-Christoph Steiner <hans@eds.org>
 */
#include <Firmata.h>

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

char stupidoAnalog;

void analogWriteCallback(byte pin, int value)
{
  pinMode(pin,OUTPUT);
  analogWrite(pin, value);
}

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;
    Firmata.sendDigitalPort(portNumber, portValue); 
  }
}

void setPinModeCallback(byte pin, int mode) {
  if(pin > 1) { // don't touch RxTx pins (0,1)
    pinMode(pin, mode);
  }
}

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

  if(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;
  }
}

void setup()
{
  Firmata.setFirmwareVersion(0, 1);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.begin();
}

void loop()
{
  outputPort(0, PIND &~ B00000011); // pins 0-7, ignoring Rx/Tx pins (0/1)
  outputPort(1, PINB); // pins 8-13
  while(Firmata.available()) {
    Firmata.processInput();
  }
  for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
    Firmata.sendAnalog(analogPin, analogRead(analogPin));
  }
}

Of course Processing is not receiving an int, and doesn’t recognize any value…

The other funny thing is that this next code works… @_@

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor's on

void setup() {
  Serial.begin(9600);
}

void loop() {
  if ((millis()% 1000)==0){
    analogValue = analogRead(analogPin);      // read the analog input
    Serial.println(analogValue);
  }
}

Anyone can help me? I really don’t understand where the problem is ç_ç

Sounds like the sketch code with "Firmata" is not sending the analog value as a decimal number, but sending raw bytes to the serial link. When you use "Serial.println", the binary value of the vairiable "analogValue" is converted to a string of decimal digits and transmitted in ASCII (i.e. from one to four digits, followed by carriage return/line feed).

Thanks for the informations. But I still don't understand why the same code+hardware works on my friend's Mac and doesn't work on my PC @_@

Ah, you didn't say that very clearly in the first posting! In that case, check your baud rate settings in the serial monitor. Your simple sketch code clearly sets 9600 baud, but it's not clear what baud rate is needed in the "Firmata" version. Wrong baud rates can give the symptoms that you showed, where the characters look like they're jumbled symbols.

Thanks! I had a little improvement: now I get çòùàòùè+òèàùòèùàç°ç°è570, where 570 is my flex value. So, well... better than nothing, but of course it still doesn't work :(

I suppose the Serial.println works ok and prints the 570 value, but I still have the Firmata library printing random symbols (and, btw, I can find no lines where it's said to Firmata to print something!!).

Any suggestions?

I don't understand why this code should behave differently between Mac and PC, but I've never used "Firmata". Does anyone else, who's used Firmata, know what's going on?

I was wondering... is there any other method to pass my flex sensors value to Processing? I'm using that library for this purpose, but maybe I don't need it and I don't even know that :P

PS: thank you so much for helping me!!

I don't know Processing! Can you make it read a line of text from the serial port? If so, you could use your simpler sketch code and just get Processing to read the few decimal digits that the Arduino is sending. Then, just convert the ASCII string to whatever Processing uses for numeric data.

Firmata uses a binary protocol with many different message types, packing up your analog values inside of a packet. If you want to read its messages in Processing, use the Arduino library for Processing: http://www.arduino.cc/playground/Interfacing/Processing

I'm already using it ù_u The problem is not Processing, it's the Arduino AND the Firmata library which, for some odd reason, doesn't work well on my PC. If I read analog values in a simple way (I posted the code in my first post) it works, but when I use Firmata (and I need it to pass values to Processing), it just returns strange values that I cannot use...