Всем привет.
Хочу настроить управление шаговым мотором через IR-пульт, а именно мне нужно, чтобы мотор проворачивался, когда зажата кнопка и останавливался, когда она отпущена.
Внизу мой код, он в принципе справляется с этой задачей, но проблема в том, что при каждой итерации происходят минизадержки, поэтому мотор проворачивается рывками.
Собственно, кто-нибудь может подсказать, как убрать рывки?
#include <IRLib.h>
#include <Stepper.h>
Stepper stepper (32, 8, 10, 9, 11); //STEPPER
#define RIGHT 0xff906f
IRrecv My_Receiver(2);//receiver on pin 2
IRdecode My_Decoder;//Decoder object
int state = 0;
unsigned long lastButtonTime = millis();
const int timeoutDelay = 500;
void StepperR() {
stepper.setSpeed(1000);
stepper.step(16);
}
void setup() {
Serial.begin(9600);
My_Receiver.enableIRIn(); // Start the receiver
}
void loop()
{
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if (My_Decoder.decode_type == NEC) {
Serial.println(My_Decoder.value, HEX);
if (My_Decoder.value == RIGHT)
{
StepperR();
state = 1;
lastButtonTime = millis();
Serial.print("LBT=");
Serial.println(lastButtonTime);
Serial.print("Millis=");
Serial.println(millis());
}
else if (My_Decoder.value == REPEAT && state == 1)
{
StepperR();
lastButtonTime = millis();
Serial.print("LBTP=");
Serial.println(lastButtonTime);
Serial.print("MillisP=");
Serial.println(millis());
}
}
My_Receiver.resume(); //Restart the receiver
}
// Turn off stepper...
if ((millis() - lastButtonTime > timeoutDelay) && state == 1)
{
Serial.print("Difference=");
Serial.println(millis() - lastButtonTime);
Serial.println("off");
state = 0;
}
}