I'm trying to create an automated fan control circuit for my cubicle, with an IR remote to cycle through the off/manual/auto modes. I think I have the hardware part figured out, but I'm having trouble with the code.
(The flying leads on the left are for checking my demultiplexer outputs once I figure out my mode switching issues... before I actually hook this up to those salvaged Crydom relays and power...)
It will start up properly, displaying the temp in C and F on the first line, and displaying the initial fan mode ("FAN MODE - OFF") on the second line. If I press Vol- or Vol+, nothing happens, which is correct in this mode. When I press the EQ button (to change modes) it will cycle through each mode every 1 second (I have a 1 second delay at the end of my loop, so it cycles the mode every loop after I press the button). It cycles through "FAN MODE - OFF", "MANUAL - OFF", and "AUTO - OFF", and does not stop. The only button press that can stop the loop is Vol+ but only in one circumstance.
If I manage to press the Vol+ button when it is on "MANUAL - OFF", it will change to "MANUAL - HIGH" and stay there. It does not cycle through the LOW or MEDIUM manual settings, just straight from OFF to HIGH. Nothing happens when I press Vol- or Vol+ again. It stays at this value until I press the EQ button again, at which point it goes back to cycling through off, manoff, autooff.
I think I'm close, but I can't figure out the piece of the puzzle I'm missing here, so that each button press will cycle 1 mode and then remain on that mode until the next button press. Does it need some sort of debounce? Am I totally off track here?
Here's my code (read comments at top for short explanation):
/*
This sketch will control a fan circuit with an IR "CarMP3" remote control.
Fan Modes: (controlled with EQ button)
fanMode=0 - OFF
fanMode=1 - MANUAL
fanMode=2 - AUTO
Fan Speeds: (controlled with Vol- and Vol+ buttons)
fanSpeed=0 - OFF
fanSpeed=1 - LOW
fanSpeed=2 - MEDIUM
fanSpeed=3 - HIGH
-Manual mode allows you to select fan speed yourself with Vol- and Vol+ buttons.
-Auto mode adjusts the fan speed based on temperature reading from DHT11 sensor.
The 2 outputs muxA and muxB trigger inputs for a demultiplexer to select 4 outputs,
which are connected to relays that apply power to one line of the fan.
*/
#include <IRremote.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
const int remote = 2;
const int muxA = 4;
const int muxB = 5;
int fanMode = 0;
int fanSpeed = 0;
LiquidCrystal lcd(8,9,10,11,12,13);
IRrecv irrecv(remote);
decode_results results;
void setup() {
lcd.begin(16,2);
dht.begin();
irrecv.enableIRIn();
pinMode(muxA, OUTPUT);
pinMode(muxB, OUTPUT);
}
void loop() {
float t = dht.readTemperature(); //take temp reading from sensor
float tf = (t*1.8)+32; //convert Celcius to Farenheit
lcd.clear();
if (isnan(t)){ //error display for bad temp sensor read
lcd.setCursor(0,0);
lcd.print("Failed DHT read");
}
else{ //display temps
lcd.setCursor(0,0);
lcd.print(t);
lcd.print("C - ");
lcd.print(tf);
lcd.print("F");
}
if (irrecv.decode(&results)){
irrecv.resume(); // Receive the next value
}
switch(results.value){
case 16748655: // EQ button is pressed
if(fanMode == 2)
fanMode=0;
else
fanMode++;
break;
case 16769055: // Vol- button is pressed
if(fanMode == 2){
while(fanSpeed>0){
fanSpeed--;
}
}
break;
case 16754775: //Vol+ button is pressed
if(fanMode == 1){
while(fanSpeed<3){
fanSpeed++;
}
}
break;
/*
case default:
lcd.setCursor(0,1);
lcd.print("Invalid input!");
delay(3000);
*/
}
if(fanMode==2){
if(t>=29){
fanSpeed=3;
lcd.setCursor(0,1);
lcd.print("AUTO - HIGH");
}
else if(t>=26 && t<=28){
fanSpeed=2;
lcd.setCursor(0,1);
lcd.print("AUTO - MEDIUM");
}
else if(t>=23 && t<=25){
fanSpeed=1;
lcd.setCursor(0,1);
lcd.print("AUTO - LOW");
}
else{
fanSpeed=0;
lcd.setCursor(0,1);
lcd.print("AUTO - OFF");
}
}
else if(fanMode==1){
if(fanSpeed==3){
lcd.setCursor(0,1);
lcd.print("MANUAL - HIGH");
}
else if(fanSpeed==2){
lcd.setCursor(0,1);
lcd.print("MANUAL - MEDIUM");
}
else if(fanSpeed==1){
lcd.setCursor(0,1);
lcd.print("MANUAL - LOW");
}
else if(fanSpeed==0){
lcd.setCursor(0,1);
lcd.print("MANUAL - OFF");
}
}
else if(fanMode==0){
fanSpeed=0;
lcd.setCursor(0,1);
lcd.print("FAN MODE - OFF");
}
/*
if(fanSpeed==3){
muxA=1;
muxB=1;
}
else if(fanSpeed==2){
muxA=0;
muxB=1;
}
else if(fanSpeed==1){
muxA=1;
muxB=0;
}
else{
muxA=0;
muxB=0;
}
*/
delay(1000);
}