Android Bluetooth control and a stepper motor using AccelStepper

Hi Guys,

I have this little bit of code below to move a stepper motor using the AccelStepper library and 3 push buttons - Right, Left, and OK. When I press the "Right" button the stepper moves one direction until I let go of the "Right" button. When I press the "Left" button, the stepper moves in the opposite direction until I let go of that "Left" button.

What I'm trying to do now is to change the input from push buttons to bluetooth control from my phone. I have a simple android bluetooth app that I created by using parts of an example that I found online and it also has 3 (software) buttons - Right, Left, and OK. when I press the "Right Button" the app sends the character ">" to the arduino and when I let go, it sends ".". When I press the "left" software button it sends "<" and then sends "," when released.

What I can't figure out is how to modify my hardware code to work with the software commands - ">", "<", ",", and "." and I would appreciate any and all help.

This is my section of the Hardware button code:

while (digitalRead(OkB) != 0) {
  Serial.print("(START POS) Current Position: ");   
  Serial.println(StartPoint);
while (digitalRead(RightB) == 0){
  stepper1.setSpeed(slidespeed);
  stepper1.runSpeed();
  StartPoint = stepper1.currentPosition();
  }
while (digitalRead(LeftB) == 0){
  stepper1.setSpeed(-slidespeed);
  stepper1.runSpeed();
  StartPoint = stepper1.currentPosition();
  }
}
while (digitalRead(OkB) != 0) {
Serial.print("(START POS) Start Position: ");   
Serial.print(StartPoint);    
Serial.println(" Click OK to Continue");    
}

The buttons are defined as:
OkB = Ok Button (with pull-up)
RightB = Right Button (with pull-up)
LeftB = Left Button (with pull-up)

And This is an example of the Bluetooth code. Right now it only turns on a couple LEDs connected to pins 49 and 53 on my Arduino Mega.

void loop()
{
if(Serial3.available() > 0)  
{
  Incoming_value = Serial3.read();      //Read the incoming data and store it into variable Incoming_value - Using Serial 3 on Arduino Mega
  Serial.print(Incoming_value);        //Print Value of Incoming_value in Serial monitor
  Serial.print("\n");        //New line 

  if(Incoming_value == '<')   {          
    digitalWrite(49, HIGH);  //If receive "<" turn RED LED on
    }
  else if(Incoming_value == ',') {      
    digitalWrite(49, LOW);   //If receive "," turn RED LED off
    }
  else if(Incoming_value == '>') {     
    digitalWrite(53, HIGH);   //If receive ">" turn BLUE LED on
    }
  else if(Incoming_value == '.') {     
    digitalWrite(53, LOW);   //If receive "." turn BLUE LED off
    }
}    
}

I guess what I'm struggling with is this:

  1. In the hardware switch code, the low "signal" from the push button is there continuously while the button is pressed so the Arduino can see it anytime it checks while in the Bluetooth code, the signal (for example the ">") is sent only one time.

  2. Turning on an LED is different than running a stepper motor. For the LED you just set the pin high once while for the stepper you need to constantly send the commands.

it is better to buffer the serial in
like

char incoming[4] = {};// i expect 3 char


if (BTserial.available()) {
    //int inByte = BTserial.read();
    //Serial.write(inByte);
    failcounter = 0;
    int i = 0;
    lc.clearDisplay(0);
    delay(10);
    //clear the array of char 
    memset(incoming, 0, sizeof(incoming));
    while (BTserial.available() > 0 && i < sizeof(incoming) - 1) {
      incoming[i] = BTserial.read();
      i++;
      delay(30);
    }
// do somthing

bmbbsr, thanks for your reply but I don't understand how doing what you suggest is going to help me. Would you mind please explaining it to me? I'm still learning.