Hello
I am trying to send data from one arduino to another via bluetooth hc 05.
First I connected the two arduinos with wires (rx/tx) and checked to see if I can send/receive data - WORKED.
Than I configured one hc 05 to a master mode and paired it the the other, with the help of this tutorial http://blog.zakkemble.co.uk/getting-bluetooth-modules-talking-to-each-other/. WORKED
I am trying to send data with serial write and reading it with parseInt ( like I did when wiring the two arduinos) but it dosent work.
any ideas?
const int analogPin1 = A0;
const int analogPin2 = A1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensor1 = analogRead(analogPin1) ;
int sensor2 = analogRead(analogPin2) ;
Serial.write('a');
Serial.println(sensor1);
delay(1000);
Serial.write('b');
Serial.println(sensor2);
delay(1000);
}
receiver code
int sensor1=0;
int sensor2=0;
int inByte = -1;
char inString[6];
int stringPos = 0;
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
if(Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == 'a')
{
sensor1 = Serial.parseInt() ;
Serial.println(sensor1);
}
if (inChar == 'b')
{
sensor2 = Serial.parseInt() ;
Serial.println(sensor2);
}
}
delay(100);
}
As I wrote earlier, this code works with wire serial connection between the arduinos but not with the bluetooths.
the bluetooths are successfully paired.
I tried with Software Serial library it works for me…
#include <SoftwareSerial.h>
const int rxPin = 10; //SoftwareSerial RX pin, connect to JY-MCY TX pin
const int txPin = 12; //SoftwareSerial TX pin, connect to JY-MCU RX pin
// level shifting to 3.3 volts may be needed
SoftwareSerial mySerial(rxPin, txPin);
const int ledPin = 13; // led pin
int state = 0; // if state is 1, the LED will turn on and
// if state is 0, the LED will turn off
int flag = 0; // a flag to prevent duplicate messages
void setup() {
// sets the pins as outputs:
pinMode(ledPin, OUTPUT);
mySerial.begin(9600);
Serial.begin(9600);
digitalWrite(ledPin, LOW); // LED is initially off
}
void loop() {
//reads serial input and saves it in the state variable
//Serial.println(analogRead(A0));
if(mySerial.available() > 0){
state = mySerial.read();
delay(10);
Serial.print("Received: ");Serial.println(state);
}
// if the state is '0' the LED will turn off
if (state == '0') {
digitalWrite(ledPin, LOW);
mySerial.println("LED: off");
}
// if the state is '1' the led will turn on
else if (state == '1') {
digitalWrite(ledPin, HIGH);
mySerial.println("LED: on");
}
}