Actively switching PWM?

Hello!

I need to switch a pin on the arduino from 244 hz to 48.8hz actively. I have tried using setPwmFequency, but it throws back an error. I have included a code snippet of what I have used. I can include the whole thing if needed. I have delays in the code, and the article told me that if I use this method that I will interfere with the delays. I have read some articles on this subject, but it is very confusing to me. Here is the article I where found this info. I don’t think this is what I need though.

http://www.arduino.cc/playground/Code/PwmFrequency

Thanks so much to anyone who can help!

// Regen controls
if (prethrottle > 0)
// positive?
  {
  regen = 200;
  setPwmFrequency(11, 128);
  }

if (prethrottle < 0)
// Regen?
  {
  regen = 0;
  setPwmFrequency(11, 640);
  }

Use a PWM driver. That should be more reliable.

setPwmFrequency(11, 640);

640 is not an integer power of two.
The PWM driver is fairly simple-minded,and can’t cope with non-integer power-of-two divisors.

Yep. Try 512 or 1024. That should work.

I got rid of the 640, it still kicks back an error.

error: 'setPwmFrequency' was not declared in this scope

it still kicks back an error.

First time you mentioned that. Maybe you should post your code.

Sorry! I have been inhaling solder fumes working on this darn project all day lol. Here is the whole thing.
Thanks so much for the help!

// These constants won't change.  They're used to give names
// to the pins used:
const int sensorMin = 90;
const int enableMin = 0;

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

const int analogInPin2 = A5;  // Analog input pin that the potentiometer is attached to
const int analogOutPin2 = 10; // Analog output pin that the LED is attached to

int sensorValue2 = 0;        // value read from the pot
int outputValue2 = 0;        // value output to the PWM (analog out)

const int analogInPin3 = A1;  // Analog input pin that the potentiometer is attached to

int sensorValue3 = 0;        // value read from the pot
int outputValue3 = 0;        

int sensorValue4 = 0;        // value read from the pot
int outputValue4 = 0;     


const int digitaInPin1 = 2;  // Analog input pin that the potentiometer is attached to
const int digitalOutPin1 = 4;  // Analog input pin that the potentiometer is attached to

const int regenPin = 8;
const int throttlePin = 6;

int reversedOutput2 = 0;    // reverse output
int difference = 0;         // Average
int posNeg = 0;


const int numReadings = 10;     // Averaging speed

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int readings2[numReadings];      // the readings from the analog input
int index2 = 0;                  // the index of the current reading
int total2 = 0;                  // the running total
int average2 = 0;                // the average

int additive = 0;
int addthis = 0;
float squared = 0;
int direct = 1;
int prethrottle = 0;
int throttle = 0;
int throttlefinal = 0;
int regen = 0;
int reverse = 255;
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, sensorMin, 1023, 0, 255);  
  // change the analog out value:
  
if (outputValue < 0) 
{
    outputValue = 0;
}

  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = outputValue; 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
  // ...wrap around to the beginning: 
  index = 0;                           

  // calculate the average:
  average = total / numReadings;         
           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.print(outputValue);   
  

// SECOND SET--------------------------------
  // read the analog in value:
  sensorValue2 = analogRead(analogInPin2);            
  // map it to the range of the analog out:
  outputValue2 = map(sensorValue2, sensorMin, 1023, 0, 255);  
  // change the analog out value:

if (outputValue2 < 0) 
{
    outputValue2 = 0;
}
  
  // subtract the last reading:
  total2= total2 - readings2[index2];         
  // read from the sensor:  
  readings2[index2] = outputValue2; 
  // add the reading to the total2:
  total2= total2 + readings2[index2];       
  // advance to the next position in the array:  
  index2 = index2 + 1;                    

  // if we're at the end of the array...
  if (index2 >= numReadings)              
  // ...wrap around to the beginning: 
  index2 = 0;                           

  // calculate the average2:
  average2 = total2 / numReadings;         

  
   

  // print the results to the serial monitor:
  Serial.print("\t sensor = " );                       
  Serial.print(sensorValue2);      
  Serial.print("\t output = ");      
  Serial.print(outputValue2);   
  

  
  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);

//GUN CONTROLLER===================================

  // read the analog in value:
  sensorValue3 = analogRead(analogInPin3);
   
  outputValue3 = map(sensorValue3, 0, 1023, 10000, 500); 
  //abs(outputValue3);
  
  outputValue4 = digitalRead(digitaInPin1);
  
  
  Serial.print(" \t sens3 = "); 
  Serial.print(outputValue3, DEC); 

//CALCULATIONS=====================================

difference = average - average2;

  if (difference > 0)
  {
    direct = 1;
  }
  else
  {
    direct = -1;
  }
 




difference = sq(difference);
difference = abs(difference);
difference = difference / outputValue3 * direct;


prethrottle = prethrottle + difference;

// Keep the output in the clear
if (prethrottle > 255)

  {  
    prethrottle = 255;
  }
  
if (prethrottle < -255)

  {
    prethrottle = -255;
  }
  
// Regen controls
if (prethrottle > 0)
// Positive?
  {
  regen = 255;
  setPwmFrequency(11, 128);
  }

if (prethrottle < 0)
// Regen?
  {
  regen = 0;
//  setPwmFrequency(11, 640);
  }

//Serial.print("\t diff = " );                       
//Serial.print(difference);  

  throttle = abs (prethrottle);

// Fall off, It stops

if (average <= enableMin && average2 <= enableMin)
{
  throttle = 255;
  regen = 0;
}

//Diagnostic outputs  
  Serial.print(" \t Throt = "); 
  Serial.print(prethrottle, DEC); 
    
  Serial.print("\t Cont = "); 
  Serial.println(sensorValue3, DEC); 
  
// Fix the throttle
  throttlefinal = map(throttle, 0, 255, 255, 0);

// Output that junk! 

// Lights
  analogWrite(analogOutPin2, average2); 
  analogWrite(analogOutPin, average); 
  
// Controls
  analogWrite(throttlePin, throttlefinal); //Throttle Output
  analogWrite(regenPin, regen); // Regen Output
  digitalWrite(digitalOutPin1, reverse); // Reverse Output
  analogWrite(11, 128); //Frequency Output
}

I can't see where you've included the function "setPwmFrequency"

Hmmm.... I'm trying my best to understand what to do. I keep looking at the pwmfrequency page, but I am just not making sense of it. What do you mean by including the function? Should I put void in front of it or something? I can't say I am a programmer. I did all this coding by the knowledge of a c++ class I took back in high school. Sorry for stupid questions. I am a little worried about this message in the article I am reading:

Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).

As you can see, there is indeed a delay in my code. Should I be taking a different approach to coding? is this something that should not be done on the fly?

Why must you change the PWM frequency?

OK, so basically I have this motor controller. In order to switch between regenerative braking, and accelerating I must input a 244 hz frequency into a pin to make it regen, and 48.8 hz to accelerate. In addition I must apply current to another pin, and control the acceleration with an analog signal. Why I need both I don't know. Needless to say, I hate this controller! I am soooo close to finishing this project, this is one of the final barriers standing in my way to completing this project.

Edit: here is the motor controller:

http://fastestelectricskateboard.co.uk/shop/index.php?act=viewProd&productId=11

Why not get an easier to use controller? Look around on sparkfun… they have a few choices (including shields)

I have another controller made by kelly controller that I will use if I absolutely must. This is the stock controller, and has a remote that came with it that I need to use. Using another controller would mean I would have to greatly modify the way everything mounts to the board. I am trying to keep everything as simple as possible. I can fit the arduino inside the motor controller box too which makes the whole package perfect. This is little coding issue is literally the only thing standing in my way of completing this project.

Another thought.... Why don't you use a timer interrupt to generate the signal you need? I'm not sure how you could do this, but maybe someone else on the forum could help you.

Anybody know how?

baum

So i guess this just isn't easily achievable? it would save me much circuit board space, and hassle to get this working through software.

Wait… just found something… search for “timer one arduino.” maybe you could do pseudo-PWM by using timer interrupts…

baum