I am totally new in the field of electronics but with the help of lots of tutorials I managed to assemble my components and did some coding. I would like to control stepper motor (Nema 17 + driver A4988 and Arduino Uno) via remote control. So far I have managed to rotate it if I press and release a button. I need your help how to continuously rotate the motor while I am pressing the button and the motor should stop after I release it. The remote is givining me FFFFFFF's in the serial monitor while I'm holding a button.
I have put the problem part of the code in a comment section. Thank you so much in advance. If you rewrite the problem part I will be very grateful.
Best regards,
Nikolay
// testing a stepper motor with a Pololu A4988 driver board
// on an Uno the onboard led will flash with each step and IR receiver
#include <IRremote.h>
#include <IRremoteInt.h>
#include "Stepper.h"
/*----- Variables, Pins -----*/
byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 300;
byte ledPin = 13;
int pulseWidthMicros = 50; // microseconds
int millisbetweenSteps = 8; // milliseconds
int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
Serial.println("Starting StepperTest");
digitalWrite(ledPin, LOW);
delay(200);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFF629D: // UP button pressed
digitalWrite(directionPin, HIGH);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(500);
break;
case 0xFFA857: // DOWN button pressed
digitalWrite(directionPin, LOW);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(500);
break;
// ------------------------------ Problem part-------------------
case 0xFF22DD: // LEFT
while (results.value) {
digitalWrite(directionPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
}
delay(500);
break;
//----------------------------------------------------------------
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */
Thanks Robin for you reply. Unfortunately I'm newbie in the programming and I can't "translate & modify" your code to mine. "readRemote()" should be associated some how with the library operators "results.value"
this is what I came up with when adapting your part but still not working (marked in red):
// testing a stepper motor NEMA 17 with a Pololu A4988 driver board
// on an Uno the onboard led will flash with each step and IR receiver
#include <IRremote.h>
#include <IRremoteInt.h>
#include "Stepper.h"
/*----- Variables, Pins -----*/
byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 300;
byte ledPin = 13;
int pulseWidthMicros = 50; // microseconds
int millisbetweenSteps = 8; // milliseconds
int remoteVal;
int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 6
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
Serial.println("Starting StepperTest");
digitalWrite(ledPin, LOW);
delay(200);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
// case 0xFF02FD: Serial.println(" -OK-");
// case 0xFFC23D: Serial.println(" RIGHT");
[color=red]remoteVal = results.value;
if (remoteVal == 0xFF22DD) {
digitalWrite(directionPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
}
else[/color]
case 0xFF629D: // UP button pressed
digitalWrite(directionPin, HIGH);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(500);
break;
case 0xFFA857: // DOWN button pressed
digitalWrite(directionPin, LOW);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(500);
break;
// ------------------------------ Problem part-------------------
case 0xFF22DD: // LEFT
while (results.value) {
digitalWrite(directionPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
}
delay(500);
break;
//----------------------------------------------------------------
}
irrecv.resume(); // receive the next value
}
}/* --end main loop -- */