Arduino Bluetooth: Not following commands

Good day😀. I am currently interfacing with the arduino, HC-05, and l298n motor driver. I used a custom app that will connect to the HC-05 and send letter signals such as "a" "b" or "c". I have no problems with communications since I opened the serial monitor and saw exactly the letter signals sent by my phone.

Now the problem. I stored the character in a "char" variable. Then I used that variable in a switch case code so the arduino will know what to do when it recieves that signal. However, though the arduino recieves the right signal it does not do what I want it to do. It literally does nothing excpet the "print" portion of the code.

I made a custom 3d printed box for the arduino and I have no problems with electrical. I tested a code that just makes the motor go for 5 sec then off for 5 sec. How did it go? IT WORKED. But it does not work with the code that includes serial comms. I'm not new to interfacing with l298n but so far this problem has got me working for 6 hours straight. Here is my code:

int ena = 11;
int in1 = 8;
int in2 = 7;
char data;
void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(ena, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(ena, 255);
  
  if (Serial.available()>0){
    data = Serial.read();
    Serial.print(data);
    
    switch(data){
      case 'a':
        digitalWrite(in1,HIGH);
        digitalWrite(in2,LOW);
        digitalWrite(13,HIGH);

      case 'b':
        digitalWrite(in1,LOW);
        digitalWrite(in2,LOW);
        digitalWrite(13,LOW);
      
      default:
      break;
    
  }   
  
    } 

    delay(10);
  }

I am using arduino nano programmed with the new arduino ide. Looking forward to your kind help.:grinning::grinning:

Hello gabriel0417

The sketch won´t compile.

Check compiler messages first.

Enable "Compiler Warnings" to ALL in the preferences.

@paulpaulson thanks for your reply. I made a mistake in editing the code before showing it to you. I corrected it now and it still shows the same problem. Thanks again.

Hello gabriel0417

Perfect.

But I can´t see any timer() function the sketch to have a time controlled motor action.

Did is missed some?

Uh no. There is no timer function for it. As long as I press the button from my phone it will send the letter signal then it will hold. So its more like a push down-push up function. Push down is a, push up is b.

Also as I trial and error, I noticed that the "pin 13 high" function worked but the motor still doesn't spin.

int ena = 11;
int in1 = 8;
int in2 = 7;
char data;
void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(ena, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(ena, 255);
  
  if (Serial.available()>0){
    data = Serial.read();
    Serial.print(data);
    
    if(data == 'a'){
      digitalWrite(in1, HIGH);
      digitalWrite(in1, LOW);
      digitalWrite(13, HIGH);
      analogWrite(ena, 255);
    } 
    else{
      digitalWrite(in1, LOW);
      digitalWrite(in1, LOW);
      digitalWrite(13, LOW);
      analogWrite(ena, 255);
    }

    delay(10);
  }
}

Post the schematic of the hardware setup.


I apologize for the bad picture quality. I did this on tinkercad and they don't feature some of the electronic parts.:sweat_smile:
Also the Hc-05 VCC is connected to 5 volts not the pin next to it.

hi!

are you sure? I dind't have an Iphone but in I am sure EVERY app on phone I use to communicate with bluetooth send only one message at a time (but I didn't have the possibility to hold the button, only send a message).
Therefore, while driving my toy car with HC-05, I put a delay after the reception of an order (like go forward, turn left...).
If I didn't, the info is received by the car, eventually print on the console, but immediately after, the loop start again and ask for new command... wich will be void as I need to send again the command.
Actually the code works perfectly, but to make the motor move for few milliseconds (roughly one loop time) won't give a visible result

EDIT: about your circuitry: make sure ground are both connected from H bridge and nano

Do you have a pencil and piece of paper too?

@GrandPete Thanks for your reply. You are right. My phone sends only one message. But as soon as my phone sends a single letter, it is automatically stored in a variable named "data". Then every time the arduino "loops", calls the variable to check it in the switch case option. However it does not follow the given command under the switch case code.

@paulpaulson do you want a hand drawn schematic?

smart!
but a good way to let it it "go forward" till the next wall if you lost the connexion (real story, so now I have a function to detect loss of connexion that stops the motor)

dumb question, but we are investigating. Did you manage to drive your motor bridge accordingly to your needs WITHOUT bluetooth commands?

Well, I took an old code where I set up a uno with a HC-05 to control a toy car.
The tricks is: HC-05 deal with ASCII character, so you won't receive a "A" on your HC-05 but a "97".
Please note the serial monitor translates it, so you may think everything is fine.
See below an extract of my code. The character received from the HC-05 is stored in bChar data.
Then, I filter its value (as a number, ASCII coding) to trigger the motor.

void ECOUTE() {
  int mail = HC05.available();
  if (mail) {
    int bChar = HC05.read();   //Serial.println(bChar);
   if (DEBUG_SERIAL){   Serial.println("");Serial.print("recu: ");Serial.println(bChar);}
    if (bChar == 48) // touche 000
    { ALL_GEAR=!ALL_GEAR; 
     if (DEBUG_SERIAL){Serial.print("GEAR: ");Serial.println(ALL_GEAR);} }
     if (bChar == 97)//touche a
     { GEAR = GEAR^B00001;};
      if (bChar == 98)//touche b
      {GEAR = GEAR^B0010; };
       if (bChar == 99)//touche c
       {GEAR = GEAR^B0100; };
        if (bChar == 100)//touche d
        {GEAR = GEAR^B1000;};

Oh I remember how I drove my old bt rc car down the stairs, haha. I learned from my previous mistake because it did hurt my feelings so I had the State pin telling the arduino that if in case it gets disconnected it will automatically tell all motors to stop.

YES. Magically, I was able to do that with just:

digitalWrite(ena,255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(5000);
digitalWrite(ena,255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(5000);

It made the motor ran which made me conclude that there is no problem with circuitry and electrical. The real prob I guess is the code, cause the pin 13 led worked on bluetooth command when I used the if statements instead of switch case. I tried using if statements to drive the motor but it did not work unfortunately.

yep. One step further to the win!
Please see my post and example about the format of the value transferred from HC-05 to your founction.
Hope it will helps!

So could I use ASCII insted of 'a' 'b' 'c:? Then I test the variable again through if statements or switch case statemenrs?

I dont like switch statments (because I never used it), so my personnal choice is if statment.
Try this if you use "a" for Go and everything else STOP:

 if (Serial.available()>0){
    data = Serial.read();
    Serial.print(data);
    
    if(data == 97){
      digitalWrite(in1, HIGH);
      digitalWrite(in1, LOW);
      digitalWrite(13, HIGH);
      analogWrite(ena, 255);
    } 
    else{
      digitalWrite(in1, LOW);
      digitalWrite(in1, LOW);
      digitalWrite(13, LOW);
      analogWrite(ena, 255);
    }

@GrandPete @paulpaulson. You are lifesavers! IT WORKED! My 8 hours of trial and error can finally come to a closure. I think I need to lie my back. THANK YOU VERY MUCH!:grinning::grinning::grinning::grinning:

:v:

good job boy, to go thru troubles and get your solution.

EDIT: time for more function than "GO FORWARD TO THE STAIRS" ! :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.