serial pwm 31kh

hello : )

i wonder if anyone can help? i am trying to make my arduino run at 31kh PWM from pin 11.

i need to change the arduino serial script by adding on the 31kh pwm modification. here below are the two scripts

here is the serial script which works on my arduino :

 /*

 * Unmarshalling a Max/MSP Message 

 */



boolean getAll = false;

boolean getDigital = false;



// Unmarshalling Variables 

byte inCnt = 0;

int serialIn = -1;

int sPinNumber = -1;



// Read analogRead(analogPins[0]);

int analogPins[] = {0, 1, 2, 3, 4, 5};



// Read digitalRead(digitalPins[2]); or Write digitalWrite(digitalPins[2], HIGH or LOW);

int digitalPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};  



// Write analogWrite(analogPins[0], someValue);

int pwmPins[] = {3, 5, 6, 9, 10, 11}; 



// Serial Communication Speed

long serialBAUD[] = {9600, 19200, 28800, 57600, 115200};



int ledPin = 13;   // select the pin for the LED



/* Message Format:

 * "1 1 1" is 3 Bytes Long

 */

void serialUnmarshall()

{

  if (Serial.available())

  {

    serialIn = Serial.read();



      //Serial.println(serialIn);

      if (serialIn == 3)

      {

        getAll = false;

        getDigital = false;



        Serial.println(serialIn);

        sPinNumber = Serial.read();

        

        // Pusle Width Pin Selector

        if ((sPinNumber >= 0) && (sPinNumber < 6))

        {

             Serial.println(sPinNumber);

             //analogWrite(pwmPins[getPin(sPinNumber)], Serial.read());

             analogWrite(pwmPins[sPinNumber], Serial.read());

        }

      } else if (serialIn == 1)

      {

        getAll = true;

        getDigital = false;

        

      } else if (serialIn == 2)

      {

        getAll = false;

        getDigital = true;

        

      } else if (serialIn == 4)

      {

        getAll = false;

        getDigital = false;

        

        Serial.println(serialIn);

        sPinNumber = Serial.read();

        

        // Pusle Width Pin Selector

        if ((sPinNumber >= 0) && (sPinNumber < 11))

        {

             Serial.println(sPinNumber);

             pinMode(digitalPins[sPinNumber],OUTPUT);

             digitalWrite(digitalPins[sPinNumber], Serial.read());

        }

      }

      

      Serial.flush();

  }

}



void setup() 

{

  Serial.begin(serialBAUD[4]);

}



void loop()

{

  // Check the serial port for incoming data

  serialUnmarshall();



  if(getAll)

 {

        for (int i = 0; i < 6; i++)

        {

          Serial.print(analogRead(i)); // print the random value

          Serial.print(" "); // print a space

        }

        

        for (int i = 2; i <= 13; i++)

        {

          Serial.print(analogRead(i)); // print the random value

          Serial.print(" "); // print a space

        }

        delay(100);

        Serial.println(); // print a line-feed

 }





if (getDigital)

{

        for (int i = 0; i < 6; i++)

        {

          Serial.print(0); // print the random value

          Serial.print(" "); // print a space

        }

        

        for (int i = 2; i <= 13; i++)

        {

          Serial.print(digitalRead(i)); // print the random value

          Serial.print(" "); // print a space

        }

        delay(100);

        Serial.println(); // print a line-feed

}



  

}

and the next post is the 31kh modification.

this code needs to be added somehow to the script above? :-/

thanks for any help :slight_smile:

  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(){
}

You've already been told how to do this. Why don't you just try to run the code?

This is for another variation of serial code, the standard firmata was not what i needed after all. I re posted here in "syntax & programs" today because by your reaction to me yesterday i felt i was in the wrong forum.

All i have done is ask a question politely . i am just looking for some help.

The code to modify the frequency goes in setup(). It doesn't matter what else the sketch is doing.

i seem to have had it working after all, as i can make an led vary in brightness under the default frequency and also the modified 31k, however the problem points toward my h-bridge, it does not seem to like this high frequency. it likes the much lower default frequency but at 31k it only acts as a switch ( whereas the led will vary) so my problem was not software it seems , i am using a SN754410 at present, need to a find alternative. ::)

If the hardware you have operates at the default frequency, why do you need to find different hardware? What's wrong with using the default frequency?

i need the frequency above human hearing range.

i need the frequency above human hearing range.

Why? Can you hear the LEDs being switched on and off? Can you hear the motor controller being switched on and off?

pwm is loud at 500hz with my motor ,i need near silence. hence i am looking for a h bridge that can handle the higher frequency.

pwm is loud at 500hz with my motor ,i need near silence.

I think what you need is a different motor. Or a different way of mounting the motor.