I'm still very new to arduino and coding and having some difficulties trying to figure out what I am doing wrong here.
This program is to have control over 2 relays, using a combination of an IR receiver and a ir_remotecontrol with pre-programmed buttons (ir_remote buttons 1 = relay 1 toggle, button 2 = relay 2 toggle).
A master momentary hard wired switch to override changes to the relays and toggle their states i.e if both relays are on via ir remote - toggle them off, or if only 1 relay is on toggle it off with
first click of button then toggle both back on after second click of button.
The Bluetooth module and android app is to also control relays the same as the ir_remote via integers sent through the serial port.
It all sort of works at the moment...
If I just work the relays using the ir remote and the momentary switch it all works perfectly.
if I make sure the relays are in the off position using the remote switch and then use the Bluetooth it also works perfectly.
The problem only is when I use a combination of the ir remote or switch and the Bluetooth - things get weird - the Bluetooth commands seem to reverse and the relays start making a buzzing noise and the momentary switch muddles things up so basically it's all bad news! I've stared at this code for hours and tried a few different things but I just can't see what's wrong.
Let me know if I need to give a more detailed example of the errors I am having. Just thinking it is probably obvious for you pro's out there when you see my noob code!.
Any help would be greatly appreciated!
Thanks,
Michael
/*This program is to have control over 2 relays, using a cobination of an IR reciever and remote control with pre programmed buttons (ir_remote buttons 1 = relay 1 toggle, 2 = relay 2 toggle).
A master momentery hard wired switch to overide changes to the relays and toggle their states i.e if both relays are on via ir remote - toggle them off, or if 1 only relay is on toggle it off with first click of button then toggle both back on after sencond click of button.
Bluetooth module and andoid app to also control relays the same as the ir_remote.
*/
//include libaries, and initialise variables
#include <SoftwareSerial.h>
#include <IRremote.h>
#define irPin 8
#define RxD 7
#define TxD 6
#define relay1 13
#define relay2 12
#define buttonPin 2
SoftwareSerial blueTooth(RxD, TxD);
IRrecv irrecv(irPin);
decode_results results;
int comdata;
const int wait = 30;
int sensorValue = 0;
int sensorOut = 0;
int relay1State = 0;
int relay2State = 0;
int masterState = 0;
int buttonState = 0;
int lastButtonState = 0;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup()
{
blueTooth.begin(38400); //set bluetooth baud rate
delay(500);
irrecv.enableIRIn(); //enable infrared recieve
pinMode(relay1, OUTPUT); //setting relays 1 & 2 to output
pinMode(relay2, OUTPUT);
pinMode(buttonPin, INPUT); //setting momentery swith (overide) to inpt
digitalWrite(relay1, relay1State); //putting current status of relays into a variable
digitalWrite(relay2, relay2State);
}
void loop()
{
if (blueTooth.available()) //setting a variable for bluetooth recieved serial commands
comdata = blueTooth.read();
if(comdata == '1'){
digitalWrite(relay1, relay1State); //if bluetooth receivs "1" toggle relay1state -- essentially relay 1 ON
}
if(comdata == '0'){
digitalWrite(relay1, !relay1State); //if bluetooth receivs "0" toggle relay1state -- essentially relay 1 OFF
}
if(comdata == '2'){
digitalWrite(relay2, relay2State); //if bluetooth receivs "2" toggle relay2state -- essentially relay 2 ON
}
if(comdata == '3'){
digitalWrite(relay2, !relay2State); //if bluetooth receivs "3" toggle relay2state -- essentially relay 2 OFF
}
if (irrecv.decode(&results)){ //Start listing for ir commands, if set remote button code is recvived toggle the variable relay1State or relay2state
switch (results.value){
case 0xFF30CF:
relay1State = !relay1State;
digitalWrite(relay1, relay1State);
delay(250);
break;
case 0xFF18E7:
relay2State = !relay2State;
digitalWrite(relay2, relay2State);
delay(250);
break;
}
irrecv.resume();
}
int reading = digitalRead(buttonPin); //debounce used to help with faulse readings from momentery switch
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) { //if the momentery switch is pressed toggle masterstate which is both relay 1 and relay 2 state
masterState = !masterState;
relay1State = masterState;
relay2State = masterState;
}
}
}
digitalWrite(relay1, relay1State);
digitalWrite(relay2, relay2State);
lastButtonState = reading;
}