Blue Tooth Switch Overide

I am working on a project using blue tooth and an UNO to activate a switch. The project is working good, but I want to add another feature. I would like a manual switch to also be used in conjunction with the blue tooth.
The UNO will operate a relay that would close a circuit, thus turning the device on. There is also a manual switch that does the same task. The problem is when the manual switch is flipped off the UNO will still be activating the relay and closing the circuit. This would render the manual switch useless. I want to operate them independently for powering up the device but I want the manual switch to take over once it is flipped on.
My thought was to feed the UNO power on Pin 2 that would signal it to deactivate the blue tooth switch. This would then take it out of the picture. It’s not practical to turn the switch on manually then also turn the blue tooth one off with my iphone.
I’ve tried a few different things but nothing seems to work how I’d like. I am hoping somone can look at my code and offer me some advice.

#include <SoftwareSerial.h>
 
SoftwareSerial bluetooth(10, 11); // RX, TX
int ign = 6;    //PIN 6, OUTPUT
int SwitchIn = 2;     //FROM IGN SWITCH, INPUT 
boolean IgnState = HIGH;
String receivedBluetoothString = "";
 
void setup() {
  bluetooth.begin(9600);
  pinMode(ign, OUTPUT);
  pinMode(SwitchIn, INPUT);
  digitalWrite(SwitchIn, LOW);
}
 
void loop() {
  
  while ((bluetooth.available() > 0)){ 
 
    char receivedBluetoothChar = bluetooth.read();
    receivedBluetoothString += receivedBluetoothChar;
 
    if (receivedBluetoothChar == '\n') {
 
      if (receivedBluetoothString.toInt() == 887425) {
        digitalWrite(ign, (IgnState) ? HIGH : LOW);
        IgnState = !IgnState;
      }
                  
      if (SwitchIn == HIGH) {      // TO DISABLE BL SWITCH ONCE IGN SWITCH IS TURNED ON
          digitalWrite(ign, LOW);
          digitalWrite(IgnState, LOW);
      }
      
      receivedBluetoothString = "";
    }
  }
   
}

This sounds like an ordinary two way switch, using double throw switches but, in this case, one is operated by Arduino, which has been common practice since about 1890. I don't think it is a code issue.

Nick_Pyner:
This sounds like an ordinary two way switch, using double throw switches but, in this case, one is operated by Arduino, which has been common practice since about 1890. I don’t think it is a code issue.

Not exactly, but similar.
Yes, the code is not working like I’d like. I can’t seem to get the arduino to override the blue tooth activated switch when a signal input is on pin2.

I think the issue is that the blue tooth module is still receiving the command to activate the switch and the arduino is getting that command and not pin2 input.

I suspect you need to think about the logic.

You could use an AND so that the relay is only ON if the Bluetooth signal says "on" AND the switch says "on"

That way either of them can turn it off but if one of them says "off" the other one cannot override that.

Another concept is to have a variable that records the state of the relay. Then a signal from either the Bluetooth module OR from the switch can change the state of the relay.

That way neither Bluetooth nor the switch has priority and both them can turn it on or off at any time. However the position of the switch would not represent the state of the relay and the thing that is sending the Bluetooth message would not know, before it sends a message, whether the relay is on or off unless you code the Arduino to send a message whenever the state changes.

...R

I've tried using this line of code and it works....sort of. Still not what I am after.

  while (bluetooth.available() > 0 && digitalRead(SwitchIn) == LOW ){

It will function just like the code stated (switches ON and OFF when grounded), but will not terminate an ON or OFF when Pin2 is switched to HIGH. It actually locks the state of the relay.

For example, with Pin2 LOW I activate the relay through blue tooth and it is ON. (operates as expected)

With the relay still activated ON, I then switch Pin2 HIGH and the relay stays activated and I can not change its state through the blue tooth command. It's locked out. It will lock it out ON or OFF, depending on when Pin2 was switched to HIGH.

I need it to deactivate the relay when Pin2 goes HIGH.

Why have you a WHILE? An IF seems much more logical.

Also, rather than control the relay directly from the values from the Bluetooth module and the switch you should control it from variables that are updated from those sources.

...R

Robin2: Why have you a WHILE? An IF seems much more logical.

Also, rather than control the relay directly from the values from the Bluetooth module and the switch you should control it from variables that are updated from those sources.

...R

I'm still learning code, could you give me an example that would use variable?

Ok I was able to get it to work. I also had to add a pull-down resistor on pin2, but it works like a charm now. Thanks for your advice!