Hi.
I am using Arduino UNO with a DM556 Micro step driver to control the Stepper motor. PUL+ --> pin 3
DIR+ --> pin 5
RC receiver --> pin 2
Using CH2 of receiver
With out the RC receiver (R9SX) everything works fine, the motor works perfectly. However I cannot figure out how to control it with RC. I tried to find a tutorial using Micro step driver but everybody appears to use Arduino Nano or some other equipment.
The code I am using makes the motor work extremely slow and only in one direction.
I'd really appreciate it if some one can point error in my code or provide me with a suitable one for my use case.
CODE:
// Define Pins
#define RCPin 2
#define DIR 5
#define STEP 3
// Set up time variables for Stepper motor
unsigned long previousMotorTime = millis();
long MotorInterval;
// Set up time variables for RC Read
volatile long Pulses = 0;
int PulseWidth = 0;
void setup() {
Serial.begin(9600);
// RC Read Setup
pinMode(RCPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RCPin), PulseTimer, CHANGE);
// Set DIR and STEP pins to OUTPUT
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
// Set initial direction (LOW = one direction, HIGH = the other direction)
digitalWrite(DIR, LOW);
}
void loop() {
digitalWrite(STEP, LOW);
// Only save HIGH pulse lengths
if (Pulses < 2000){
PulseWidth = Pulses;
}
// Stepper motor speed, bigger MotorInterval is a slower speed.
if (PulseWidth >= 1400 && PulseWidth <= 1600) {
digitalWrite(STEP, LOW); // Motor doesn't move if the joystick is near the midpoint
} else if (PulseWidth < 1400) {
digitalWrite(DIR, LOW);
MotorInterval = map(PulseWidth, 1000, 1400, 1, 25); // Map the RC signal to the motor speed in reverse when the joystick is pulled down
} else if (PulseWidth > 1600) {
digitalWrite(DIR, HIGH);
MotorInterval = map(PulseWidth, 1600, 1984, 25, 1); // Map the RC signal to the motor speed when the joystick is pushed forward.
}
// Check if the MotorInterval time has elapsed and step the motor.
unsigned long currentMotorTime = millis();
if (currentMotorTime - previousMotorTime > MotorInterval) {
digitalWrite(STEP, HIGH);
previousMotorTime = currentMotorTime;
}
}
// Function to measure the length of the pulses from the remote control
void PulseTimer() {
static long StartTime = 0;
long CurrentTime = micros();
if (CurrentTime > StartTime) {
Pulses = CurrentTime - StartTime;
StartTime = CurrentTime;
}
}