I created these two new sets of codes for my Arduino Uno R3 using the Cytron MD10C motor driver powering a 12v wiper motor.
I require a 15khz PWM frequency for the motor per previous tests with dc speed controllers.
I've tested the delay() and millis() functions and all works well.
For now though I just want feedback on my coding. I've run both through an emulator with a simulated LED and all works well.
I've tried previous codes and have found I have the frequency off and then the duty cycle off as well which causes motor whine and awful results from over torque and none at all.
I've also attached another version using delay() only but using TCCR1A and TCCR1B coding, so not using the Timer one.h library.
Appreciate any and all advice and feedback. Patrick
So main inquiries are:
-
is my coding legit for PWM frequency and duty cycle on the two sketches using the TimerOne.h library to generate an almost 15khz frequency and 24% duty cycle.
-
is my coding legit for PWM frequency and duty cycle on the one sketch modifying timer1 by tweaking TCCR1A, TCCR1B,etc to generate an almost 15khz frequency and 24% duty cycle?
//Using Timer one.h library and delay() function only
// code below uses Timer1.initialize(67) which sets frequency to 14925hz
// (1/15000*1000000) = 66.67 but need integer so 1/67*1000000 = 14925
#include <TimerOne.h>
void setup() {
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
Timer1.initialize(67); // sets frequency to 14925hz
Timer1.pwm(9, 0); // sets pin 9 to duty cycle of 0%
}
void loop() {
for (int i=0; i < 28; i++) {
delay(898636UL); // motor A off for 15 minutes less 1.364 seconds
digitalWrite(13, HIGH); // control motor A spins clockwise
Timer1.setPwmDuty(9, 246); // sets duty cycle to 24% of max of 1023
delay(1364); // rotate at 24 percent power 1.364 seconds in clockwise direction
Timer1.setPwmDuty(9, 0); // sets duty cycle to 0% or off
}
// change direction to reset to east facing
delay(61161814UL); // motor A off for 17 hours less east rotation
digitalWrite(13, LOW); // control motor A spins anti-clockwise
Timer1.setPwmDuty(9, 246); // sets duty cycle to 24% of max of 1023
delay(38186UL); // rotate at 24 percent power 38.186842 seconds in anti-clockwise direction
Timer1.setPwmDuty(9, 0); // sets duty cycle to 0% or off
}
This version is using Timer one.h library and millis() and delay() functions:
#include <TimerOne.h>
unsigned long rotateStartMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long rotatePeriod = 898636; //15 minutes less 1.364 seconds
byte numRepeats = 0;
byte maxRepeats = 28;
void setup()
{
Serial.begin(115200); //start Serial in case we need to print debugging info
rotateStartMillis = millis(); //initial start time
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
Timer1.initialize(67); //sets frequency to 14925hz
Timer1.pwm(9, 0); //sets pin 9 to duty cycle of 0% or off
}
void loop()
{
currentMillis = millis(); //get the current time
if (numRepeats == 28)
{
byte newRepeats = 28-numRepeats;
numRepeats=newRepeats;
}
if (numRepeats < maxRepeats)
{
if (currentMillis - rotateStartMillis >= rotatePeriod) //test whether the period has elapsed
{
digitalWrite(13, HIGH); //control motor A spins clockwise
Timer1.setPwmDuty(9, 246); //sets duty cycle to 24% of max of 1023
delay(1364); //rotate at 24 percent power 1.364 seconds in clockwise direction
Timer1.setPwmDuty(9, 0); //sets duty cycle to 0% or off
numRepeats ++;
rotateStartMillis = millis(); //resets start time
}
}
// change direction to reset to east facing
if (numRepeats == 28)
{
delay(61161814UL); //motor A off for 17 hours less east rotation
digitalWrite(13, LOW); //control motor A spins counter-clockwise
Timer1.setPwmDuty(9, 246); //sets duty cycle to 24% of max of 1023
delay(38186UL); //rotate at 24 percent power 38.186842 seconds in counter-clockwise direction
Timer1.setPwmDuty(9, 0); //sets duty cycle to 0% or off
rotateStartMillis = millis(); //resets start time
}
}
This version is not using the TimerOne.h library and just modifying the TCCR1A and TCCR1B,etc and using delay() functions only and still using the two const byte definitions because I haven't gotten around to eliminating them: everything good?
const byte PWM_PIN =9; // The Arduino UNO R3 pin connected to the PWM pin Cytron MD10C
const byte DIR_PIN =13; // The Arduino UNO R3 pin connected to the DIR pin Cytron MD10C
void setup() {
TCCR1A = _BV(COM1A1) | _BV(WGM11); // Enable the PWM output OC1A on digital pins 9
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // Set fast PWM and prescaler of 8 on timer 1
ICR1 = 1066; // Set the PWM frequency to 15Khz: 16MHz/15000 = 1066
OCR1A = 0; // Set the duty-cycle to off
pinMode(PWM_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void loop() {
for (int i=0; i < 28; i++) {
delay(898636UL); // motor A off for 15 minutes less 1.364 seconds
digitalWrite(DIR_PIN, HIGH); // control motor A spins clockwise
OCR1A = 256; // Set the duty-cycle to 24% power
delay(1364); // rotate at 24 percent power 1.364 seconds in clockwise direction
OCR1A = 0; // Set the duty-cycle to off
}
// change direction to reset to east facing
delay(61161814UL); // motor A off for 17 hours less east rotation
digitalWrite(DIR_PIN, LOW); // control motor A spins anti-clockwise
OCR1A = 256; // Set the duty-cycle to 24% power
delay(38186UL); // rotate at 24 percent power 38.186842 seconds in anti-clockwise direction
OCR1A = 0; // Set the duty-cycle to off
}
