PWM Controller

Hi, My pwm controller is not working. I connected my pwm to oscilloscope and my voltage waveform is flat. I think its problem with code but i cant find an error. I use arduino uno.

My code

const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;

boolean buttonState2 = false;
boolean buttonState3 = false;
boolean buttonState4 = false;
boolean buttonState5 = false;

int buttonPressed2 = 0;
int buttonPressed3 = 0;
int buttonPressed4 = 0;
int buttonPressed5 = 0;

const int ledPin =  11;

double Td = 5;
double Tl = 15;
double d = Td/(Td+Tl);
double a = 0.05 * (Td + Tl);

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  Serial.begin(9600);
}

void loop()
{
  buttonPressed2 = digitalRead(buttonPin2);  
  buttonPressed3 = digitalRead(buttonPin3);
  buttonPressed4 = digitalRead(buttonPin4);
  buttonPressed5 = digitalRead(buttonPin5);
  
  if (buttonPressed2 == HIGH) {
    delay(500); 
    Td += a;
    Tl -= a;
     Serial.println("klikles 2");
     return;
  }
  else if (buttonPressed3 == HIGH) {
    delay(500); 
    Td -= a;
    Tl += a;
    Serial.println("klikles 3");
     return;
  }
  else if (buttonPressed4 == HIGH) { 
    delay(500); 
    Td -= 0,2;
    Tl -= 0,2;
    Serial.println("klikles 4");
     return;
  }
  else if (buttonPressed5 == HIGH) {
    delay(500); 
    Td += 0,2;
    Tl += 0,2;
    Serial.println("klikles 5");
     return;
  }
  
  d = Td/(Td+Tl);
  if (d < 0) d=0;
  else if (d > 1) d=1;
 
  digitalWrite(ledPin, HIGH);
  delay(Td);
  digitalWrite(ledPin, LOW);
  delay(Tl);
 
  Serial.println("------");
  Serial.println(Td);
  Serial.println(Tl);
  Serial.println(d);
  Serial.println("------");
}

}[/quote]

where is "AnalogWrite()" in your code?

If i use analogwrite i cant change frequency. In my project i have to change duty cycle and frequency. I replaced Analogwrite with this

digitalWrite(ledPin, HIGH);
  delay(Td);
  digitalWrite(ledPin, LOW);
  delay(Tl);

why its not working ?

Not looked at your code in detail but all them Serial prints will have an adverse effect on your code.

Thanks, Is it posible to change frequency when i will use analogwrite()?

Hi Avanti

You are using floating point doubles to calculate the "on" and "off" durations of your PWM output. When you call delay(), I'm not sure how the function will handle this. Even if it casts it correctly to unsigned long, it is always going to truncate it to a whole millisecond, so the floating point arithmetic seems a bit overkill.

As a test, change your digitalWrites to this:

  digitalWrite(ledPin, HIGH);
  delay(5);
  digitalWrite(ledPin, LOW);
  delay(15);

Does this change what you see on your oscilloscope?

Regards

Ray

Avanti: Thanks, Is it posible to change frequency when i will use analogwrite()?

The hardware can do it but the analogwrite() function is limited to a single frequency.

If you want to do it properly you need to learn about the hardware timers.

There may be a better library out there somewhere.

Hackscribble: Hi Avanti

You are using floating point doubles to calculate the "on" and "off" durations of your PWM output. When you call delay(), I'm not sure how the function will handle this. Even if it casts it correctly to unsigned long, it is always going to truncate it to a whole millisecond, so the floating point arithmetic seems a bit overkill.

Hmm i dont have oscilloscope now, but i dont think it will help, i have to put there variable because i have to change frequency and duty cycle. Problem is that i see that LED is turning off and on on smaller frequency (0,5Hz) but on oscilloscope i have flat voltage weaveform anyway. Is digitalwrite() turning signal to digital and oscilloscope dont see right (analog) weaveform ? LED is digital output.

Problem is that i see that LED is turning off and on on smaller frequency (0,5Hz) but on oscilloscope i have flat voltage weaveform anyway. Is digitalwrite() turning signal to digital and oscilloscope dont see right (analog) weaveform ? LED is digital output.

If the LED is turning on and off that slowly, maybe the oscilloscope was in AC coupled input mode, instead of DC coupled. It then would not show the waveform.

I appreciate you need the variable to adjust the PWM. As I said, I suggested putting 5 ms and 15 ms in the code [u]for testing[/u] to check that you were seeing something sensible on the output, and to eliminate the possible double issue.

You could try this library for pwm freqence

http://forum.arduino.cc/index.php?topic=117425.0