I am using metro library to make delays in order to do a PWM ramp-up for the motor.
The timer is set for 30 ms and i have a delay in code for 2, counter goes up to 266 so 32*266= 8192
so the whole start up should take around 8 sec. And it does it in about 40% of cases other 60% it takes just 1 sec to speed up the motor.
I cant understand what is wrong, i even added 2nd timer to see if it will trigger the check but it never does so i assume that whatever the cause is it causes all the timers to malfunction in a same way .
Board is Teensy LC
The timer is set for 30 ms and i have a delay in code for 2, counter goes up to 266 so 32*266= 8192
so the whole start up should take around 8 sec. And it does it in about 40% of cases other 60% it takes just 1 sec to speed up the motor.
I cant understand what is wrong, i even added 2nd timer to see if it will trigger the check but it never does so i assume that whatever the cause is it causes all the timers to malfunction in a same way .
Board is Teensy LC
PHP:
#include <Encoder.h>
#include <Metro.h> // Include the Metro library
Metro tbutton = Metro(30); // Instantiate an instance with 30ms timer 1000 is 1 sec
Metro ttest = Metro(29);
Encoder lefts(3, 4);
Encoder rights(6, 5); //encoders
// avoid using pins with LEDs attached
void setup() {
analogWriteFrequency(17, 1000); //Set PWM Freq to 1khz
Serial.begin(230400);
Serial.println("Table Startup:");
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(13, OUTPUT);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
digitalWrite(11, HIGH);
digitalWrite(12,HIGH);
}
volatile int pwml=0; //Speed can use byte
volatile int pwmr=0; //Speed
volatile int testl=0; //test var
volatile int testr=0; //
void loop() {
if (digitalRead(24)==LOW )
{
delay(2);
if (digitalRead(24)==LOW && tbutton.check()==1)
{
Serial.println("Up button Pressed");
digitalWrite(12, LOW);
go(); //call go function that does motor speed up
}
}
else if (digitalRead(24)==HIGH)
{
delay(2);
if (digitalRead(24)==HIGH)
{
Serial.print(".");
pwml=pwmr=0;
testl=testr=0;
digitalWrite(12, HIGH);
delay(150);
}
}
}
void level_up()
{
if(lefts.read()-rights.read()>10)
{
pwml--;
}
if(rights.read()-lefts.read()>10)
{
pwmr--;
}
}
void go()
{
if (pwml<256 and pwmr<256)
{
Serial.println("Spooling UP");
analogWrite(16, pwml++);
analogWrite(17, pwmr++);
if (ttest.check()==1)
{
testl++;
testr++;
}
if (pwml-testl>1)
{
Serial.println("////////////////////////////Corruption occured UP");
delay(2000);
}
if (pwmr-testr>1)
{
Serial.println("////////////////////////////Corruption occured UP");
delay(20000);
}
prpos(); //function to print out info to serial
// very fast spool up need to solve without delay
}
else
{
Serial.println("////////////////////Full Speed Reached/////////////////////////////////");
prpos();
}
}
void prpos()
{
Serial.print("Current Position Left = ");
Serial.print(lefts.read());
Serial.print(", Speed Left = ");
Serial.print(pwml);
Serial.print(", Right = ");
Serial.print(rights.read());
Serial.print(",Speed Right = ");
Serial.print(pwmr);
Serial.println();
}
Last edited: