Hi,
I am trying to write a code that changes the duty cycle of each pulse by 25%. Basically it is the fading example but without a delay at the end and a different frequency. When I test my code using an oscilloscope the pulses to change their duty cycle properly. I'm not sure if I wrote my code wrong or not, hopefully someone can help! Here is my code:
/*
This code is designed to output a square wave at 60 Hz on pins 5 and 6
Date: March 19, 2014
Authour: Edward White
Version: 2
*/
int p1 = 3; //pin 3 is the first output
int p2 = 9; //pin 9 is the second output
// The setup routine runs once when you press reset:
void setup() {
//declare pin 3 and 9 to be outputs
pinMode(p1,OUTPUT);
pinMode(p2,OUTPUT);
// set output frequency to 490.2 Hz
TCCR1B = TCCR1B & 0b11111000 | 0x03;
TCCR2B = TCCR2B & 0b11111000 | 0x04;
}
// The loop routine will run forever:
void loop() {
//increase pin 3 from 0 to 98.8% duty cycle:
for(int dutyCycle = 0 ; dutyCycle <=252; dutyCycle +=63){
// sets the value (range from 0 to 252):
analogWrite(p1, dutyCycle);
}
// decrease pin 3 from 98.8% to 0% duty cycle:
for(int dutyCycle = 252 ; dutyCycle >=0; dutyCycle -=63) {
//sets the value (range from 252 to 0):
analogWrite(p1, dutyCycle);
}
//increase pin 9 from 0% to 98.8% duty cyle:
for(int dutyCycle = 0 ; dutyCycle <=252; dutyCycle +=63){
// sets the value (range from 0 to 252):
analogWrite(p2, dutyCycle);
}
// decrese pin 9 from 98.8% to 0% dutycycle:
for(int dutyCycle = 252 ; dutyCycle >=0; dutyCycle -=63) {
//sets the value (range from 252 to 0)
analogWrite(p2, dutyCycle);
}
}
I'm not getting any errors when I verify the code.
Thanks,
Ed
Sorry, I should have gave that a read over before I submitted it I guess.
My problem is that the duty cycle does not change properly, and that instead of pin 3 outputting followed by pin 9, both pins are outputting at the same time. Let me know if you need more info.
Yes they will.
The changes you are making to the duty cycle are so fast you do not have time to see them change.
The PWM defaualt frequency is way slower than the time it takes to go once round the loop.
Grumpy_Mike:
Yes they will.
The changes you are making to the duty cycle are so fast you do not have time to see them change.
The PWM defaualt frequency is way slower than the time it takes to go once round the loop.
The full cycle of pulse width changes should happen in 16 ms, or 1 cycle of 60 Hz. If I change the seconds per division on the oscilloscope I should be able to see the changes if I stop the oscilloscope. When I do this both pin 3 and pin 9 are active. Is there an error in my code that causes this?
I could be wrong, but I think I set the output frequency of pin 3 and pin 9 to 490.2 Hz, so one pulse every 2 milliseconds. Then I set each pin to output a train of pulses each one 25% wider than the previous pulse until 100% is reached. Then the pulses decrease in width by 25% until it reaches 0%. So, that would be 8 pulses in total each at 490.2 Hz, or 2 milliseconds each. Therefore the total train of pulses from 0% - 100% - 0% should 16 milliseconds, the same amount of time as one cycle at 60 Hz. What I want is for pin 3 to do this followed by pin 9 doing the exact same thing. Does this clear it up?
Read reply #3 again and try and understand what I am saying.
You will not see any change because you change the values too quickly.
Add some delays in each for loop to see what I mean.
Hi, are you expecting a phase difference between the two output pins?
Can you draw/CAD picture jpg, png or pdf, an idea of what you want out of the two outputs and how they are related.
I think you are saying you see both outputs in phase?
Grumpy_Mike:
You seem to think the code will pause until the PWM cycle has completed. It won't
Oh! So I need to insert a delay the length of time that it takes each loop to execute? That makes sense, I assumed that the code would wait until the function was executed. That makes sense why the output was wrong. Thanks a lot!
So I just made some changes to my code. I think this should make a big difference!
/*
This code is designed to output a square wave at 60 Hz on pins 5 and 6
Date: March 21, 2014
Authour: Edward White
Version: 3
*/
int p1 = 3; //pin 3 is the first output
int p2 = 11; //pin 11 is the second output
// The setup routine runs once when you press reset:
void setup() {
//declare pin 3 and 9 to be outputs
pinMode(p1,OUTPUT);
pinMode(p2,OUTPUT);
// set output frequency to 245.1 Hz
TCCR2B = TCCR2B & 0b11111000 | 0x05;
}
// The loop routine will run forever:
void loop() {
//increase pin 3 from 0 to 98.8% duty cycle:
for(int dutyCycle = 0 ; dutyCycle <=252; dutyCycle +=63){
// sets the value (range from 0 to 252):
analogWrite(p1, dutyCycle);
delayMicroseconds(2040);
}
// decrease pin 3 from 98.8% to 0% duty cycle:
for(int dutyCycle = 252 ; dutyCycle >=0; dutyCycle -=63) {
//sets the value (range from 252 to 0):
analogWrite(p1, dutyCycle);
delayMicroseconds(2040);
}
//increase pin 9 from 0% to 98.8% duty cyle:
for(int dutyCycle = 0 ; dutyCycle <=252; dutyCycle +=63){
// sets the value (range from 0 to 252):
analogWrite(p2, dutyCycle);
delayMicroseconds(2040);
}
// decrese pin 9 from 98.8% to 0% dutycycle:
for(int dutyCycle = 252 ; dutyCycle >=0; dutyCycle -=63) {
//sets the value (range from 252 to 0)
analogWrite(p2, dutyCycle);
delayMicroseconds(2040);
}
}
With fixed delays like that your overall timing is going to ignore the time needed to execute the code so you can't control the frequency exactly. If you're trying to generate an accurate 60Hz signal, that will be a problem. One way round it is to calculate the required time for each pwm change in terms of millis() or micros() values, and then poll the clock repeatedly until the required time arrives.
PeterH:
With fixed delays like that your overall timing is going to ignore the time needed to execute the code so you can't control the frequency exactly. If you're trying to generate an accurate 60Hz signal, that will be a problem. One way round it is to calculate the required time for each pwm change in terms of millis() or micros() values, and then poll the clock repeatedly until the required time arrives.
In my last code post I changed my output frequency to 245.1 Hz when I should have changed it to 980.39 Hz. If I make this change each pwm change should take 1.02 milliseconds. I'm not sure what you mean by "poll the clock repeatedly until the required time arrives." Can you explain?