Arduino + Processing--need help

I have uploaded Firmata to my duemilenove and can run the blink program on Processing with no errors. I have tried the example program from pachube and can't get a value from an analogRead. So I tried modifying the blink program and the blink works, but no analogRead. Is there some obvious error in my code? I only get 0's.

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int ledPin = 13;
float myValue;

void setup()
{
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 115200);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
}

void draw()
{
  arduino.digitalWrite(ledPin, Arduino.HIGH);
  delay(1000);
  arduino.digitalWrite(ledPin, Arduino.LOW);
  delay(1000);
  myValue = arduino.digitalRead(0);
  println(myValue);
}

What's connected to analog pin 0? What results do you get from a sketch that reads and serial prints the value from analog pin 0?

I've tried connecting a thermistor and resistor with +5V and ground across the resistor network with the junction between the resistor and thermistor being connected to pin 0. I've also tried just connecting +5V to pin 0 with the same result.

That answers the first question. What about the second one?

The blink part works fine. I don't get the expected value returned to processing when doing an analogRead. Why do I get 0's? If I load the arduino with a thermistor program and serial.print, I get reasonable values back with Arduino serial monitor. With Processing, I get 0's.

OK, I found a mistake in what I posted. My code shows a digitalRead, but I had changed it from analogRead and still don't see any change.

So, forget about Processing and Firmata for a moment.

You need to verify that the hardware is set up correctly. Create a sketch to read the analog pin, Serial.print(ln) the value, and delay for 200 milliseconds.

Do you get correct values being printed? Is not, the problem is not in Processing/Firmata. It is in the hardware.

If you do get good values, then the problem might be with Firmata or Processing.

I did a test with just the arduino and serial monitor displaying an analog read back to the computer. I did not use processing for the test. I used a thermistor and as I warmed the thermistor, the value changed and as it cooled the value would begin to swing the opposite direction. I am leaning toward the problem being in Processing or Firmata. I don't know how to tell which one, though.

I did a test with just the arduino and serial monitor displaying an analog read back to the computer.

And, that code looked like?

// (Ground) ---- (10k-Resister) -------|------- (Thermistor) ---- (+5v)
//                                     |
//                                Analog Pin 0

#include <math.h>
int gndPin = 0;
int TPin = 1;
int hotPin = 2;

double Thermister(int RawADC) {
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
 Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
 return Temp;
}

void setup() {
 Serial.begin(19200);
 pinMode(gndPin, INPUT);
 pinMode(hotPin, OUTPUT);
 digitalWrite(gndPin, LOW);
 digitalWrite(hotPin, HIGH);
 pinMode(TPin, INPUT);
}

void loop() {
 Serial.println(int(Thermister(analogRead(TPin))));  // display Fahrenheit
 delay(1000);
}
pinMode(TPin, INPUT);

Setting your serial transmit pin to be an input is not recommended.

There is also this code:

 int gndPin = 0;
 pinMode(gndPin, INPUT);
 digitalWrite(gndPin, LOW);

This sets the Serial RX pin to input, and turns on the pull-up resistor.

You really should avoid using digital pins 0 and 1, unless you absolutely have to. Then, if you do, you can't also use them for serial input/output.

Just plug the 10K resistor into a +5V pin, and the other end of the voltage divider into ground. Connect the middle to an analog pin, as you are doing now.

If you want to be able to turn the voltage divider on or off, use a pin above 1.

True. I had started with the gndPin being 14 and hotPin being 16. I've gone through so many interations that I've gotten a little mixed up when setting I go back and look over it again. The reason I did it that way is because I had taken a 3 pin header and soldered a thermistor and resistor to it. I then just plug it into the arduino and run it. It's a little easier to use this way, rather than with a protoboard. This only holds true when using a single component like this one, though.

To get back on track. When using this sketch with pins 14 and 16, rather than 0 and 2, I get an analog value that changes as I heat or cool the thermistor. This works like I expect it to. When I try the processing sketch, I can get the arduino to flash the led connected to pin 13, but I cannot get it to return a value for an analog read. I have tried removing the thermistor assembly, putting it into a protoboard and then connecting ground and +5V and trying the analog read and still no cigar. The analog input doesn't even provide a floating value if I leave it unconnected. It just returns 0.

Just so we are all looking at the same thing, put the thermistor/resistor in place, make the code set pins 14 and 16 appropriately, and show some output as the temperature changes.

Then, show the Firmata sketch you load on the Arduino, and the current Processing sketch that you use to read the thermistor value, as well as the output you get.

Here is the setup as simply as I can make it for mistake-proofing. I have a thermistor and resistor connected together. I put 5VDC across them. They form a resistor network with the junction being tied to analog pin 1. I use the following code and get these results:
543
554
557
560
562
555
544
538
534
532
530
529
528
528
527
527
526

// (Ground) ---- (10k-Resister) -------|------- (Thermistor) ---- (+5v)
//                                     |
//                                Analog Pin 0
#include <math.h>

int myValue;
int Pin = 1;

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

void loop() {
  myValue = analogRead(Pin);
  Serial.println(myValue);
  delay(1000);
}

When I run the following processing code without changing my circuit, I get these results:

Stable Library

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] “COM1”
[1] “COM3”
0.0
0.0
0.0
0.0
0.0

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
float myValue;
int Pin = 1;

void setup()
{
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 115200);
//  arduino.pinMode(1, Arduino.INPUT);
}

void draw()
{
  myValue = arduino.analogRead(Pin);
  println(myValue);
  delay(1000);
}

But, analogRead does not return a float.

Other than that (which really shouldn't be a problem), I don't see anything else. Presuming, of course, that you have actually loaded the Firmata sketch on the Arduino.

Please post the sketch that is running on the Arduino when you run the Processing application.

This is standardFirmata that came with Arduino0015

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

/* 
 * TODO: add Servo support using setPinMode(pin, SERVO);
 * TODO: use Program Control to load stored profiles from EEPROM
 */

#include <EEPROM.h>
#include <Firmata.h>

/*==============================================================================
 * GLOBAL VARIABLES
 *============================================================================*/

/* analog inputs */
int analogInputsToReport = 0; // bitwise array to store pin reporting
int analogPin = 0; // counter for reading analog pins

/* digital pins */
byte reportPINs[TOTAL_PORTS];   // PIN == input port
byte previousPINs[TOTAL_PORTS]; // PIN == input port
byte pinStatus[TOTAL_DIGITAL_PINS]; // store pin status, default OUTPUT
byte portStatus[TOTAL_PORTS];

/* timer variables */
unsigned long currentMillis;     // store the current value from millis()
unsigned long nextExecuteMillis; // for comparison with currentMillis


/*==============================================================================
 * FUNCTIONS                                                                
 *============================================================================*/

void outputPort(byte portNumber, byte portValue)
{
  portValue = portValue &~ portStatus[portNumber];
  if(previousPINs[portNumber] != portValue) {
        Firmata.sendDigitalPort(portNumber, portValue); 
        previousPINs[portNumber] = portValue;
        Firmata.sendDigitalPort(portNumber, portValue); 
    }
}

/* -----------------------------------------------------------------------------
 * check all the active digital inputs for change of state, then add any events
 * to the Serial output queue using Serial.print() */
void checkDigitalInputs(void) 
{
    byte i, tmp;
    for(i=0; i < TOTAL_PORTS; i++) {
        if(reportPINs[i]) {
            switch(i) {
            case 0: outputPort(0, PIND &~ B00000011); break; // ignore Rx/Tx 0/1
            case 1: outputPort(1, PINB); break;
            case ANALOG_PORT: outputPort(ANALOG_PORT, PINC); break;
            }
        }
    }
}

// -----------------------------------------------------------------------------
/* sets the pin mode to the correct state and sets the relevant bits in the
 * two bit-arrays that track Digital I/O and PWM status
 */
void setPinModeCallback(byte pin, int mode) {
    byte port = 0;
    byte offset = 0;

    if (pin < 8) {
      port = 0;
      offset = 0;
    } else if (pin < 14) {
      port = 1;
      offset = 8;     
    } else if (pin < 22) {
      port = 2;
      offset = 14;
    }

    if(pin > 1) { // ignore RxTx (pins 0 and 1)
        pinStatus[pin] = mode;
        switch(mode) {
        case INPUT:
            pinMode(pin, INPUT);
            portStatus[port] = portStatus[port] &~ (1 << (pin - offset));
            break;
        case OUTPUT:
            digitalWrite(pin, LOW); // disable PWM
        case PWM:
            pinMode(pin, OUTPUT);
            portStatus[port] = portStatus[port] | (1 << (pin - offset));
            break;
        //case ANALOG: // TODO figure this out
        default:
            Firmata.sendString("");
        }
        // TODO: save status to EEPROM here, if changed
    }
}

void analogWriteCallback(byte pin, int value)
{
    setPinModeCallback(pin,PWM);
    analogWrite(pin, value);
}

void digitalWriteCallback(byte port, int value)
{
    switch(port) {
    case 0: // pins 2-7 (don't change Rx/Tx, pins 0 and 1)
        // 0xFF03 == B1111111100000011    0x03 == B00000011
        PORTD = (value &~ 0xFF03) | (PORTD & 0x03);
        break;
    case 1: // pins 8-13 (14,15 are disabled for the crystal) 
        PORTB = (byte)value;
        break;
    case 2: // analog pins used as digital
        PORTC = (byte)value;
        break;
    }
}

// -----------------------------------------------------------------------------
/* sets bits in a bit array (int) to toggle the reporting of the analogIns
 */
//void FirmataClass::setAnalogPinReporting(byte pin, byte state) {
//}
void reportAnalogCallback(byte pin, int value)
{
    if(value == 0) {
        analogInputsToReport = analogInputsToReport &~ (1 << pin);
    }
    else { // everything but 0 enables reporting of that pin
        analogInputsToReport = analogInputsToReport | (1 << pin);
    }
    // TODO: save status to EEPROM here, if changed
}

void reportDigitalCallback(byte port, int value)
{
    reportPINs[port] = (byte)value;
    if(port == ANALOG_PORT) // turn off analog reporting when used as digital
        analogInputsToReport = 0;
}

/*==============================================================================
 * SETUP()
 *============================================================================*/
void setup() 
{
    byte i;

    Firmata.setFirmwareVersion(2, 0);

    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
    Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
    Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
    Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
    Firmata.attach(SET_PIN_MODE, setPinModeCallback);

    portStatus[0] = B00000011;  // ignore Tx/RX pins
    portStatus[1] = B11000000;  // ignore 14/15 pins 
    portStatus[2] = B00000000;

//    for(i=0; i<TOTAL_DIGITAL_PINS; ++i) { // TODO make this work with analogs
    for(i=0; i<14; ++i) {
        setPinModeCallback(i,OUTPUT);
    }
    // set all outputs to 0 to make sure internal pull-up resistors are off
    PORTB = 0; // pins 8-15
    PORTC = 0; // analog port
    PORTD = 0; // pins 0-7

    // TODO rethink the init, perhaps it should report analog on default
    for(i=0; i<TOTAL_PORTS; ++i) {
        reportPINs[i] = false;
    }
    // TODO: load state from EEPROM here

    /* send digital inputs here, if enabled, to set the initial state on the
     * host computer, since once in the loop(), this firmware will only send
     * digital data on change. */
    if(reportPINs[0]) outputPort(0, PIND &~ B00000011); // ignore Rx/Tx 0/1
    if(reportPINs[1]) outputPort(1, PINB);
    if(reportPINs[ANALOG_PORT]) outputPort(ANALOG_PORT, PINC);

    Firmata.begin(115200);
}

/*==============================================================================
 * LOOP()
 *============================================================================*/
void loop() 
{
/* DIGITALREAD - as fast as possible, check for changes and output them to the
 * FTDI buffer using Serial.print()  */
    checkDigitalInputs();  
    currentMillis = millis();
    if(currentMillis > nextExecuteMillis) {  
        nextExecuteMillis = currentMillis + 19; // run this every 20ms
        /* SERIALREAD - Serial.read() uses a 128 byte circular buffer, so handle
         * all serialReads at once, i.e. empty the buffer */
        while(Firmata.available())
            Firmata.processInput();
        /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
         * 60 bytes. use a timer to sending an event character every 4 ms to
         * trigger the buffer to dump. */
      
        /* ANALOGREAD - right after the event character, do all of the
         * analogReads().  These only need to be done every 4ms. */
        for(analogPin=0;analogPin<TOTAL_ANALOG_PINS;analogPin++) {
            if( analogInputsToReport & (1 << analogPin) ) {
                Firmata.sendAnalog(analogPin, analogRead(analogPin));
            }
        }
    }
}

16 steps into this dialog you get around to pointing out that you are using an old version of the IDE. OK. Thanks for mentioning that up front.

Have you considered upgrading to 0016, 0017, or 0018?

Sorry for not bringing that up earlier. I’ll try a newer version and see what happens.

I am now running Arduino IDE 0018. Using Standard Firmata that came with it. Processing Version 1.2.1

The problem persists. The output remains the same as previously posted.