Hey ya'll,
I am an ME trying to emulate the main PCB drive signal for motor testing. I am using a Teensy 3.6 + L298N Dual H Bridge + DC Power supply to power strings of 12V DC Motors.
The code seems to function as intended well when I am just using the regular Teensy Clock and not using analogWriteResolution or analogWriteFrequency.
Does anyone have any ideas or examples of changing the resolution and frequency?
I am an ME trying to emulate the main PCB drive signal for motor testing. I am using a Teensy 3.6 + L298N Dual H Bridge + DC Power supply to power strings of 12V DC Motors.
The code seems to function as intended well when I am just using the regular Teensy Clock and not using analogWriteResolution or analogWriteFrequency.
Does anyone have any ideas or examples of changing the resolution and frequency?
Code:
/*
* Frequency to Match
20. kHz Drive Signal
6V 50% Duty cycle 12V Source
*/
// Motor A
const int enA = 10;
const int in1 = 8;
const int in2 = 7;
// Motor B
const int enB = 3;
const int in3 = 4;
const int in4 = 5;
// Set Up Test Variables
float Duty = 6; // Faster than normal Arduino Board needed to output good enough signal to emulate Brewer.
int Max = 20; //Arbitrary Number of Max
int Counter = 0;
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){
}
//Pin Set Up
pinMode(11, INPUT); //interrupt switch
pinMode(12, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
[B]//Setting Up Teensy PWM Output Frequencies
//https://www.pjrc.com/teensy/td_pulse.html
/*
analogWriteResolution(11); //11 Bit
analogWriteFrequency(3, 20000);
analogWriteFrequency(10, 20000);
*/[/B]
}
void Forward(float Duty, unsigned long Time_On )
{
unsigned long Work = (255/12)*Duty; //Math to come up with duty cycle voltage
pinMode(13,OUTPUT); //LED
digitalWrite (13, HIGH); //LED On
// 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);
//LED Off
digitalWrite (13, LOW);
}
void loop()
{
Serial.println(Counter);
if (Counter >= Max) //program reached maximum cycles
{
Serial.println(Counter);
Serial.println("Work Stoppage...Max Cycles Reached.. so hard to say goodbye");
Serial.end();
}
while (digitalRead(11) != HIGH && Counter < Max)
{
Forward ( Duty, Time_On );
delay(Time_Off);
Counter++;
Serial.println (Counter);
}
}
Last edited by a moderator: