Do the “C” and “D” commands print “C” and “D” in the Arduino terminal? If they do (and I suspect they do, since I don’t immediately see any errors) then it has to be something in the hardware as you imply. Make sure those desired pins aren’t being used for something else in addition to checking the wiring and LEDs. If you see no problems with the wiring or the LEDs try a different pin for the second LED
Thank you oldguy and cbensen! I now have the code written to control the Rover V2 with the HM-10 bluetooth and using the serial terminal app. I actually tested it on another bluetooth terminal app and it works fine there too.
The HM-10’s rx is attached to pin 0 and tx to pin 1 on the Rover. Also ground to gnd and vcc to 3.3v
#include <SoftwareSerial.h>
/* Copy and paste the code below into the Arduino software */
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
SoftwareSerial Bluetooth(0, 1);
void setup()
{ Serial.begin(9600);
Bluetooth.begin(9600);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
}
void loop() {
int command;
if (Bluetooth.available()) {
command = Bluetooth.read();
Serial.println(“Input received:”);
}
if (command == 65)//forward
{
Serial.println(‘A’);
analogWrite (E1, 255);
digitalWrite(M1, LOW);
analogWrite (E2, 255);
digitalWrite(M2, LOW);
}
if (command == 66)//reverse
{
Serial.println(‘B’);
analogWrite (E1, 255);
digitalWrite(M1, HIGH);
analogWrite (E2, 255);
digitalWrite(M2, HIGH);
}
if (command == 67)//left
{
Serial.println(‘C’);
analogWrite (E1, 255);
digitalWrite(M1, HIGH);
analogWrite (E2, 255);
digitalWrite(M2, LOW);
}
if (command == 68) //right
{
Serial.println(‘D’);
analogWrite (E1, 255);
digitalWrite(M1, LOW);
analogWrite (E2, 255);
digitalWrite(M2, HIGH);
}
if (command == 69)//stop
{
Serial.println(‘E’);
analogWrite (E1, 0);
analogWrite (E2, 0);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
}
}
Great! Glad it’s working.
Looking forward to seeing videos of your project’s progress!
Here is a couple of pics of the robot. Tryin to resize my video so it will upload. Work is still in progress.
Nice! I like it!
Thanks! I am trying to use this same method of coding using the int commands through the serial app, but for controlling a servo. I would like to add some servos once I can figure out how to program them. I looked at a lot of examples and combined some stuff and made my own to control 1 servo. The code compiles and uploads but does not turn the servo. Can someone point me in the right direction as to what I am doing wrong? Code is below:
#include <SoftwareSerial.h>
#include<Servo.h>
Servo myServo;
SoftwareSerial Bluetooth(0, 1);
void setup() {
myServo.attach(4);
Serial.begin(9600);
Bluetooth.begin(9600);
}
void loop() {
int command;
if (Bluetooth.available()) {
command = Bluetooth.read();
Serial.println(“Input received:”);
}
if (command == 65) //set servo to 0
{
Serial.println(‘A’);
myServo.write(0);
}
if (command == 66) //set servo to 90 degrees
{
Serial.println (‘B’);
myServo.write(90);
}
if (command == 67) //set servo to 180 degrees
{
Serial.print (‘C’);
myServo.write(180);
}
}
It’s late, so my brain isn’t in the best shape. But I think if you move the curly bracket at the bottom of that quote all the way to the bottom of the loop() function it will work.
Give it a try and let me know. If that works I will explain why. If not I will look closer
Yes! It worked, thank you once again!
I assume that having the curly bracket there closed off the rest of the code from working?
It was still “working,” just not correctly. I will post an explanation when I have more time.
Here is the explanation of why that change worked.
void loop() is a function. the name is loop and the void means it does not return a value
A function enters at the top and either exits when it hits a “return” statement or the closing brace.
It goes sequentially (in order) from top to bottom unless a statement (if, while, do) changes the flow.
The name is misleading. The loop function doesn’t loop. It just gets called over and over by code you don’t see. So it gets called, does its thing, then exits. Then gets called again forever.
You declare the variable “int command” near the top. Since it is declared INSIDE the loop function, it gets created as a new variable every time the function gets called. It is a “local variable.” When the function exits at the bottom, the variable and its value goes away. Since you don’t assign a value to the command variable, it has an undefined value after it is created. It gets whatever garbage is in the memory location reserved for it. You then have the “if” statement that checks for something on the bluetooth interface and assigns that to the command variable BUT ONLY IF there is something new available. If there is nothing there, the command variable still has garbage. But since the closing brace of the if statement is right after that assignment, the if does not control any of the statements after it Serial.println(“Input received.”); That means the following if statements that check for a desired value get executed even though nothing new came in. It compares whatever garbage is in the variable command.
What you want instead is to NOT execute the following if statements that do the comparison and set the servo UNLESS a new character was received. By moving the closing curly brace from right after the Serial.println() statement to the end of the function, those if statements become “controlled” by the first if. That way, they don’t execute unless something was received.
By making that change, the loop function gets called, checks for a character on bluetooth, and if nothing arrived skips over everything else and exits, making no change. The way you had it, whatever garbage was in the variable (quite likely the last character received, for reasons I won’t explain here) and perhaps writes a new value to the servo, but NOT what you wanted it to write.