Hey there.
I'm Chrisbisi, and I'm working on an RC car using hc05 Bluetooth modules.
I'm having sum problems with the code slash working.
I copied some code from the internet (GitHub - embeddedlab786/Bluetooth_Modules_as_Master_and_Slave: Bluetooth_Modules_as_Master_and_Slave)
and it worked.
However, when I just added a few inputs/outputs it did not work.
This is the code I made/modified.
transmitter
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
#define button A0
#define button2 A1
#define button3 A2
#define button4 A3
int buttonState;
int buttonState2;
int buttonState3;
int buttonState4;
void setup() { // put your setup code here, to run once
pinMode(button, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
Serial.begin(9600); // Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
mySerial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
// Reading the button
buttonState = digitalRead(button);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);
if (buttonState == LOW) { //send data to slave
mySerial.write('1');
}
else if (buttonstate2 = LOW)
{
mySerial.write('2');
}
else if (buttonstate3 = LOW)
{
mySerial.write('3');
}
else if (buttonstate4 = LOW)
{
mySerial.write('4');
}
else
{
mySerial.write('0');
}
}
receiver
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
#define rightfront 2
#define rightback 3
#define leftfront 4
#define leftback 5
int state = 0;
void setup() { // put your setup code here, to run once
pinMode(rightfront, OUTPUT);
pinMode(rightback, OUTPUT);
pinMode(leftfront, OUTPUT);
pinMode(leftback, OUTPUT);
Serial.begin(9600); //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
mySerial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
if(mySerial.available() > 0){ // Checks whether data is comming from the serial port
state = mySerial.read(); // Reads the data from the serial port
}
// Controlling the motors
if (state == '1') {
digitalWrite(rightfront, HIGH);
digitalWrite(rightback, LOW);
digitalWrite(leftfront, HIGH);
digitalWrite(leftback, LOW);
}
else if (state == '2') {
digitalWrite(rightfront, LOW);
digitalWrite(rightback, HIGH);
digitalWrite(leftfront, LOW);
digitalWrite(leftback, HIGH);
}
else if (state == '3') {
digitalWrite(rightfront, LOW);
digitalWrite(rightback, HIGH);
digitalWrite(leftfront, HIGH);
digitalWrite(leftback, LOW);
}
else if (state == '4') {
digitalWrite(rightfront, HIGH);
digitalWrite(rightback, LOW);
digitalWrite(leftfront, LOW);
digitalWrite(leftback, HIGH);
}
else
{
Serial.println("0 or unidentified");
}
}
I tried many different tutorials but only this one worked.
And when I modified it for my purpose it didn't work.
Could u pls compare the two code and try to rectify/find the problem for me?