Using HC-05 with SerialCommand.h Library

Hi,

I wrote code to use an Arduino Uno as a Motor Control unit. I'm using a DC-Motor, supplied from a Battery. 2 Relays for switching polirisation and the hall sensors of the dc motor for positioning.
The code works all fine when connecting the arduino to usb an using serial monitor to transmit my commands "a" "b" ... and for example "auto 070 170". The Automode uses a formula to calculate the desired motor position out of two variables - here 70 and 170.
These commands are routed to functions in the code using SerialCommand Library.

In Arduino IDE serial Monitor i'm using "NL". All the commands are intepreted correctly, the code runs 100% fault free and all my functions do what i want.

Now I want to use a mobile phone instead of the serial monitor. Having wired up a hc-05 module (gnd, vcc, tx, rx with resistor) to the arduino I tried to switch on and off the buildin LED and it worked. I used my Galaxy S8, paired it with the HC-05 and used a small app, created with MIT App Inventor.

My idea was then to swap the standard serial settings in my code to Digital Pins 10 and 11 as I also tried this in the led example.

I set up another app in MIT App Inventor which send strings to a paired bluetooth device. My app mainly works the same way as described on howtomechatronics arduino hc-05 tutorial

Unfortunately it is not working.

I assume, that the serialread in my loop is not enough or the wrong way but I don't know how to to this better.

Any ideas?

Here's my code, I deleted the code from functions that work probably

#include <SoftwareSerial.h>
#include <SerialCommand.h>

// wire setup
SoftwareSerial mySerial(10, 11); // RX 10 , TX 11 for usb debugging, later change to 0 and 1  // HC-05 Serial Connection
#define inputA 2     // Sensor A input
#define inputB 3     // Sensor B input
#define motorLow 4    //  relay out
#define motorHigh 5   //  relay out
#define stopLow 12   // endstop Switch as pullup (resistor to +5)
#define stopHigh 9   // endstop Switch as pullup (resistor to +5)
#define ledPin 13    // led output / arduinoLED

SerialCommand SCmd(mySerial);  // SerialCommand Object to handle serial operations

// intialize vars
char stateLED = 0;   // status led
int counter = 0;   // step counter for Sensor
int lastCounter;    // remeber counter when movement starts
int dir = 0;       // 0 for increasing, 1 for decreasing
int aState;
int aLastState;
double stepsMax = 245.0;  // maximum steps the motor can do
int stepsIncr = 15;   // steps for small movements

// set up the in- and outputs
void setup() {
  pinMode (inputA, INPUT); // Sensor A input
  pinMode (inputB, INPUT); // Sensor B input
  pinMode (ledPin, OUTPUT);  // Led Output
  digitalWrite(ledPin, LOW);  // initialize led pin
  pinMode (stopLow, INPUT);   // endstop switch low
  digitalWrite(stopLow, HIGH);   //
  pinMode (stopHigh, INPUT);   // enstop switch high
  digitalWrite(stopHigh, LOW);   //
  mySerial.begin(9600);  // def com rate of bl mod
  pinMode (motorLow, OUTPUT);
  pinMode (motorHigh, OUTPUT);
  digitalWrite(motorLow, HIGH);
  digitalWrite(motorHigh, HIGH);
 //Serial.begin(9600);

  // Setup callbacks for SerialCommand commands

  SCmd.addCommand("led", LED_onoff);     // led test
  SCmd.addCommand("a", moveToA);     // move to a - minimum position with endstop
  SCmd.addCommand("b", moveToB);    // move to b - predefined position
  SCmd.addCommand("c", moveToC);     // move to c - predefined position
  SCmd.addCommand("d", moveToD);    // move to d - maximum position
  SCmd.addCommand("e", moveToE);    // move to e - maximum position with endstop
  SCmd.addCommand("plus", movePlus);  // move incremental plus
  SCmd.addCommand("minus", moveMinus); // move incremental minus
  SCmd.addCommand("auto", process_auto); // Converts two arguments to integers and calculates steps
  SCmd.addCommand("hello", SayHello);    // Echos the string argument back
  SCmd.addDefaultHandler(unrecognized);  // Handler for command that isn't matched
  Serial.println("Ready");
  moveToMinimum();
}

// loop is runnig continously
void loop() {
  SCmd.readSerial();      // process serial commands
  //SCmd2.readSerial();     // process serial commands from standard serial port 
}


// Counting Steps ---------------------------------------------------------------------------------
int readSensor() {
  ....
  return counter;
}


void moveToMinimum() { 
  ...
}

void moveToA() { 
}

void moveToB() { 
}

void moveToC() { 
}

void moveToD() {
}

void moveToE() {
}

void movePlus(){
}

void moveMinus(){
   
}

void process_auto()
{
  int weight;
  int height;
  int steps;
  char *arg;

  mySerial.println("We're in auto_command");
  arg = SCmd.next();
  if (arg != NULL)
  {
    weight = atoi(arg);  // Converts a char string to an integer
    mySerial.print("weight was: ");
    mySerial.println(weight);
  }
  else {
    mySerial.println("No arguments");
  }

  arg = SCmd.next();
  if (arg != NULL)
  {
    height = atoi(arg);
    mySerial.print("height was: ");
    mySerial.println(height);
  }
  else {
    mySerial.println("No second argument");
  }


  steps = calcSteps(weight, height);
  moveMotor(steps); // moves motor to the position
  mySerial.print("moving for ");
  mySerial.print(steps);
}

// This gets set as the default handler, and gets called when no other command matches.
void unrecognized()
{
  mySerial.println("Nothing declared for that?");
}

// this moves the motor until the steps have been reached

void moveMotor(int steps) {
  // get current position
  // decide to go up or down
  // set relay for the movement to to low until step is reached
  if (steps == counter) {
    Serial.println("nothing to move, already in position");
  } else if (steps < counter) {
    // move to lower position
    digitalWrite(motorLow, LOW);  // begin rotation and rotate until counter is equal to steps
    Serial.println("moving lower towards steps position");
    while (steps < counter) {
      // read sensor and store data to counter
      counter = readSensor();
    }
      Serial.print("reached position: ");
      Serial.print(counter);
      Serial.println(" stopping motor.");
    digitalWrite(motorLow, HIGH);   // when counter equals stop rotation
  } else {
    // move to higher position
    digitalWrite(motorHigh, LOW);  // begin rotation and rotate until counter is equal to steps
    Serial.println("moving higher towards steps position");
    while (steps > counter) {
      // read sensor and store data to counter
      counter = readSensor();
    }
      Serial.print("reached position: ");
      Serial.print(counter);
      Serial.println(" stopping motor.");
    digitalWrite(motorHigh, HIGH);   // when counter equals stop rotation
  }

}

// this calculates the steps the motor has to rotate
int calcSteps(int weight, int height) {
 ...
  return steps;
}



// functions for debugging

void LED_onoff()
{
}

void LED_on()
{
  
}

void LED_off()
{
 
}

void SayHello()
{
}

These commands are routed to functions in the code using SerialCommand Library.

You need to provide a link to that library.

  pinMode (stopLow, INPUT);   // endstop switch low
  digitalWrite(stopLow, HIGH);   //

It would be more understandable to use INPUT_PULLUP as the mode, and ditch the call to digitalWrite().

It would, of course, be better to NOT use that library until you KNOW that the bluetooth device is connected correctly, is paired, and what the SoftwareSerial instance is actually receiving.

max_f_2019:
the code runs 100% fault free and all my functions do what i want.

Now I want to use a mobile phone instead of the serial monitor.

In that case, all you need to do is retire the monitor, connect Bluetooth to hardware serial pins 0,1 Rx>Tx and TX>Rx, and away you go. No change to code, no fartarsing about with software serial.

Nonetheless, the above does not guarantee what is going on at the other end, only Arduino<>Bluetooth. I suggest you use a standard Bluetooth Terminal to start with.

Thanks a lot for your answers, I'll check just using standard TX and RX and will give feedback

PaulS:
You need to provide a link to that library.
[

PaulS:

  pinMode (stopLow, INPUT);   // endstop switch low

digitalWrite(stopLow, HIGH);   //



It would be more understandable to use INPUT_PULLUP as the mode, and ditch the call to digitalWrite().

It would, of course, be better to NOT use that library until you KNOW that the bluetooth device is connected correctly, is paired, and what the SoftwareSerial instance is actually receiving.

Thank you very much for that hint - it saves a lot of time to not wire up additional pullup/pulldown resistors! Thanks