Programming and sending messages via bluetooth.

Hey guy, I am currently having trouble with my project. I am sending messages between HC-05 and HC-06 bluetooth modules. My code is supposed to control a door strike by sending a one and a zero one being being off and 0 being on, as this is a fail-safe door strike. I have the Door strike in this configuration. Note this is a diagram I followed and the LED is my door strike. My problems is that the door stike is not responding when I press the button on the transmitting end (HC-05) the HC-06 modue is recieving it but the doorstike is not turning off. I supposed it might be might code:

HC-05:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX

char c = ‘ ‘;

byte switchPin = 8;
boolean switch_State = HIGH;
boolean oldswitch_State = HIGH;

void setup()
{
Serial.begin(9600);
Serial.println(“Arduino is ready”);
Serial.println(“Remember to select Both NL & CR in the serial monitor”);

BTserial.begin(9600);
}

void loop()
{

if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}

if (Serial.available())
{
c = Serial.read();
BTserial.write(c);
}
boolean state1 = digitalRead(switchPin); delay(1);
boolean state2 = digitalRead(switchPin); delay(1);
boolean state3 = digitalRead(switchPin); delay(1);
if ((state1 == state2) && (state1==state3))
{
switch_State = state1;

if (switch_State != oldswitch_State)
{
if ( switch_State == LOW) { BTserial.print(“1” ); Serial.println(“1”); }
else { BTserial.print(“0” ); Serial.println(“0”); }

oldswitch_State = switch_State;
}
}
}

HC-06: The is where the door strike is connected

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX

char c=’ ‘;
byte LEDpin = 4;

void setup()
{
Serial.begin(9600);
Serial.println(“Enter AT commands:”);

// HC-06 default baud rate is 9600
BTSerial.begin(9600);

pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin,LOW);
}

void loop()
{
if (BTSerial.available())
{
c = BTSerial.read();

// 49 is the ascii code for “1”
// 48 is the ascii code for “0”
if (c==49) { digitalWrite(LEDpin,LOW); }
if (c==48) { digitalWrite(LEDpin,HIGH); }
Serial.println(c);
}
{

if (BTSerial.available())
Serial.write(BTSerial.read());

if (Serial.available())
BTSerial.write(Serial.read());
}
}

oxZ8JDs.png

Please use code tags next time. The forum software eats some of your code if you don't.

    if (BTserial.available())


        c = BTserial.read();
        Serial.write(c);
    }

if (Serial.available())
    {
        c =  Serial.read();
        BTserial.write(c); 
    }

If you were planning to do anything with the character you read from bluetooth, then it may or may not have been overwritten by Serial. You are allowed to use more than one variable. Each variable should have only one purpose and that purpose should not change part-way through the loop.

    boolean state1 = digitalRead(switchPin); delay(1);

boolean state2 = digitalRead(switchPin); delay(1);
    boolean state3 = digitalRead(switchPin); delay(1);

Well, that's a novel way to do debouncing: stick your head in the sand for 3 milliseconds. Use the debounce library instead. Some switches need 10-20ms of debouncing and that's really going to cause problems for you later with that much dead-time in the sketch.

Use the control-T autoformat to put the braces ({}) on their own lines. It's going to make it much easier to read the sketch and expand it in the future. One-line if() statements are actually very rare in real programs.

    if (BTSerial.available())

{
        c = BTSerial.read();

// 49 is the ascii code for "1"
        // 48 is the ascii code for "0"
        if (c==49)  { digitalWrite(LEDpin,LOW);  }
        if (c==48)  { digitalWrite(LEDpin,HIGH);    }
        Serial.println(c);
    }
  {

if (BTSerial.available())
    Serial.write(BTSerial.read());

Some of the time the first BTSerial.read() is going to get a character and some of the time the second one will. Try not to read at many places in the program: get all the serial input in one place and then start decoding it.

There's a much better way to put character constants into your sketch:

        if (c=='1')   { digitalWrite(LEDpin,LOW);   }
        if (c=='0')   { digitalWrite(LEDpin,HIGH);    }

If you were planning to do anything with the character you read from bluetooth, then it may or may not have been overwritten by Serial. You are allowed to use more than one variable. Each variable should have only one purpose and that purpose should not change part-way through the loop.

How would I make sure that the serial does't overwrite the character that is sent?

Try not to read at many places in the program: get all the serial input in one place and then start decoding it.

So should I put my code under one? How would this look like?

Thanks for taking time out to help me.

Some switches need 10-20ms of debouncing and that's really going to cause problems for you later with that much dead-time in the sketch.

And, it is ONLY necessary to deal with bouncing when the switch has changed state.

How would I make sure that the serial does't overwrite the character that is sent?

Suppose that you have two wells. How do you make sure that water from the water well doesn't dilute the beer from the beer well when you are sent to the wells to get a bucket of water and a pitcher of beer?

Don't you suppose that you might use two different buckets to scoop fluids from the wells?

mumbus12:
So should I put my code under one? How would this look like?

Thanks for taking time out to help me.

It would look like this...

void loop(){
  readSerial();
  readBT();
  doCalculations();
  doOutput();
}