Firmata/ARD/Proc handling digital pushbuttons

I'm at a loss. I've used Firmata to succesfully interface with processing and different sets of sensors, but I just can't get a stupid pushbutton to work. I'm doing something wrong either in syntax or serial communication, but it's still weird.

I wanted three pushbuttons to toggle different sensors, but no digital input button seems to work in Proc/Firmata. When I load up the buttonsketch in Arduino itself (http://www.arduino.cc/en/Tutorial/Button) It works a charm, so my breadboardschematic and arduino are working fine. However, when I set the exact same thing up in processing with Firmata loaded, I notice that LED pin 13 can't even be toggled on and off and it seems that the loop is doing weird things (making the RX led blink erratic instead of 13, as if the if loop isn't checking properly).

this is my code:

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;
PImage backgroundIMG;

// Images must be in the "data" directory to load correctly


// Kleur blauw (R G B)
color blauw = color(0,0,180);
color groen = color(0,180,0);
color zwart = color(0,0,0);
color grijs = color(200,200,200);


/////////////////////////////////////////////////////////////////
//////////////////SETUP//////////////////////////////////////////
/////////////////////////////////////////////////////////////////

void setup() {
  // Grootte van je window
  size(800, 600);
  background(255);
  // Geen stroke
  noStroke();
  backgroundIMG = loadImage("oogkas.jpg");

  

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

  // Gebruik pin 0 als INPUT
  arduino.pinMode(0, Arduino.INPUT);
  arduino.pinMode(1, Arduino.INPUT);
  arduino.pinMode(7, Arduino.INPUT);
  arduino.pinMode(13, Arduino.OUTPUT);
  arduino.pinMode(3, Arduino.INPUT);
  arduino.pinMode(12, Arduino.INPUT);
  arduino.pinMode(11, Arduino.INPUT);
  arduino.pinMode(10, Arduino.INPUT);

//  imgSleep = loadImage("low.jpg"); 
//  imgWake = loadImage("high.jpg");
}


////////////////*********************////////////////////////////
//////////        DRAW     //////////////////////////////////////
////////////////*********************////////////////////////////

void draw() {
background(backgroundIMG);


if (arduino.digitalRead(7) == Arduino.HIGH) {
  print("BUTTON PRESSED");
arduino.digitalWrite(13, Arduino.HIGH);
} else {
arduino.digitalWrite(13, Arduino.LOW);
}



//  toggleSensors();
//  sonicDist();
//  lightSensor();
//  infraDist();
//  delay(20);
}

I've triend nearly all digital inputs, but no pushbutton seems to give the correct result.Also, if i change 'if (arduino.digitalRead(7) == Arduino.HIGH) ' to Arduino.LOW, it will say 'button pressed', but i can't seem to change from HIGH to LOW...

  arduino.pinMode(0, Arduino.INPUT);
  arduino.pinMode(1, Arduino.INPUT);

Pins 0 and 1 are the serial port pins. You can’t even think about using them with Firmata.

First of, thanks for replying. Secondly, I don't understand your reply. The analog 0-pin is succesfully used with the ultrasonic sensor. It does a good job at it as well. Also, you have to forgive me for using both pins with Firmata - apparantly that is not done - but, why?

The analog 0-pin is succesfully used with the ultrasonic sensor. It does a good job at it as well.

Fine. The pinMode command is not used for analog pins, though. Since they are read-only, there is no need to explicitly set them to be input.

Therefore

  arduino.pinMode(0, Arduino.INPUT);
  arduino.pinMode(1, Arduino.INPUT);

is setting the digital pins to input mode. You can't do this, because pins 0 and 1 are the Arduino end of the serial port, and it is the serial connection that Firmata is using to talk to the PC.

Oh, well, I wasn't aware of that. I commented out the lines immediately. But the problem at hand is still there - I can't get a simple pushbutton working, or in any way change the digital input from LOW to HIGH through Firmata. Perhaps this is a pin-issue as well?

In the Processing sketch, you are not enabling the pull-up resistors on the Arduino, for the pin(s) that have switches attached. Do you have external pull-up or pull-down resistors attached with the switches?

Using a simple read switch state sketch, on the Arduino, can you read the switch state consistently?

(I know. Nothing but more darn questions...)

No no, i'm glad you ask them. It helps. So yes, I have a resistor connected exactly as shown here: http://www.arduino.cc/en/Tutorial/Button - this is really everything I want to achieve in processing/firmata right now, simply because i cannot get the button to work at all.

Curious part is, when i use the code with the button sketch mentioned above (the link) and load it directly on arduino, it works a charm with the built in led 13 of arduino lighting when the button is pressed.

in the meantime, im reading this to understand the inner workings a bit better. http://sheepdogguides.com/arduino/FA1inp1.htm

So to summarize, I seem to be unable to issue commands via a physical buttonpress. I want to 'enable' a sensor with a corresponding button. In a clean button Arduino Sketch, I can toggle something, such as a led, with a pushbutton. With StandardFirmata loaded on the Arduino, and using processing with this code (i revised it once more), i cannot seem to switch something AT ALL. In arduino you could toggle a LED for instance if a digitalPin Read HIGH for instance. I cannot get the pin to HIGH in Firmata, however.

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


Arduino arduino;
PImage backgroundIMG;




int state = Arduino.LOW;



/////////////////////////////////////////////////////////////////
//////////////////SETUP//////////////////////////////////////////
/////////////////////////////////////////////////////////////////

void setup() {

  size(800, 600);

  background(255);

  noStroke();
  backgroundIMG = loadImage("oogkas.jpg");

  


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


  arduino.pinMode(2, Arduino.INPUT);
  arduino.pinMode(13, Arduino.OUTPUT);

  arduino.pinMode(12, Arduino.INPUT);
  arduino.pinMode(11, Arduino.INPUT);
  arduino.pinMode(10, Arduino.INPUT);


}





////////////////*********************////////////////////////////
//////////        DRAW     //////////////////////////////////////
////////////////*********************////////////////////////////

void draw() {
  delay(10);
background(backgroundIMG);



int sensorValue = arduino.digitalRead(2);
  if(state != sensorValue) {
    state = sensorValue; 
    arduino.digitalWrite(13, sensorValue); // turns the LED on or off
    println(sensorValue);
  }

Just grasping at straws at this point, but I do have one more question.

There are several Firmata sketches, that do different things. Which one did you load on the Arduino?

StandardFirmata found in the Arduino examples.

If you load the SimpleDigitalFirmata.pde file to the Arduino, does it make any difference?

I probably should have asked this a while ago, but, which Arduino are you using?

Well, frankly, at

void loop()
{
byte i;

for (i=0; i<TOTAL_PORTS; i++) {
outputPort(i, readPort(i));
}

while(Firmata.available()) {
Firmata.processInput();
}
}

an error occurs

/Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata/Boards.h: In function ‘void loop()’:
/Applications/Arduino.app/Contents/Resources/Java/libraries/Firmata/Boards.h:270: error: too few arguments to function ‘unsigned char readPort(byte, byte)’
SimpleDigitalFirmata:54: error: at this point in file

this is getting odd. My arduino is a deumilanove with atmega 328. It has a replaced chip though…

Same error at AllInputsFirmata, but not SimpleAnalogFirmata.

Which version of the Arduino IDE are you using? What version of Processing? I'm guessing that there is a mis-match there.

its version 1.2.1 of processing and 0021 for arduino.

In Boards.h, readPort was modified to take two arguments. Unfortunately, no one bothered to update the sketches.

I would seriously consider junking Firmata, and writing my own protocol, if I were you.

Wish I could, but as you might’ve noticed, I’m not that good with code. Isn’t there another protocol out there?

Just an update, and I'm not sure if this is the solution, but it fixed the problem for me. PaulS was right, there was a certain miscommunication between the deumilanove (a friend of mine tested his diecimila, which worked fine) Firmata and Proc - but it was typically an issue in the current version of StandardFirmata found in the Arduino examples that disrupted digitalReads to digital pins (used for handling pushbuttons :) ).

However, thank god or any other holy spirit, this version here fixed it for me. http://sourceforge.net/projects/firmata/files/ (REV 334)

to recap: the example StandardFirmata has a weird way of handling digitalRead's. Be careful with it when using a Deumilanove, and use the above link to avoid problems and hairpulling.

PaulS, thanks for your help.