Firmata, PWN pins acting different than Digital pins in Arduino Mega

Hello guys!

I am using Standard Firmata at my board just receiving data from Processing. I am using arduino Mega due to the ouputs I need to lit some leds in sequence.

In processing, my program turns off all leds at the first function of the each frame, (similar as processing "background()") then, it checks what led needs to be turned on according to the course of the animation before turning it on.

In short, leds are behaving weird while connected to PWM pins. There is no analogWrite nor nothing. Maybe is there a way I should disable something at "standart firmata" example? Or PWM can't handle this "on/off" caused by the program?

Thanks a lot!

Bontempos~

Are the pins you are using being setup as output?
What pins numbers are you trying to use?
Does Processing have provision to tell Firmata that the pin is being used as a PWM pin?
I am not familiar with Processing, but I suspect you are missing a step in initialization of the pins.

I have used the 2560 with PWM and PyMata GitHub - MrYsLab/PyMata: A Python client class library for Interaction with Standard Firmata without issue.

Hey! Thanks for your reply!

So, I am using Standart Firmata compiled in an Arduino Mega.

Despite of the code, and all the loops it holds, what I meant was that the same routine have different effects while using HIGH to lit a LED outputting from a PWM output than from a DIGITAL output.

In processing I declare all 53 pins as outputs, at setup():

for(int i = 0; i >= OUTPUTS ; i++){
        arduino.pinMode(i,Arduino.OUTPUT);
      }

At the draw() function, again in processing, I have a clearAll() function to turn every OUTPUT LOW:

for (int i = 0; i <= OUTPUTS ; i++) {
       arduino.digitalWrite(i, Arduino.LOW);
     }

The led will turn on at:

arduino.digitalWrite(pin, Arduino.HIGH);

So. LED will turn on and look like on, if using the so called DIGITAL outputs (pins 22 to 53). All the pins from 2 to 13 (PWM) and 14 to 21 (COMMUNICATION) will cause the LED to flicker.

There are examples in Processing using the PWM by calling it with arduino.analogWrite. But that is not the case. I intended the pins to act like Digital.

Also, I have made an experiment at the clearAll() function, by reducing the looping to iterate until i<10 . And by this, I have got the first 2 ~ 9 pins to flicker less, so it looks like ON. But I can't use this workaround since I need all the pins...

:~

I am not familiar with Processing, but looking at the code, before doing an analog write, Firmata requires you to establish the mode type for the pin as PWM. The Processing Arduino library has a method to set a pin's mode: pinMode(pin, mode). For PWM you would use Arduino.PWM. Once you set the mode correctly and do an analogwrite with values from 0 (off) to 255 (full on), the led should not flicker.

If you look at the source for StandardFirmata, in order to perform an analogWrite, the pin must be set up as a PWM pin. If not setup, the write will not proceed. Take a look at void setPinModeCallback(byte pin, int mode) and void analogWriteCallback(byte pin, int value) in the StandardFirmata sketch.

Also, note that if you set a pin to OUTPUT mode, that was previously set to PWM, the pin is no longer in PWM mode. From setPinModeCallback:

case OUTPUT:
    if (IS_PIN_DIGITAL(pin)) {
      digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM
      pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
      pinConfig[pin] = OUTPUT;
    }
    break;