Hi All,
I made an auto-repeat motor control system with three pots to control speed, acceleration and delay time onT3.2.
I was able to stop the auto-repeat by using the Interrupt button, but realized that there is no way to stop it if the delaytime is long.
So, I tried to studying millis for apply it to my code.. but failed finally, Because I'm a just newbie of coding.
Would you advice to me something for replacing delay1, 2, 3 in my code with millis? Or could you recommend a millis-related library?
Sorry. I know it's tricky to reading the code using a library.
Thank you so much.
I made an auto-repeat motor control system with three pots to control speed, acceleration and delay time onT3.2.
I was able to stop the auto-repeat by using the Interrupt button, but realized that there is no way to stop it if the delaytime is long.
So, I tried to studying millis for apply it to my code.. but failed finally, Because I'm a just newbie of coding.
Would you advice to me something for replacing delay1, 2, 3 in my code with millis? Or could you recommend a millis-related library?
Sorry. I know it's tricky to reading the code using a library.
Thank you so much.
Code:
#include "TeensyStep.h"
#include <EasyButton.h>
#define BTN0 17 // Button
#define BTNC 13
#define DIP1 22 // Dip Switch
#define DIP2 23
EasyButton button0(BTN0);
EasyButton buttonC(BTNC);
long aPoint = 2560;
long bPoint = 3840;
long cPoint = 7680;
int spdpot = 0;
int accpot = 0;
int dlypot = 0;
constexpr int spd = A10; // Pot
constexpr int acc = A14;
constexpr int dly = A11;
Stepper motor(3, 2); // Motor
StepControl controller;
volatile bool auto_repeat = false;
bool skipDelay = false;
void setup()
{
pinMode(DIP1, INPUT_PULLDOWN);
pinMode(DIP2, INPUT_PULLDOWN);
dlypot = analogRead(dly); //-----------------------------------DLAY MAP
dlypot = map(dlypot, 0, 1023, 0, 66000);
button0.begin();
buttonC.begin();
button0.onPressed(onButton0Pressed);
buttonC.onPressed(onButtonCPressed);
if (buttonC.supportsInterrupt())
{
buttonC.enableInterrupt(buttonISR);
}
}
void loop()
{
button0.read();
buttonC.read();
dlypot = analogRead(dly); //-----------------------------------DLAY MAP
dlypot = map(dlypot, 0, 1023, 0, 66000);
}
void onButton0Pressed() //==============button0 Pressed
{
auto_repeat = false;
skipDelay = false;
int DIP1v = digitalRead(DIP1);
int DIP2v = digitalRead(DIP2);
int var = 0;
int trn = 0;
if ((DIP1v == HIGH) && (DIP2v == LOW)) {
trn = 1;
}
else {
trn = 100;
}
while (var < trn)
{
var++;
dlypot = analogRead(dly);
dlypot = map(dlypot, 0, 1023, 0, 66000);
/////// START AUTO REPEAT!
spdpot = analogRead(spd);
spdpot = map(spdpot, 0, 1023, 0, 18000);
accpot = analogRead(acc);
accpot = map(accpot, 0, 1023, 4000, 25000);
motor
.setMaxSpeed(spdpot)
.setAcceleration(accpot);
motor.setTargetAbs(aPoint);
controller.move(motor); ////// move to a point
delay(50);
if (auto_repeat == true)
{
break;
}
if (skipDelay == true)
{
delay(dlypot); //-------------------------------------------------delay1***
}
motor
.setMaxSpeed(spdpot)
.setAcceleration(accpot);
motor.setTargetAbs(bPoint);
controller.move(motor); ///////// move to b point
delay(50);
if (auto_repeat == true)
{
break;
}
if (bPoint != 0)
{
delay(dlypot); //---------------------------------------------------delay2***
}
motor
.setMaxSpeed(spdpot)
.setAcceleration(accpot);
motor.setTargetAbs(cPoint);
controller.move(motor); ///////// move to c point
delay(50);
if (auto_repeat == true)
{
break;
}
if (cPoint != 0)
{
delay(dlypot); //---------------------------------------------------delay3***
}
skipDelay = true;
}
}
void onButtonCPressed() //=================buttonC Pressed
{
}
void buttonISR() //========================buttonC ISR
{
buttonC.read();
auto_repeat = true;
}