I have 2 Arduino Uno's and I am trying to connect the via Bluetooth HC05s. I have set them up as master and slave and when I press a button on the slave I want to light a LED on the master. To test the button I have put a LED on the slave to light when the button is pressed which it does.
The code for the salve is:
/* == SLAVE CODE ==
//AT+ADDT 21,13,17783
//AT+UART 9600,0,0
//AT+ROLE 0
//AT+CMODE 0
//AT=VERSION 2.0-20100601
*/
const int buttonPin = 8;
const int ledPin = 9;
int buttonState = 0;
int flag=0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop(){
//Read button state (pressed or not pressed?)
buttonState = digitalRead(buttonPin);
//If button pressed...
if (buttonState == HIGH) {
//...ones, turn led off!
if ( flag == 0){
Serial.print(flag);
Serial.write('0');
Serial.println("\toff");
digitalWrite(ledPin, LOW);
flag=1; //change flag variable
}
//...twice, turn led off!
else if ( flag == 1){
Serial.print(flag);
Serial.write('1');
Serial.println("\ton");
digitalWrite(ledPin, HIGH);
flag=0; //change flag variable again
}
}
delay(200); //Small delay
}
and the code for the master is
/* == MASTER CODE ==
//AT+ADDT 18:91:d75f2e
//AT+UART 9600,0,0
//AT+ROLE 1
//AT+CMODE 0
//AT=VERSION 2.0-20100601
*/
#define ledPin 13
int state = 0;
int potValue = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
Serial.println(state);
}
// Controlling the LED
if (state == '1') {
digitalWrite(ledPin, HIGH); // LED ON
state = 0;
}
else if (state == '0') {
digitalWrite(ledPin, LOW); // LED OFF
state = 1;
}
delay(200);
}
On both Arduino Uno's I have connected the RX to RX on the HC05's and the TX to the TX.
The LED on the master is connected to pin 13.
Can anybody help?
my email is fowelld@yahoo.co.uk