-1

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
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • 2
    Both the while conditions will go on infinitely if they are started even once. As you have put in comments `If CH1 is greater than CH1Previous run the difference`, these `while` conditions have to be replaced by `if statements`. That is, make them as `if(ch1 > ch1previous)` and `else if(ch1 < ch1previous)` . – svtag Jun 29 '17 at 14:09
  • What's the point of `;` after the for loops? Those for loops are do nothing. – gre_gor Jun 29 '17 at 14:27
  • You seem to expect the pulse to be up to 50000 micro seconds. But you are storing it into an `int` which can only store the maximum of 32767. Use `unsigned long` for `ch1` and `ch1previous`. – gre_gor Jun 29 '17 at 14:42
  • @Sma you should post that as an answer. – gre_gor Jun 29 '17 at 14:42
  • @gre_gor Okay, i will do that. – svtag Jun 29 '17 at 15:50

2 Answers2

1

Can ch1 = pulseIn(3, HIGH, 50000) ever be a negative value? if not, then that could explain this behavior, when you consider this statement:

 while ( ch1 > ch1previous) { 

and ch1previous is initialized to zero.

TomServo
  • 7,248
  • 5
  • 30
  • 47
1

Both the while conditions will go on infinitely if they are started even once. As you have put in comments If CH1 is greater than CH1Previous run the difference, these while conditions have to be replaced by if statements. That is, make them as if(ch1 > ch1previous) and else if(ch1 < ch1previous). And for loops' conditions should also be reversed, as per the if condition bounding them.

After these changes, your code will become something like this

void loop() {
    ch1 = pulseIn(3, HIGH, 50000);

    if ( ch1 > ch1previous) {
        for ( int i = ch1; i > ch1previous; i--){
            //code
        }
    }

    else if ( ch1 < ch1previous) {
        for ( int i = ch1; i<ch1previous; i++){
            //code
        }
    }

    ...
}
svtag
  • 184
  • 4
  • 12
  • Thank You! this now behaves much more like I was hoping, however I am now getting a drift on the stepper motor it very slowly moves in the CW direction even though my set point value doesn't change, any ideas? – Anthony Wilson Jul 01 '17 at 02:35