//code sourced from [Bytes N Bits]
#include <SoftwareSerial.h>
const byte pinRX = 2, pinTX = 3, bt_Vin = 4;
const byte button_Vin = 9, button_Vout = 11;
SoftwareSerial BTSerial(pinRX, pinTX);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
BTSerial.begin(9600);
delay(1000);
pinMode(pinRX, INPUT);
pinMode(pinTX, OUTPUT);
pinMode(bt_Vin, OUTPUT); //powers bluetooth module
pinMode(button_Vin, OUTPUT);
pinMode(button_Vout, INPUT_PULLUP);
delay(100);
digitalWrite(bt_Vin, HIGH); //powers bluetooth module
delay(500);
if(BTSerial.available()) BTSerial.read(); //clears mysterious byte from bluetooth serial buffer
//BTSerial.write("AT+BAUD4"); //sets bluetooth's baud rate to 9600
//BTSerial.write("AT+NAMEBackseatGuardian"); //changes display name
}
void loop() {
String msg = "";
String msgbuffer = "";
char data = "";
char databyte = "";
//writes "AT" to bluetooth when button is pressed
if(digitalRead(button_Vout) == 0) { //press
Serial.println("AT");
BTSerial.write("AT");
delay(100);
}
//command line -> bluetooth module
while(Serial.available()){
databyte = Serial.read();
BTSerial.write(databyte);
//Serial.print((char)databyte);
}
//bluetooth module -> serial monitor
while(BTSerial.available()){
databyte = BTSerial.read();
Serial.write(databyte);
}
}