I posted the original code and I have still having issues getting analogWriteResolution. In the code below, if I change the PWM or drive Frequency off of the ideal for the Teensy 3.6 CPU the motors do not turn on. Everytime i try and change the Resolution and the corresponding variables. The system does not turn on. Any ideas?
Situation: Powering two DC Motors thru an H-Bridge
Situation: Powering two DC Motors thru an H-Bridge
Code:
// Motor A
/*
* Frequency to Match
20. kHz
+ pulse 24micro s
- pulse 26micro s
*/
//TURN ON SERIAL CONNECTION
const int enA = 10;//FTM0 Timer
const int in1 = 8;
const int in2 = 7;
// Motor B
const int enB = 3;//FTM0 Timer
const int in3 = 4;
const int in4 = 5;
// Set Up Test Variables
/* Resolution Bits = 11 , pwmValue = 2047, freq = 29296.875
* Resolution Bits = 8 (default), pwmValue = 255
*/
float Duty = 6.0;
float driveFreq = 234375.00; //or 20.0*1000 or 234375
int Res = 8;//8
int pwmValue = 255;//255 , 2047
int Max = 3;
int Counter = 0;
//time on $ time off
unsigned long Time_On = 48.5*1000; //Motors On variable long & math calc to avoid clock issues
float Time_Off = 5000; //Motors Off
void setup()
{
Serial.begin(9600);
while(!Serial){
}
analogWriteResolution(Res);
analogWriteFrequency(enA,driveFreq);
analogWriteFrequency(enB,driveFreq);
//Button Input
pinMode(11, INPUT);
pinMode(12, OUTPUT);
//Motor Outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//Setting Up Teensy PWM Output Frequencies
//https://www.pjrc.com/teensy/td_pulse.html
}
void Forward(float Duty, unsigned long Time_On, int pwmValue )
{
float Work = (pwmValue/12.00)*Duty; //.XX added to force the float calculation
pinMode(13,OUTPUT); //LED ON
digitalWrite (13, HIGH);
// Serial.println (Duty);
// Serial.println(Work);
// Turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Working Voltage Percentage out of possible range 0~255
analogWrite(enA, Work);
// Turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Set Working Voltage Percentage out of possible range 0~255
analogWrite(enB, Work );
delay (Time_On);
// Now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite (13, LOW); //LED Off
}
void loop()
{
while (digitalRead(11) != HIGH && Counter < Max)
{
Forward ( Duty, Time_On, pwmValue );
delay(Time_Off);
Counter++;
Serial.println (Counter);
}
Serial.println(Counter);
Serial.println("Work Stoppage...Max Cycles Reached.. so hard to say goodbye");
Serial.flush();
for (int i=0; i<100; i++)
{
Serial.print("\n");
}
}
*/