Arduino Arduino UNO BJT BlueSMiRF Bluetooth DC motor RFCOMM RoboRemo slider motor speed power transistor
// Control dc motor speed via Bluetooth
// using RoboRemo app
// www.roboremo.app
// Hardware setup:
// BT module Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1
// Vin
// |^^^^^^^^^^^|___|
// ==| dc motor |___
// |___________| |
// | C
// ___ B |/
// pin9--|___|-----| NPN
// 220 Ohm |->.
// | E
// |
// GND
#define bluetooth Serial
int motorSpeedPin = 9; // pin 9 (PWM) to contorl motor speed
char cmd[100];
int cmdIndex;
void exeCmd() {
if( cmd[0]=='s' &&
cmd[1]=='p' &&
cmd[2]=='e' &&
cmd[3]=='e' &&
cmd[4]=='d' &&
cmd[5]==' ' ) {
int val = 0;
for(int i=6; cmd[i]!=0; i++) { // number begins at cmd[6]
val = val*10 + (cmd[i]-'0');
}
// if cmd is "speed 100", val will be 100
analogWrite(motorSpeedPin, val);
}
}
void setup() {
delay(500); // wait for bluetooth module to start
bluetooth.begin(115200); // Bluetooth default baud is 115200
pinMode(motorSpeedPin, OUTPUT);
analogWrite(motorSpeedPin, 0);
cmdIndex = 0;
}
void loop() {
if(bluetooth.available()) {
char c = (char)bluetooth.read();
if(c=='\n') {
cmd[cmdIndex] = 0;
exeCmd(); // execute the command
cmdIndex = 0; // reset the cmdIndex
} else {
cmd[cmdIndex] = c;
if(cmdIndex<99) cmdIndex++;
}
}
}