Hi all
I am trying to control a stepper motor using an IR remote control the problem is when any button is pressed the HEX code produced is only momentary, it is repetitive though.
The sketch I have cobbled together from other peoples work is a bit clunky and will not latch the motor on, as by the time it has started to move, the HEX code has gone and the motor stops.
Is there a way to latch the motor on while it is receiving the correct HEX code?
I have pasted the code below and would appreciate any help.
Many Thanks
#include <IRremote.h>
const int RECV_PIN = 9; // the pin where you connect the output pin of the IR receiver
const byte Step = 2; // one step per change
const byte Dir = 3; // the direction
const byte Enable = 4; // turn EasyDriver off when not turning
const byte MS1 = 7; // 1/4 the stepping rate 1
const byte MS2 = 8; // 1/8 the stepping rate 2
const byte ledPin = 13; // led plugged to digital pin 13
const byte button_slow = 6; // button plugged to digital pin 5
const byte button_fast = 5; // button plugged to digital pin 6
bool blinking_1 = false; //defines when blinking should occur
bool blinking_2 = false; //defines when blinking should occur
unsigned long blinkInterval_1 = 10; // number of milliseconds for blink
unsigned long currentMillis_1; // variables to track millis()
unsigned long previousMillis_1;
unsigned long blinkInterval_2 = 50; // number of milliseconds for blink
unsigned long currentMillis_2; // variables to track millis()
unsigned long previousMillis_2;
// variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int LEDstate = 0; // “0” is off, “1” is on.
IRrecv irrecv(RECV_PIN);
decode_results results;
#define Fast_CW 0xFFA857 // code received from button Vol+ 0xFFA857
#define Fast_CCW 0xFFE01F // code received from button Vol- 0xFFE017
#define Slow_CW 0xFFE21D // code received from button CH+ 0xFFE21D
#define Slow_CCW 0xFFA25D // code received from button CH- 0xFFA25D
#define Sleep_Mode 0xFF629D // code received from button CH 0xFF629D
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
//irrecv.blink13(true);
pinMode(Dir, OUTPUT);
pinMode(Step, OUTPUT);
pinMode(Enable, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(RECV_PIN, INPUT);
pinMode(button_slow, INPUT);
pinMode(button_fast, INPUT);
digitalWrite(Enable, LOW);
}
void loop() {
if (irrecv.decode(&results)) {
if (results.value == Fast_CW || results.value == 0x431E009D || results.value == 0x7A33ACF3) {
digitalWrite(Enable, LOW); //takes the motor out of sleep mode
digitalWrite(Dir, LOW); //sets the direction to clockwise
blinking_1 = true; // start blinking_1
Serial.write("Fast Clockwise\n");
delay (10);
digitalWrite(Enable, HIGH);
} else if (results.value == Fast_CCW || results.value == 0x421DFF0A || results.value == 0x7933AB60) {
digitalWrite(Enable, LOW);
digitalWrite(Dir, HIGH);
blinking_1 = true; // start blinking_1
Serial.write("Fast Counter Clockwise\n");
delay (10);
digitalWrite(Enable, HIGH);
} else if (results.value == Slow_CW || results.value == 0x1C1B84A3 || results.value == 0xEB3B1A99) {
digitalWrite(Enable, LOW);
digitalWrite(Dir, LOW);
blinking_2 = true; // start blinking_2
Serial.write("Slow Clockwise\n");
delay (10);
digitalWrite(Enable, HIGH);
} else if (results.value == Slow_CCW || results.value == 0x6EBDA732 || results.value == 0x506F7EC2) {
digitalWrite(Enable, LOW);
digitalWrite(Dir, HIGH);
blinking_2 = true; // start blinking_2
Serial.write("Slow Counter Clockwise\n");
delay (10);
digitalWrite(Enable, HIGH);
} else if (results.value == Sleep_Mode || results.value == 0x82727700 || results.value == 0x506F7EC2) {
digitalWrite(Enable, HIGH);
Serial.write("Sleep Mode\n");
}
// Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
// this code steps the motor fast
if (blinking_1) {
currentMillis_1 = millis(); // better to store in variable, for less jitter
if ((unsigned long)(currentMillis_1 - previousMillis_1) >= blinkInterval_1) { // enough time passed yet?
digitalWrite(Step, !digitalRead(Step)); // shortcut to make a step
previousMillis_1 = currentMillis_1; // sets the time we wait "from"
}
} else {
digitalWrite(Step, LOW); // force Step off when not blinking_1
}
int reading_1 = digitalRead(button_fast);
delay(0); // crude de-bouncing
if (reading_1 == LOW) // buttons with pull-up are pressed when LOW
blinking_1 = true; // start blinking_1
else
blinking_1 = false; // stop blinking_1
// REPEAT ABOVE
// this code steps the motor slow
if (blinking_2) {
currentMillis_2 = millis(); // better to store in variable, for less jitter
if ((unsigned long)(currentMillis_2 - previousMillis_2) >= blinkInterval_2) { // enough time passed yet?
digitalWrite(Step, !digitalRead(Step)); // shortcut to toggle the LED
previousMillis_2 = currentMillis_2; // sets the time we wait "from"
}
} else {
digitalWrite(Step, LOW); // force Step off when not blinking_2
}
int reading_2 = digitalRead(button_slow);
delay(0); // crude de-bouncing
if (reading_2 == LOW) // buttons with pull-up are pressed when LOW
blinking_2 = true; // start blinking_2
else
blinking_2 = false; // stop blinking_2
}