Hello,
I am trying to get bluetooth HC-06 chip to work on my mega. I hooked the rx (pin 19) of mega to tx pin of the HC-06 and hooked the tx (pin 18) to 5 1k resistors (i didn't have a 5k) and at the end of that there is a wire, connected to the rx of the HC-06 and a 10k resistor attached to ground, so that it acts as a voltage divider.
I hooked VCC of the HC-06 to 5v output on the mega, and hooked the gwd to gwd. I tried this code, and it sends hello to the serial monitor every second, but when I type something in the serial monitor on lap top2 (for example Led On) to send to the HC-06 that tells the arduino to turn on or off the led, it says
const int ledPin=32;
const int ledPin2=33;
boolean ledState=LOW;
String readString;
unsigned long previousMillis=0;
const int interval=2000;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
while (!Serial1) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
Serial.println("Setup Finished");
}
void loop() // run over and over
{blinkwithoutDelay();
checkBluetooth();
}
void blinkwithoutDelay(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
ledState=!ledState;
if (ledState==HIGH)
Serial1.println(F("led2 is on"));
else
Serial1.println(F("led2 is off"));
digitalWrite(ledPin2, ledState);
}
}
void checkBluetooth()
{
if (Serial1.available())
{
Serial.println(F("something available"));
Serial.print(F("Number of bytes available: "));
Serial.println(Serial1.available());
Serial.print(F("The first Byte is a: "));
// Serial.println(Serial1.read());
char c =Serial1.read();
readString+=c;
Serial.write(c);
Serial.println();
Serial.print(F("The readString is: "));
Serial.println(readString);
Serial.println(F("==========end of this loop============"));
Serial.println();
if (readString.endsWith("On"))
{
digitalWrite(ledPin,HIGH);
Serial.println(F("led1 is high"));
}
else if (readString.endsWith("Off"))
{ digitalWrite(ledPin,LOW);
Serial.println(F("led1 is low"));
}
}
}
This is the serial readout from Serial.print on the mega
something available
Number of bytes available: 1
The first Byte is a:
for some reason when i copied the serial monitor, didn't want to paste the whole thing
here is the next part
The readString is:
... that 1 byte must be a '\n' because it did it again when i tried to paste.
Do I need to put in a buffer or anything? Why is it only saying there is one Byte, when there should be 6?