Hi, I have been able to get the Hc-05 communicating with the serial to turn a light on via Bluetooth when I type a number into the serial. I would now like to be able to send data from my Arduino to the serial port via Bluetooth. I have tried and I cant seem to get digital.read working for the pins when communicating through the Hc-05. And assistance would be great. All I want is to get a button to communicate back to the serial port.
/* Code written by Chams for Youtube Channel - That'sEngineering
13/2/2020
Youtube video: HC-05 BT module, interfacing with Arduino (via Computer)
*/
#include <SoftwareSerial.h>
char value;
int Tx = 2; // connect BT module TX to 2
int Rx = 3; // connect BT module RX to 3
const int LED = 7;
const int buttonPin = 4;
int buttonState = 0;
// creates a "virtual" serial port/UART
SoftwareSerial bluetooth(Tx, Rx);
void setup() {
pinMode(LED, OUTPUT);
pinMode(buttonPin, INPUT);
// start serial communication at default baud rate 9600bps
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop() {
if (bluetooth.available()) {
value = bluetooth.read();
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) { // turn LED on:
digitalWrite(LED, HIGH);
Serial.println("ON");
} else {
// turn LED off:
digitalWrite(LED, LOW);
Serial.println("OFF");
}
if (value == 1) { // turn LED on:
digitalWrite(LED, HIGH);
Serial.println("ON");
}
}
}