Go Down

Topic: bluetooth connection (Read 170 times) previous topic - next topic

Fowelld

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

Robin2

FIRST, edit your Post and remove your email address, otherwise it can be detected by spammers and you will get all the junk. People here won't be sending you emails - you will get your responses here in the Forum.


SECOND, to make it easy for people to help you please modify your post and use the code button </>


Code: [Select]
so your code
looks like this

 and is easy to copy to a text editor. See How to use the Forum


It would be wise to connect the Bluetooth modules to a SoftwareSerial connection on each Uno so that you leave Serial free for uploading programs and for sending debug messages to your PC.


Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
The LED on the master is connected to pin 13.
So, the slave is telling the master what to do. I think you have your roles completely reversed.
The art of getting good answers lies in asking good questions.

Nick_Pyner

#3
May 14, 2019, 01:58 am Last Edit: May 14, 2019, 02:05 am by Nick_Pyner
On both Arduino Uno's I have connected the RX to RX on the HC05's and the TX to the TX.
No, THINK about it,  Tx>Rx and Rx<Tx. Transmitters transmit to receivers, and receivers shouldn't expect too much from another receiver. This may be your only problem.
So, the slave is telling the master what to do. I think you have your roles completely reversed.
No, only the wiring. A master makes the connection. Once done, it doesn't matter which role is which.

Go Up