Hello.
I have some problems controlling a relay with an IR receiver.
I have a a relay, an IR receiver (TSOP2236), an IR LED, a green LED and a red LED. All of them are connected to an Arduino Uno R3. I want to control an split air conditioner which has soem problems with the mainboard, so when it turns off the outside part (I don´t know how to call it, it is the big one that is outside the room/house), it doesn´t turn it on again.
What I want the Arduino to do is:
1- With the relay turned off (A/C off), wait for IR signal.
2- IR signal received, turn relay on, wait 3-4 seconds (I haven´t decided yet) send IR on signal to the A/C.
3- Wait 10-15 minutes (I haven´t decided yet. In the sketch that value is 20 seconds to debug it faster). While waiting, wait IR off signal. If there is an IR off signal, send IR off signal to A/C (just in case the A/C didn´t received it from the remote controller), wait 3-4 seconds, turn off the relay, go back to the loop() function and wait for an IR on signal.
4- Having past that time without any IR signal received, send IR off signal to the A/C, wait 3-4 seconds, turn off the relay and go back to the first step.
These are the problems:
1- In this moment I don´t know what I modified, but after the 4th step, it won´t go back to the 1st step.
2- It doesn´t detect the IR off signal in the 3rd step.
Here is the code:
#include <IRremote.h>
unsigned long On = 0x9E00040A; // where XXXXXXXX is on our your remote's values
unsigned long Off = 0x8E00040A; // where XXXXXXXX is another button on your remote
#define ON_LED_PIN 10
#define RELAY_PIN 9
#define RELAY_LED_PIN 12
int RECV_PIN = 11;
int interval = 20000;
int check = 0;
int ant = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
IRsend irsend;
void irOn();
void irOff();
void relayOn();
void relayOff();
void blinkRelLed();
void opciones();
void opciones2();
void apagadoTotal();
void opcionesPrev();
unsigned int powerOn[68] = {8950, 4350, 650, 450, 650,450,650, 450, 650, 450, 650, 1550, 650, 450, 650, 450, 650, 450, 650, 1550, 700, 1500, 700, 1550, 650, 450, 650, 450, 650, 1550, 650, 1550, 650, 1550, 650, 1550, 650, 450, 700, 400, 650, 450, 650, 450, 650, 500, 600,1600, 650, 450, 650, 450, 650, 1550, 650, 1550, 650, 1550, 700, 1500, 700, 1500, 700, 400, 650, 1600, 650};
unsigned int powerOff[68] = {8950, 4350, 650, 450, 650,450,650, 450, 650, 450, 650, 1550, 650, 450, 650, 450, 650, 450, 650, 1550, 700, 1500, 700, 1550, 650, 450, 650, 450, 650, 1550, 650, 1550, 650, 1550, 650, 1550, 650, 450, 700, 400, 650, 450, 650, 450, 650, 500, 600,1600, 650, 450, 650, 450, 650, 1550, 650, 1550, 650, 1550, 700, 1500, 700, 1500, 700, 400, 650, 1600, 650};
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(RELAY_LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(ON_LED_PIN, OUTPUT);
digitalWrite(ON_LED_PIN, HIGH);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
// Receive the next value
irrecv.resume();
}
if(results.value == Off){
apagadoTotal();
}
if(check == 0){
opcionesPrev();
}
if ((unsigned long)(millis() - previousMillis) >= interval) {
if(check == 1){
previousMillis = millis();
opciones2();
}
}
}
void opciones(){
ant = 0;
check = 1;
relayOn();
delay(4000);
irOn();
}
void opciones2(){
check = 0;
ant = 1;
irOff();
delay(3000);
relayOff();
delay(4000);
}
void apagadoTotal(){
check = 0;
blinkRelLed();
irOff();
delay(3000);
relayOff();
delay(4000);
}
void opcionesPrev(){
if(results.value == On){
opciones();
}
}
void irOn(){
irsend.sendRaw(powerOn,68,38);
}
void irOff(){
irsend.sendRaw(powerOff,68,38);
}
void relayOff(){
digitalWrite(RELAY_PIN, LOW);
digitalWrite(RELAY_LED_PIN, LOW);
}
void relayOn(){
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(RELAY_LED_PIN, HIGH);
}
void blinkRelLed(){
digitalWrite(RELAY_LED_PIN, LOW);
delay(100);
digitalWrite(RELAY_LED_PIN, HIGH);
delay(100);
digitalWrite(RELAY_LED_PIN, LOW);
delay(100);
digitalWrite(RELAY_LED_PIN, HIGH);
delay(100);
digitalWrite(RELAY_LED_PIN, LOW);
delay(100);
digitalWrite(RELAY_LED_PIN, HIGH);
delay(100);
digitalWrite(RELAY_LED_PIN, LOW);
delay(100);
digitalWrite(RELAY_LED_PIN, HIGH);
delay(100);
digitalWrite(RELAY_LED_PIN, LOW);
delay(100);
digitalWrite(RELAY_LED_PIN, HIGH);
}
Thank you.
Sorry for my bad English.
Nicolas.