Hello,
I use an Arduino Mega 2560, HC-06 bluetooth module.
I would like to turn on and off an led with keys on the keyboard through bluetooth. When I try this piece of code and connect the TX and RX to RX0 and TX0 (default receive and transmit pins) then it works :
char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
if(Serial.available())
{//if there is data being recieved
blueToothVal=Serial.read(); //read it
}
if (blueToothVal=='n')
{//if value from bluetooth serial is n
digitalWrite(13,HIGH); //switch on LED
if (lastValue!='n')
Serial.println(F("LED is on")); //print LED is on
lastValue=blueToothVal;
}
else if (blueToothVal=='f')
{//if value from bluetooth serial is n
digitalWrite(13,LOW); //turn off LED
if (lastValue!='f')
Serial.println(F("LED is off")); //print LED is on
lastValue=blueToothVal;
}
delay(1000);
}
Though when I try to assign the RX and TX pins somewhere else to pin19 and 18, it wont work or send/receive anything.
Here is my modified code:
#define BT_SERIAL_TX 19
#define BT_SERIAL_RX 18
#include "SoftwareSerial.h"
SoftwareSerial BSerial(BT_SERIAL_TX, BT_SERIAL_RX);
char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)
void setup()
{
BSerial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
if(BSerial.available())
{//if there is data being recieved
blueToothVal=BSerial.read(); //read it
}
if (blueToothVal=='n')
{//if value from bluetooth serial is n
digitalWrite(13,HIGH); //switch on LED
if (lastValue!='n')
BSerial.println(F("LED is on")); //print LED is on
lastValue=blueToothVal;
}
else if (blueToothVal=='f')
{//if value from bluetooth serial is n
digitalWrite(13,LOW); //turn off LED
if (lastValue!='f')
BSerial.println(F("LED is off")); //print LED is on
lastValue=blueToothVal;
}
delay(1000);
}
Thanks in advance! Urgent help please.