Bluetooth On/Off Sketch compiling but not functioning

Hello all,

Brand New coder here. I am attempting to turn an electric scooter on / off with an HC-05 BT module and a Nano.

The BMS uses a 6 digit hex code to communicate with the motor controller, which is what I am using the Arduino to replace (trying for speed). The scooter functions fine with the code, but it's not turning on/off with Bluetooth as expected.

I am using SoftwareSerial as I need the Rx/Tx pins for the scoot, and the Bluetooth is wired to D10 & D11. I've done my diligence of checking wiring & making sure the module is connected (i have the slow flash and it connects with my phone).

My code might be a little sloppy; I kinda hopped into a big project rather that starting slow like I probably should've.

Any suggestions / constructive criticism welcome. I wasn't sure if char was the correct command to use, but I looked up some tutorials and it's what everyone else seemed to use when using SoftwareSerial.

Dump:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //rx / tx

void setup() {
 mySerial.begin(9600);
 // initialize Serial1:
  Serial.begin(9600);
  Serial.write(0xA6);
  Serial.write(0x12);
  Serial.write(0x02);
  Serial.write(0x10);
  Serial.write(0x14);
  Serial.write(0xCF);
  delay(500);
  Serial.write(0xA6);
  Serial.write(0x12);
  Serial.write(0x02);
  Serial.write(0x11);
  Serial.write(0x14);
  Serial.write(0x0B);
   delay(500);
}

void loop() {

if
(mySerial.available())
 char c = mySerial.read();
if (char c = 'A')
  { 
delay(500);
  Serial.write(0xA6);
  Serial.write(0x12);
  Serial.write(0x02);
  Serial.write(0x15);
  Serial.write(0x14);
  Serial.write(0x30);
  }
else if (char c = 'B')
 
 
{
delay (500);  
Serial.write (0x00); 
}

}

You have not nested the code with the test for Serial.available(). So it runs over and over repetitively each time through loop(), even if a new character isn't received. Is that what you intended?

Please learn to use standard formatting. Custom indentation is usually harder to read.

Also this is a declaration, not just a test. You are creating a whole new variable 'c' which exists only in the scope of the 'if' statement, it isn't the same 'c' that you declared elsewhere. Also you used the assignment statement '=' instead of the equality operator '==', so it isn't doing what you expect.

if (char c = 'A')

you should just have

if (c == 'A')
1 Like

I do want the Serial.available to repeat with the loop because if the scooter disconnects from BT I'd like it to turn off. Would adding a delay after be appropriate?

Also will do on formatting, as stated this is literally my first project and I'm a bit over my head but enjoying it a lot.

Also thank you for pointing out my declaration error, I'll fix it and update you on if it works.

Update: Thank you it functions as intended now.

I ended up just scrapping the char c = mySerial.read(); command and putting the mySerial.read() directly in the else/if statements (see below). if (mySerial.read() == 'A') { delay(500); Serial.write(0xA6); Serial.write(0x12); Serial.write(0x02); Serial.write(0x15); Serial.write(0x14); Serial.write(0x30); } else if (mySerial.read() == 'B')
Not sure if this is bad form or not.

Where would be the best references to learn proper formatting? Also thank you again for the help.

This tutorial Arduino Software Solutions has a number of sketches for reading from Serial, starting with reading a single char.

1 Like

Serial.read() removes at least one incoming character from the stream (the one it reads). Therefore, you can't call it repeatedly and obtain the same value. You had it right the first time, read it once, assign it to a variable, and use the variable if you need to test it multiple times.

The IDE has an automatic code formatter built in. Consult the documentation.