Hey, Here is my code. I am attempting to utilize my code to give my controll over the direction of my robot car, and it's one attached servo, via a text based interface.
Although this appears initially to work, what I am attempting to complete is to have the ability to control the robot where the HM-18 module can receive text based control instructions, and attempt to cause the robot to move, forward, back, and stop, and additionally have some response given to the user via text send via serial over Bluetooth to the phone utilized by the user to control the robot. I have attempted to program this myself, although it is proving quite difficult. The code for both the whole system is listed below, I sincerely hope to hear back soon.
/////////////////////////////////////////////////////////////////////////
//Code by Matthew Mowerson,
// Code reused is from Michael Stal's HM-10 and HM-18 Demo,
//and Ryan Chan and Reichenstein7's L298N Motor Controller Code
/////////////////////////////////////////////////////////////////////////
int ServoAxis = 0;
#include <SPI.h>
#include <Servo.h>
//atteched servo is beign defined
Servo Test_Servo;
int pos = 0;
//const byte address[6] = "00001"; //Channel 1
//int ServoWrite;
//int sDir = 90; // initial direction for servo, tells the servo it is in middle of range, need to work on auto position sensing.
// Wir verwenden Software Serial
#ifdef softserial
#include <SoftwareSerial.h>
const int BTRX = 0; // pin 1
const int BTTX = 1; // pin 0
SoftwareSerial SerialBT(BTRX, BTTX);
#else
HardwareSerial SerialBT = Serial;
#endif
//MOTOR 1 WIREING
int motor1pin1 = 8;
int motor1pin2 = 4;
int ENM1 = 10;
//MOTOR 2 WIREING
int motor2pin1 = 2;
int motor2pin2 = 12;
int ENM2 = 11;
// Die versendete Nachricht:
String msg;
///////////////////////////////////////////////////
//
// setup
// Verbindung mit HM-18 und HM-10 aufbauen
//
///////////////////////////////////////////////////
void setup() { // Setup runs once per reset
SerialBT.begin(9600); // initialize serial communication @ 9600 baud:
SerialBT.println("Bluetooth-Verbindung steht");
//Code to tell program what pin to use to communicate with servo.
Test_Servo.attach(5); // Attaches servo to pin 5 (PWM)
//Define L298N Dual H-Bridge Motor Controller Pins
//motor pin definitions
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
//motor one enable pin
pinMode(ENM1, OUTPUT);
//motor two enable pins
pinMode(ENM2, OUTPUT);
}
///////////////////////////////////////////////////
// watch dog loop
// wait for message in each iteration,
// parse message,
// trigger action (ether drive left motors forward or backwards or stops them
// and/or drives the right motors forward or backwards or stops them.)
///////////////////////////////////////////////////
void loop() {
ServoAxis = digitalRead(0);
if (SerialBT.available()) { // Daten liegen an
msg = SerialBT.readString(); // Nachricht lesen
if (msg == "SER ") { // if the HM-18 recives the string "SER " it tells the servo to rotate CCW
SerialBT.print("SEL For Loop entered");
Test_Servo.write(0); // tell servo to go to position in variable 'pos'
SerialBT.print("Sero 'Test_servo' have been given the direction to move to position Pos, which should be less than 90, thus moving it to the right");
delay(15);
SerialBT.print("Servo is now waiting till its next move more to the right.");
}
else if (msg == "SS ") { // if the HM-18 recives the string "SS " it tells the servo to rotate to the zero position
Test_Servo.write(180);
SerialBT.print("position zero is now writen to servo 'Test_Servo'");
SerialBT.print("Servo is at zero position");
SerialBT.print(" ");
}
else if (msg == "SEL ") {
SerialBT.print("SEL For Loop entered");
Test_Servo.write(pos); // tell servo to go to position in variable 'pos'
SerialBT.print("Sero 'Test_servo' have been given the direction to move to position Pos, which should be less than 90, thus moving it to the right");
delay(15);
SerialBT.print("Servo is now waiting till its next move more to the right.");
}
else if (msg == "F ") {
analogWrite(ENM1, 255); //Sets speed variable via PWM on ENM1 pin
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
analogWrite(ENM2, 255); //Sets speed variable via PWM on ENM2 pin
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
SerialBT.print("Motor 1 Forward"); // Prints out “Motor 1 Forward” on the serial monitor
SerialBT.print("Motor 2 Forward"); // Prints out “Motor 2 Forward” on the serial monitor
SerialBT.print(" ");
}
else if (msg == "S ") {
analogWrite(ENM1, 0);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
analogWrite(ENM2, 0);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
SerialBT.print("Motor 1 Stop");
SerialBT.print("Motor 2 Stop");
SerialBT.print(" ");
}
else if (msg == "R ") {
analogWrite(ENM1, 255);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
analogWrite(ENM2, 255);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
SerialBT.print("Motor 1 Reverse");
SerialBT.print("Motor 2 Reverse");
SerialBT.print(" ");
}
else {
SerialBT.print("There is No <");
SerialBT.print(msg);
SerialBT.println("> Command");
}
}
}