Problem with arduino.analogWrite() in Processing

heya'z,

I'm controlling 4 DC motors through Processing, using StandardFirmata on the Arduino.
When I test the motors with a simple sketch in Arduino, they work perfect. When I try to actuate the motors through Processing, they do nothing.

  • I have checked the syntax of the Arduino library in Processing and I believe I have called the functions correctly.
  • I have printed a list of serial ports to confirm that I am listening to the correct port.
  • Both StandardFirmata and Processing are set to 9600 baud.
  • If I change the analogWrite() to digitalWrite(pin,HIGH), I am able to turn the motors on.

Is there something I am missing when calling arduino.analogWrite() in Processing?

Help, please and thank you!
:slight_smile:

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

int motor1 = 3;
int motor2 = 5;
int motor3 = 9;
int motor4 = 10;
int pulseWidth = 200;

void setup() {
  size(100,100);
  background(0);
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 9600);
  arduino.pinMode (motor1, Arduino.OUTPUT); 
  arduino.pinMode (motor2, Arduino.OUTPUT);
  arduino.pinMode (motor3, Arduino.OUTPUT);
  arduino.pinMode (motor4, Arduino.OUTPUT); 
}

void draw() {
    arduino.analogWrite(motor1, pulseWidth);
    arduino.analogWrite(motor2, pulseWidth);
    arduino.analogWrite(motor3, pulseWidth);
    arduino.analogWrite(motor4, pulseWidth);
}

I'm controlling 4 DC motors through Processing, using StandardFirmata on the Arduino.

  • I have checked the syntax of the Arduino library in Processing and I believe I have called the functions correctly.

Prior to uploading the StandardFirmata sketch, did you spend any time looking at it? In particular, the setPinModeCallback function is interesting. There are several cases - ANALOG, INPUT, OUTPUT, PWM, SERVO, and I2C.

You are calling using the pinMode method, which triggers the callback, to set the mode of the pin to OUTPUT.

Then, you are calling the analogWrite method for the pins. Look at the analogWriteCallback method. See how it checks the mode of the pin, and does nothing for INPUT, OUTPUT, and I2C pins (yours are OUTPUT).

If you want to use the pins for PWM, you need to call the pinMode function with a different value. I'm willing to bet you can figure out what the correct value is for PWM usage.

If not, look at that available options again - ANALOG, INPUT, OUTPUT, PWM, SERVO, and I2C. Perhaps you'll need to try them all, but I'm pretty sure that sooner or later you'll figure out the correct value for PWM pins.

Ahhhhh! Thanks, Paul!
It didnt occur to me to look there.
:smiley:

So for whatever reason, calling arduino.analogWrite() in Processing doesn't appear to work.
Instead, I'm calling arduino.digitalWrite(), based on what Tim Igoe says here under "Writing your own pulseOut":
http://www.tigoe.net/pcomp/code/controllers/input-output/analog-output

So, it works!

I found that adding the following code for an OUTPUT case to the analogWriteCallback() function in the FIRMATA.StandardFirmata sketch was just as easy.....since PWM wouldn't work for me, for whatever reason.

void analogWriteCallback(byte pin, int value)
{
  if (pin < TOTAL_PINS) {
    switch(pinConfig[pin]) {
    case SERVO:
      if (IS_PIN_SERVO(pin))
        servos[PIN_TO_SERVO(pin)].write(value);
        pinState[pin] = value;
      break;
    case PWM:
      if (IS_PIN_PWM(pin))
        analogWrite(PIN_TO_PWM(pin), value);
        pinState[pin] = value;
      break;
    case OUTPUT:
      if (IS_PIN_DIGITAL(pin))
        analogWrite(pin,value);
        pinState[pin] = value;
      break;
    }
  }
}