I have two combine two programs regarding cpntroling a bldc motor via arduino and co trolling arduino via hc-05 module bluetooth so i want all of them motor esc and arduino torun 9n the command of bluetooth here are my sketches
A simple Arduino sketch that allows you to control a brushless motor via an ESC (or any servo really) using inputs from the Arduino IDE serial monitor.
servo_test.ino
#include <Servo.h>
Servo esc;
int escPin = 9;
int minPulseRate = 1000;
int maxPulseRate = 2000;
int throttleChangeDelay = 100;
void setup() {
Serial.begin(9600);
Serial.setTimeout(500);
// Attach the the servo to the correct pin and set the pulse range
esc.attach(escPin, minPulseRate, maxPulseRate);
// Write a minimum value (most ESCs require this correct startup)
esc.write(0);
}
void loop() {
// Wait for some input
if (Serial.available() > 0) {
// Read the new throttle value
int throttle = normalizeThrottle( Serial.parseInt() );
// Print it out
Serial.print("Setting throttle to: ");
Serial.println(throttle);
// Change throttle to the new value
changeThrottle(throttle);
}
}
void changeThrottle(int throttle) {
// Read the current throttle value
int currentThrottle = readThrottle();
// Are we going up or down?
int step = 1;
if( throttle < currentThrottle )
step = -1;
// Slowly move to the new throttle value
while( currentThrottle != throttle ) {
esc.write(currentThrottle + step);
currentThrottle = readThrottle();
delay(throttleChangeDelay);
}
}
int readThrottle() {
int throttle = esc.read();
Serial.print("Current throttle is: ");
Serial.println(throttle);
return throttle;
}
// Ensure the throttle value is between 0 - 180
int normalizeThrottle(int value) {
if( value < 0 )
return 0;
if( value > 180 )
return 180;
}
And my second sketch for bluetooth is
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4,5);
void setup() {
String setName = String("AT+NAME=MyBTBee\r\n"); //Setting name as 'MyBTBee'
Serial.begin(9600);
BTSerial.begin(38400);
BTSerial.print("AT\r\n"); //Check Status
delay(500);
while (BTSerial.available()) {
Serial.write(BTSerial.read());
}
BTSerial.print(setName); //Send Command to change the name
delay(500);
while (BTSerial.available()) {
Serial.write(BTSerial.read());
}}
void loop() {}
Thats all i have to combine these two sketches what i have to do