Hi,
I have read all of the examples, for some reason Processing is unable to read my analog pins from Arduino. I can see plenty of data being written to the usb serial port, but values are always read as 0 in processing.
Can you see anything wrong with the below?
Thanks,
Aaron.
Arduino code:
#include <Boards.h>
#include <Firmata.h>
#include <FirmataConstants.h>
#include <FirmataDefines.h>
#include <FirmataMarshaller.h>
#include <FirmataParser.h>
#define red 8
#define green 9
#define blue 10
#define POT1 A0 // The potentiometer is connected to analog pin 0
#define POT2 A1 // The potentiometer is connected to analog pin 0
#define POT3 A2
int pot1Value = 0, pot2Value = 0, pot3Value = 0;
byte redValue, greenValue, blueValue = 0;
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
Firmata.printVersion();
Firmata.begin(57600);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
pot1Value = analogRead(POT1);
pot2Value = analogRead(POT2);
pot3Value = analogRead(POT3);
redValue = (int) map(pot1Value, 0, 1023, 0, 255);
greenValue = (int) map(pot2Value, 0, 1023, 0, 255);
blueValue = (int) map(pot3Value, 0, 1023, 0, 255);
analogWrite(red, redValue);
analogWrite(green, greenValue);
analogWrite(blue, blueValue);
Firmata.sendAnalog(POT1, pot1Value);
Firmata.sendAnalog(POT2, pot2Value);
Firmata.sendAnalog(POT3, pot3Value);
}
Processing code
import org.firmata.*;
import controlP5.*;
import processing.serial.*;
import cc.arduino.*;
ControlP5 cp5;
Knob ledbrightness;
Serial usbPort;
Arduino arduino;
String port;
// Arduino pots
static final int POT1 = 0;
static final int POT2 = 1;
static final int POT3 = 2;
int r, g, b = 0;
int pot1Value, pot2Value, pot3Value, prevPot1Value, prevPot2Value, prevPot3Value = 0;
color ledColor = color(0, 0, 0);
void setup() {
size(700, 400);
smooth();
noStroke();
for (String port : Serial.list()) {
if (port.contains("cu.usb")) {
println(port);
arduino = new Arduino(this, port, 57600);
}
}
cp5 = new ControlP5(this);
ledbrightness = cp5.addKnob("ledbrightness")
.setRange(0, 255)
.setValue(50)
.setPosition(100, 70)
.setRadius(50)
.setDragDirection(Knob.VERTICAL);
}
void draw() {
mapPotsToColor();
background(ledColor);
}
void mapPotsToColor() {
pot1Value = arduino.analogRead(POT1);
pot2Value = arduino.analogRead(POT2);
pot3Value = arduino.analogRead(POT3);
println(new Object[] {pot1Value, pot2Value, pot3Value});
if (pot1Value != prevPot2Value || pot2Value != prevPot2Value || pot3Value != prevPot3Value) {
r = (int)map(pot1Value, 0, 1023, 0, 255);
g = (int)map(pot2Value, 0, 1023, 0, 255);
b = (int)map(pot3Value, 0, 1023, 0, 255);
ledColor = color(r, g, b);
prevPot1Value = pot1Value;
prevPot2Value = pot2Value;
prevPot3Value = pot3Value;
}
}
void ledbrightness(int theValue) {
println("a knob event. setting background to "+theValue);
ledColor = color(theValue, 0, 0);
}