So I played around with the arduinos and modules and used the code below. If I press the button on the slave it should send int value 1 and light the LED on the board. The master will read and print this in the console and then light it's LED.
This works in the first few seconds and I've observed two weird things (not counting the fact that my modules can't be configured in another baud rate than 115200).
-
When I press the button (value=1 sent from the slave) my value that is displayed in the console is 76 and when not pressed (value=0 sent from the slave) it goes to 48-49.
-
The console stops updating and the LED on the master doesn't work after a few seconds.
MASTER CODE
#include <SoftwareSerial.h>
int i = 0;
int LED_RED = 6;
int LED_YELLOW = 7;
int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;
int value_BT = 0;
int bluetoothTx = 9; // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10; // RX-I pin of Bluesmirf silver, Arduino D10
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(bluetoothTx, INPUT);
pinMode(bluetoothRx, OUTPUT);
///Indicating that this is the master
led_indicate_master();
Serial.begin(115200);
bluetooth.begin(115200);
bluetooth.print("$$");
delay(100);
bluetooth.println("SM,1");
delay(100);
bluetooth.println("C,000666643C3D");
delay(100);
bluetooth.println("---");
// initialize LED pin and pusbutton
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.println("Debugging works");
}
void loop() {
if (bluetooth.available() > 0) {
value_BT = bluetooth.read();
Serial.println(value_BT);
if (value_BT >= 76) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
void led_indicate_master() {
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
delay(50);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
delay(50);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
delay(50);
}
SLAVE CODE
#include <SoftwareSerial.h>
int LED_RED = 5;
int LED_GREEN = 6;
int value_BT = 0;
int ledPin = 11;
int buttonPin = 12;
int buttonState = 0;
int bluetoothTx = 9; // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10; // RX-I pin of Bluesmirf silver, Arduino D10
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(bluetoothTx, INPUT);
pinMode(bluetoothRx, OUTPUT);
///Indicating that this is the slave
led_indicate_slave();
bluetooth.begin(115200);
bluetooth.print("$$");
delay(100);
bluetooth.println("SM,0");
delay(100);
bluetooth.println("---");
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
value_BT = 1;
digitalWrite(ledPin, HIGH);
bluetooth.print(value_BT);
} else {
value_BT = 0;
digitalWrite(ledPin, LOW);
bluetooth.print(value_BT);
}
}
void led_indicate_slave() {
digitalWrite(LED_GREEN,HIGH);
digitalWrite(LED_RED,HIGH);
delay(5000);
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_RED,LOW);
delay(50);
}