Issue with StandardFirmata and Arduino Mega 2560

Hi,

Sorry if this has been posted before, I've searched through the forums and couldn't find what I needed. I am using an Arduino Mega with Processing to create an interactive box which plays sounds when a sensor is triggered using the Minim Library.

It's all been working perfectly until I needed to use the analog pins after A5, I cannot get any values from these when I print them using Processing. Is this an issue with Firmata and the Mega? I was wondering if it isn't working beyond pin A5 because the Uno only goes up to A5? Is there a way to get around this? I want to have 15 analog sensors working ideally.

Many thanks

Which version of Firmata are you using? You can change the number of pins known to be analog pins in some versions.

I am using StandardFirmata, I think its version 2.1

The Firmata library ships with the IDE. What version of the IDE are you using? Which Firmata sketch did you upload to the Mega? What does your Processing sketch look like? The Boards.h file in the Firmata library suggests that the Mega supports analogRead on 16 pins.

I am using Arduino 1.0.5. I uploaded the StandardFirmata sketch onto my mega, here is my processing sketch:

import ddf.minim.*;
import ddf.minim.ugens.*;
import processing.serial.*;
import cc.arduino.*;
 
//this object represents your board
Arduino arduino;

//this is the minim environment
Minim minim;

AudioOutput   out;

AudioPlayer cow;
AudioPlayer sheep;
AudioPlayer unicorn;
AudioPlayer frog;
AudioPlayer dinosaur;



Oscil         osc;

Delay myDelay;

Midi2Hz midi;
 
//this is the object that plays your file
AudioPlayer player;
AudioPlayer player2;
 
//use variables for pin numbers 
int sensorPin = 0;
//led pin
int SPACE_PIN = 51;
int SPACE_PIN2 = 49;
int SPACE_PIN3 = 47;
int SPACE_PIN4 = 45;
int SPACE_PIN5 = 43;
int SPACEREDLED_PIN = 52;

//button
int photosensorPin = 7;
int photoResistorPin = 1;
int photoResistorPin2 = 2;
int cowPin = 3;
int sheepPin = 4;
int unicornPin = 5;
int frogPin = 7;
int dinosaurPin = 6;
 
//this is the minimum value from the sensor that will trigger the sound
int threshold = 300;
int photoThreshold = 150;
int photoThreshold2 = 150;
int cowThreshold = 250;
int sheepThreshold = 450;
int unicornThreshold = 250;
int frogThreshold = 250;
int dinosaurThreshold = 250;
float threshold2 = 0.5;

float photoresistor=0;
int photoresistorLed=0;
float photoresistor2=0;
int photoresistorLed2=0;

 
void setup()
{
  size(640, 200);
 
  //the arduino object needs to be created at the beginning
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[4], 57600);
 
  arduino.pinMode(SPACE_PIN, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN2, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN3, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN4, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN5, Arduino.OUTPUT);
  arduino.pinMode(SPACEREDLED_PIN, Arduino.OUTPUT);

  //initialize minim
  minim = new Minim(this);
 
  // initialize myDelay with continual feedback and audio passthrough
  myDelay = new Delay( 0.4, 0.5, true, true );

  out = minim.getLineOut();


  osc = new Oscil( 440, 0.5 );

  
   cow = minim.loadFile("moo.wav");
   sheep = minim.loadFile("baa.mp3");
   unicorn = minim.loadFile("horse.mp3");
   frog = minim.loadFile("frog.mp3");
   dinosaur = minim.loadFile("dinogrowl.wav");

}
 
void draw()
{
    size(512, 200);

  //stop led being on constantly
  arduino.digitalWrite(SPACE_PIN, Arduino.LOW);
  arduino.digitalWrite(SPACE_PIN2, Arduino.LOW);
  arduino.digitalWrite(SPACE_PIN3, Arduino.LOW);
  arduino.digitalWrite(SPACE_PIN4, Arduino.LOW);
  arduino.digitalWrite(SPACE_PIN5, Arduino.LOW);
  arduino.digitalWrite(SPACEREDLED_PIN, Arduino.LOW);
  
  //define photoresistor
  photoresistor = arduino.analogRead(0);

  
  //define photoresistor
  //potentiometer = arduino.analogRead(0);

  //photoresistor with delay osc sounds for space
  float value = map(photoresistor,200,1700,0,16000);
  osc.setFrequency(value);

  //ufo
int photoResistorValue =  arduino.analogRead(photoResistorPin);
 // println(photoResistorValue); //print it for testing purposes
  //if photoresistor is greater than 100, turn on LED  
  if (photoResistorValue > photoThreshold)
  {
    //turn on LED
    arduino.digitalWrite(SPACEREDLED_PIN, Arduino.HIGH);

  }
  
  //stars
  int photoResistorValue2 =  arduino.analogRead(photoResistorPin2);
  //println(photoResistorValue2); //print it for testing purposes
  //if photoresistor is greater than 100, turn on LED  
  if (photoResistorValue2 > photoThreshold2)
  {
    //turn on LED
    arduino.digitalWrite(SPACE_PIN, Arduino.HIGH);
    arduino.digitalWrite(SPACE_PIN2, Arduino.HIGH);
    arduino.digitalWrite(SPACE_PIN3, Arduino.HIGH);
    arduino.digitalWrite(SPACE_PIN4, Arduino.HIGH);
    arduino.digitalWrite(SPACE_PIN5, Arduino.HIGH);


  }

  //cow noise
  //read an analog value from the sensor pin
  int cowValue =  arduino.analogRead(cowPin);
  //println(cowValue); //print it for testing purposes
  //check if the reproduction is in process if not don't trigger another sound
  if ( cow.isPlaying() == false && cowValue > cowThreshold)
  {
    cow.rewind();
    cow.play();
  }
  
  //sheep noise
    int sheepValue =  arduino.analogRead(sheepPin);
  //println(sheepValue); //print it for testing purposes
  //check if the reproduction is in process if not don't trigger another sound
  if ( sheep.isPlaying() == false && sheepValue > sheepThreshold)
  {
    sheep.rewind();
    sheep.play();
  }
  
  //unicorn noise
    //cow noise
  //read an analog value from the sensor pin
  int unicornValue =  arduino.analogRead(unicornPin);
 // println(unicornValue); //print it for testing purposes
  //check if the reproduction is in process if not don't trigger another sound
  if ( unicorn.isPlaying() == false && unicornValue > unicornThreshold)
  {
    unicorn.rewind();
    unicorn.play();
  }
  
  int dinosaurValue = arduino.analogRead(dinosaurPin);
    println(dinosaurValue); //print it for testing purposes
  //check if the reproduction is in process if not don't trigger another sound
  if ( dinosaur.isPlaying() == false && dinosaurValue > dinosaurThreshold)
  {
    dinosaur.rewind();
    dinosaur.play();
  }
    //frog noise
  //read an analog value from the sensor pin
  int frogValue =  arduino.analogRead(frogPin);
  println(frogValue); //print it for testing purposes
  //check if the reproduction is in process if not don't trigger another sound
  if ( frog.isPlaying() == false && frogValue > frogThreshold)
  {
    frog.rewind();
    frog.play();
  }
                  //drawings
                  // erase the window to black
                  background( 0 );
                  // draw using a white stroke
                  stroke( 255 );
                  // draw the waveforms
                  for( int i = 0; i < out.bufferSize() - 1; i++ )
                  {
                    // find the x position of each buffer value
                    float x1  =  map( i, 0, out.bufferSize(), 0, width );
                    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
                    // draw a line from one buffer position to the next for both channels
                    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
                    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
                  }   
                  //end of drawings
}

void stop()
{
  minim.stop();
  super.stop();
}

In this sketch I have 8 photo resistors working as sensors to control an oscillator, LED's and sound files. Everything works perfectly until I go past pin A5

  arduino.pinMode(SPACE_PIN, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN2, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN3, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN4, Arduino.OUTPUT);
  arduino.pinMode(SPACE_PIN5, Arduino.OUTPUT);
  arduino.pinMode(SPACEREDLED_PIN, Arduino.OUTPUT);

This is setting the mode in the arduino instance, which passed that information on the Arduino, when needed. You are not defining the ANALOG pins as analog pins to the arduino instance. The Arduino already knows that they are analog pins, but the arduino instance does not.

Those are to turn on LED's which are connected to digital pins. I thought I had defined my photoresistors as analog pins at the top by doing this at the top:

int cowPin = 3;
int sheepPin = 4;
int unicornPin = 5;
int frogPin = 7;
int dinosaurPin = 6;

and then this in the draw:

int unicornValue =  arduino.analogRead(unicornPin);

I defined cowPin, sheepPin or unicornPin like that and they work fine, so I'm very confused why frogPin and dinosaurPin won't print in any data

I thought I had defined my photoresistors as analog pins at the top by doing this at the top:

That's the problem with computers. They really are stupid. It doesn't matter what you think. It only matters what you DO.

Try

  arduino.pinMode(cowPin, Arduino.ANALOG);

The Arduino knows that cowPin is an analog pin. The arduino instance does not.

Performing an analogRead() on a non-analog pin won't get you good results. Yes, I KNOW that the pin on the Arduino is an analog pin. In the arduino instance, it is NOT. Unless you tell the arduino instance that it IS.