Back from a short fishing trip. The problem of never completing uploading was solved by simply removing the wires from the RX and TX (pins 0 and 1) when uploading.
I think the UNO, with only one hardware serial port, is not the best for what I'm trying to do because the port is shared by the USB connection (if I understand this correctly). So I decided to try using SoftwareSerial for my connection to the HC-05. I have the TX and RX pins on the HC05 connected to UNO pins 2 and 3, respectively.
Pushing the button changes the buttonState variable successfully. However, mySerial.available and mySerial.availableForWrite return a zero, and prevState never changes. (The LED on the master doesn't change either).
What value should I get from mySerial.available and mySerial.availableForWrite? What am I doing wrong?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
const int button = 8;
int buttonState = 0;
int prevState = 0;
void setup()
{
pinMode(button, INPUT_PULLUP);
// Set data rate for the serial monitor:
Serial.begin(9600);
delay(5);
// set the data rate for the SoftwareSerial port to match the UART baud on HC05
mySerial.begin(38400);
delay(5);
}
void loop()
{
// Reading the button
buttonState = digitalRead(button);
Serial.print("buttonState = ");
Serial.println(buttonState);
Serial.print("prevState = ");
Serial.println(prevState);
Serial.print("mySerial.availableForWrite = ");
Serial.println(mySerial.availableForWrite());
delay(1000);
if (buttonState != prevState) // if button has been pushed or released
{
if (buttonState == LOW && (mySerial.availableForWrite()>0)) // becomes LOW when button is pushed
{
mySerial.write('1'); // Sends '1' to the master to turn on LED
prevState = LOW;
delay(5);
}
else if (buttonState == HIGH && (mySerial.availableForWrite()>0))
{
mySerial.write('0');
prevState = HIGH;
delay(5);
}
}
}
