I am new to Arduino programming and have spent a couple days getting this far, but cannot seem to figure out why this code is not working.
I am trying to control a stepper motor using a hobby grade remote control RX/TX.
I have an RC receiver sending an analog value from 1000 to 2000 to my Arduino. If that signal is 1000, I would like to make 1000 = -360 degrees on the stepper and 2000 = +360 degrees on the stepper.
I am receiving the correct signals from the receiver and printing them to the serial monitor, but I cant seem to control the stepper motor with this value yet. This motor simply gets stuck in the first while loop and continues to spin in 1 direction.
int ch1 = 0; // RC Reciever Channel Value
int ch1previous = 0; // RC Receiver Channel Previous Value
int PUL=7; //define Pulse pin of stepper driver
int DIR=6; //define Direction pin of stepper driver
int ENA=5; //define Enable Pin of stepper driver
void setup() {
pinMode (PUL, OUTPUT); // Stepper Driver Pulse Pin
pinMode (DIR, OUTPUT); // Stepper Driver Direction Pin
pinMode (ENA, OUTPUT); // Stepper Driver Enable Pin
pinMode(3, INPUT); // RC Reciever Pin Input
Serial.begin(9600);
}
void loop() {
ch1 = pulseIn(3, HIGH, 50000); // Read RC Reciever Channel Value
while ( ch1 > ch1previous) { // If CH1 is greater than CH1Previous run the differance
for ( int i = ch1; i < ch1previous; i++);{ // in steps to maintain the setpoint value in forwared position
digitalWrite(DIR,HIGH);
digitalWrite(ENA,HIGH); // pulsing stepper motor in forward motion
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
while ( ch1 < ch1previous) { // if CH1 is less than CH1Previous run the differance
for ( int i = ch1; i<ch1previous; i--);{ // in steps to maintain the setpoint value in reverse motion
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH); // pulsing stepper motor in reverse motion
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
Serial.print ("Channel 1: "); // print text to the serial monitor
Serial.println(ch1); // print ch1 value to the serial monitor and end line
Serial.print("CH1 Previous: "); // print text to the serial monitor
Serial.println(ch1previous); // print ch1previous value to the serial monitor and end line
ch1previous = ch1; // remember the previous value from ch1
delay(500); // just to clean up the serial monitor
}