4 LEDs Arduino Arduino UNO BlueSMiRF Bluetooth LED control RFCOMM RoboRemo buttons
// Control 4 LEDs via Bluetooth
// using RoboRemo app
// www.roboremo.app
// Hardware setup:
// BT module Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin2
// RX-I ------ pin3
// LEDs are connected on pins 13, 12, 11, 10
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of BT module to Arduino pin2
int bluetoothRx = 3; // RX-I pin of B module to Arduino pin3
// also connect VCC pin from BT module to Arduino 5V pin
// and GND pin from BT module to Arduino GND pin.
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
char cmd[100];
int cmdIndex;
void exeCmd() {
if(strcmp(cmd, "led1 0")==0) digitalWrite(led1, LOW);
if(strcmp(cmd, "led1 1")==0) digitalWrite(led1, HIGH);
if(strcmp(cmd, "led2 0")==0) digitalWrite(led2, LOW);
if(strcmp(cmd, "led2 1")==0) digitalWrite(led2, HIGH);
if(strcmp(cmd, "led3 0")==0) digitalWrite(led3, LOW);
if(strcmp(cmd, "led3 1")==0) digitalWrite(led3, HIGH);
if(strcmp(cmd, "led4 0")==0) digitalWrite(led4, LOW);
if(strcmp(cmd, "led4 1")==0) digitalWrite(led4, HIGH);
}
void setup() {
delay(500); // wait for bluetooth module to start
bluetooth.begin(115200); // Bluetooth default baud is 115200
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$"); // enter cmd mode
delay(250);
bluetooth.println("U,9600,N"); // change baud to 9600
bluetooth.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
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++;
}
}
}