Bluetooth Serial Value being read, but not updated after re-reading

Hello All,

This is my first time here because I seem to have encountered a problem when interfacing with Bluetooth.

I had a code to send a “1” from one HC-05 to another, and turn an LED on the second module by holding a button down on the first module. Everything worked fine.

Fast forward to today. I have to run a “bottle logic” of pumps when Arduino2 receives a “1” through bluetooth from Arduino1. Arduino 2 also has a local button. When neither button is pressed, the system will idle.

Now, the local button works fine. It runs once when you press it, and then waits for you to press it again.

The bluetooth button on the other hand seems to run indefinitely after a single press. It will run through the logic fully, repeatedly until power is disconnected.

It should send a “1” when pressed, then repeatedly send “0” when unpressed (as I saw on the serial monitor for the original LED code). Even when I power the bluetooth down, the machine still continues to run.

I believe the value of “1” is being stored and is not being updated, despite multiple attempts to set it back to 0, or tell the arduino to re-read the bluetooth input.

I am on mobile at the moment, but I will try my best to attach the code below so you may see what I have so far.

Thanks in advance!

Machine-Code-BTtest2.ino (1.26 KB)

Some helpers (including me) use a mobile device that cannot view .ino attachments. The locked topics at the top of the forum will show you the correct way to post, including posting inline using code tags.

The main problem is that you aren't showing us the transmitter code. My guess is that the transmitter code is spam sending a thousand or so '1' chars every time the remote button is pressed (even for a moment). Thus, the way you have your code written, it will take (probably) about 46 iterations of the loop to finally clear out all the extraneous '1' chars in the input buffer. You could send a '1' char only after an edge detection and this would stop the transmitter from spamming.

Again, this is all a guess until you post the transmitter code.

I think this may be the problem.

The LED toggle code that worked fine first did not have any Delays in it, so it would (almost) instantaneuously update the serial, however this code will pull the numbers out one by one.

I have seen it in the previous test code.

How can I change the transmitter code to only send a single 1 on a button press?

Also, i will post the transmitter code now if it help, just need to move to my PC.

Here is the transmitter code:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(9, 10);

int state = 0;
const int ledPin = 8;  //the pin the led is connected to
const int buttonPin = 2;  //the pin the button is connected to

int buttonState = 0;

void setup() {

  Serial.begin(9600);
  BTserial.begin(38400);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    BTserial.write('1');  //sends a 1 through the bluetooth serial link
    delay (20);
  }

  else {
    BTserial.write('0');
    digitalWrite(ledPin, LOW);
    delay(20);
  }
}

How am I able to change this to only send a single 1 across the Bluetooth?

Try this for the transmitter:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(9, 10);

int state = 0;
const int ledPin = 8;  //the pin the led is connected to
const int buttonPin = 2;  //the pin the button is connected to

int previousButtonState = 0;
int currentButtonState = 0;

void setup() {

  Serial.begin(9600);
  BTserial.begin(38400);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}

void loop() {
  previousButtonState = currentButtonState;
  currentButtonState = digitalRead(buttonPin);

  //check for rising edge
  if ((currentButtonState == HIGH) && (previousButtonState == LOW)) {
    digitalWrite(ledPin, HIGH);
    BTserial.write('1');  //sends a 1 through the bluetooth serial link
  }
}

Note that you need to reset the state value after running through the commands on the receiver cause the transmitter will not send a '0'.

Receiver code:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);
const int WaterP = 4;
const int Solenoid = 5;
const int Heater = 6;
const int AirP = 7;
const int StartPB = 8;
const int LedON = 12;
const int LedOFF = 13;
int startval = 0;
int state = 0;

void setup() {

  Serial.begin(9600);
  BTserial.begin(38400);

  pinMode(StartPB, INPUT);
  pinMode(WaterP, OUTPUT);
  pinMode(Solenoid, OUTPUT);
  pinMode(Heater, OUTPUT);
  pinMode(AirP, OUTPUT);
  pinMode(LedON, OUTPUT);
  pinMode(LedOFF, OUTPUT);
  //pinMode(A2, OUTPUT);
}

void loop() {

if (BTserial.available() > 0) {
    state = BTserial.read();// Reads remote
}

  startval = digitalRead(StartPB); // Reads local

  if ((state == '1')||(startval == HIGH)) {
  
  digitalWrite(LedON, HIGH);
  digitalWrite(LedOFF, LOW);
  digitalWrite(WaterP, HIGH);
  digitalWrite(Solenoid, HIGH);
  delay(14000);

  digitalWrite(WaterP, LOW);
  digitalWrite(Heater, HIGH);
  delay(900);

  digitalWrite(Heater, LOW);
  digitalWrite(Solenoid, LOW);
  digitalWrite(AirP, HIGH);
  delay(12000);

  digitalWrite(AirP, LOW);
  digitalWrite(LedON, LOW);
  digitalWrite(LedOFF, HIGH);
  //digitalWrite(A2, LOW);
  state = 0;
  //state = BTserial.read();
  delay(2000);
}
else {
  digitalWrite(LedON, LOW);
  digitalWrite(LedOFF, HIGH);
  state = 0;
}

  if(state != 0)
  {
    state = 0;
  )
}

Thank you very much!

I went with:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(9, 10);

int state = 0;
const int ledPin = 8;  //the pin the led is connected to
const int buttonPin = 2;  //the pin the button is connected to

int buttonState = 0;
int lastButtonState = 0;

void setup() {

  Serial.begin(9600);
  BTserial.begin(38400);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}

void loop() {

  buttonState = digitalRead(buttonPin);
  
if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    BTserial.write('1');  //sends a 1 through the bluetooth serial link
  }

  else {
    BTserial.write('0');
    digitalWrite(ledPin, LOW);
;  }
  delay(50);
}
lastButtonState = buttonState;
}

The machine is fully operational, and will only run once from a single button press on both ends.

It was funny, I was browsing on here over the last couple of days and noticed someones signature/quote was "The problem is in the code you didn't post."

I chuckled to myself but after messing around with the machine side for 3 hours last night, it is very ironic that it was the transmitter all along!

Regardless, thanks again for the help.