Firmata PWM tweak.

hello

I am hoping to raise the frequency of the PWM on the arduino.
I use standard firmata for serial between max and arduino.

i have seen a tweak here below, but wonder is there a special way to merge this code into the standard firmata arduino code?

Thanks for any help on this : )

PWM_Timer2_31kHz

set Timer2 Frequency to 31kHz

/**************************************************************************************************
*
*   PWM_Timer2_31kHz_00
*
*   Version:      00 - Mai 2010
*   Author:       Tom Pawlofsky www.caad.arch.ethz.ch tom-DOT-pawlofsky-AT-arch-DOT-ethz-DOT-ch
*
*   Desc:         change prescaler of Timer 2 to have 31kHz Frequency at pin 3 and 11
*
***************************************************************************************************/


int pinA = 3; // pin 3 and 11 are PWM output controled by Timer2
int pinB = 11; // connect pinA/B to H-Bridge

void setup(){
        //__________________________________TIMER2_for_Motor_PWM_________________________________
        // set TIMER2 for PWM 32 Hz
        //
        // clear all prescaler bits in TCCR2B = the last 3 Bits
        // leave other bits as set by arduino init() in wiring.c
        byte mask = B11111000;
        TCCR2B &= mask; // TCCR2B is now xxxxx000
        //
        // set CS22:20 in TCCR2B see p 156 of datasheet
        TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001

        //__pinmode
        pinMode(pinA,OUTPUT);
        pinMode(pinB,OUTPUT);

        //
        analogWrite(pinA,128); // 50% Duty
        analogWrite(pinB,32); //12.5 % Duty
}

void loop(){
}

here below is the standard firmata code i am trying to merge the above code with. Do i just paste the above code at the bottom? thanks for help : )

standard firmata code wont fit in the code box. : /

Do i just paste the above code at the bottom?

No.

The first thing you do is go modify your previous posts, and put the code in a code window. Select the code, and press the # button, like you did in the original post.

Then, look at the code in the first post. There is nothing in the loop function. So, the analogWrite statements in setup are just to prove that the frequency change worked. You don't need them. You will be setting the pins by other means.

Obviously, the pin declarations and pinMode calls are not needed, either. They are already being performed in the Firmata sketch or by the application that is driving the Arduino.

That leaves just the few lines that actually adjust the PWM frequency, which go in setup().

"The first thing you do is go modify your previous posts, and put the code in a code window. Select the code, and press the # button, like you did in the original post."

The code would not fit in the insert code box. its too long.
i'll try again.

nope wont fit. :smiley:

here is the setup of standard firmata

/*==============================================================================
 * SETUP()
 *============================================================================*/
void setup() 
{
  byte i;

  Firmata.setFirmwareVersion(2, 1);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(START_SYSEX, sysexCallback);

  // TODO: load state from EEPROM here

  /* these are initialized to zero by the compiler startup code
  for (i=0; i < TOTAL_PORTS; i++) {
    reportPINs[i] = false;
    portConfigInputs[i] = 0;
    previousPINs[i] = 0;
  }
  */
  for (i=0; i < TOTAL_PINS; i++) {
    if (IS_PIN_ANALOG(i)) {
      // turns off pullup, configures everything
      setPinModeCallback(i, ANALOG);
    } else {
      // sets the output to 0, configures portConfigInputs
      setPinModeCallback(i, OUTPUT);
    }
  }
  // by defult, do not report any analog inputs
  analogInputsToReport = 0;

  Firmata.begin(57600);

  /* send digital inputs to set the initial state on the host computer,
   * since once in the loop(), this firmware will only send on change */
  for (i=0; i < TOTAL_PORTS; i++) {
    outputPort(i, readPort(i), true);
  }
}

and here is what i think you mean by adding the lines into the setup >

/*==============================================================================
 * SETUP()
 *============================================================================*/
void setup() 
{
  byte i;

  Firmata.setFirmwareVersion(2, 1);

  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
  Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
  Firmata.attach(SET_PIN_MODE, setPinModeCallback);
  Firmata.attach(START_SYSEX, sysexCallback);

  // TODO: load state from EEPROM here

  /* these are initialized to zero by the compiler startup code
  for (i=0; i < TOTAL_PORTS; i++) {
    reportPINs[i] = false;
    portConfigInputs[i] = 0;
    previousPINs[i] = 0;
  }
  */
  for (i=0; i < TOTAL_PINS; i++) {
    if (IS_PIN_ANALOG(i)) {
      // turns off pullup, configures everything
      setPinModeCallback(i, ANALOG);
    } else {
      // sets the output to 0, configures portConfigInputs
      setPinModeCallback(i, OUTPUT);
    }
  }
  // by defult, do not report any analog inputs
  analogInputsToReport = 0;

  Firmata.begin(57600);

  /* send digital inputs to set the initial state on the host computer,
   * since once in the loop(), this firmware will only send on change */
  for (i=0; i < TOTAL_PORTS; i++) {
    outputPort(i, readPort(i), true);


//__________________________________TIMER2_for_Motor_PWM_________________________________
        // set TIMER2 for PWM 32 Hz
        //
        // clear all prescaler bits in TCCR2B = the last 3 Bits
        // leave other bits as set by arduino init() in wiring.c
        byte mask = B11111000;
        TCCR2B &= mask; // TCCR2B is now xxxxx000
        //
        // set CS22:20 in TCCR2B see p 156 of datasheet
        TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001




  }
}

:slight_smile: