spirit - I borrowed some code from "IRrelay.iso", in the IRremote custom library examples. It makes it cycle through the modes properly when I push the buttons... so thanks for that suggestion! I'm going to have to read through it to get a better understanding of the syntax, but I'm glad that part of the circuit is working now...
PaulS - I've just been following the coding style of all the examples and a lot of the other code I've been seeing posted elsewhere, RE:
if(case){
statement;
}
I understand the following practice, but didn't realize it was so preferred to the other way?
if(case)
{
statement;
}
Is the second formatting more widely accepted? Why are all the examples and tutorials formatted in the first method I showed?
----------------------------------------
I've realized now that because of the way I reorganized my code to make the mode switching work properly, I've stopped displaying the temperature in real time. So now it only displays current temperature whenever I press one of the buttons on the remote to change the fan mode or fan speed (which is not how I want it to work, I want real time temp shown)... Derp.
So I have some work to do to patch that up and better understand this... I'll come back and post an update when I've fixed the rest of the code, but thanks for pointing me in the right direction for switching my modes correctly!
/*
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;
int modeStatus = 0;
int speedStatus = 0;
int prevInput = 0;
LiquidCrystal lcd(8,9,10,11,12,13);
IRrecv irrecv(remote);
decode_results results;
void dump(decode_results *results){
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");
}
switch(results->value){
case 16748655: // EQ button is pressed
if(fanMode == 2)
fanMode=0;
else
fanMode = fanMode+1;
break;
case 16769055: // Vol- button is pressed
if(fanMode == 1 && fanSpeed>0)
fanSpeed=fanSpeed-1;
break;
case 16754775: //Vol+ button is pressed
if(fanMode == 1 && fanSpeed<3)
fanSpeed=fanSpeed+1;
break;
}
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;
}
*/
}
void auto(){
}
void setup() {
lcd.begin(16,2);
dht.begin();
irrecv.enableIRIn();
pinMode(muxA, OUTPUT);
pinMode(muxB, OUTPUT);
}
int on = 0;
unsigned long last = millis();
void loop() {
if (irrecv.decode(&results)){
if(millis()-last>250){
on = !on;
dump(&results);
}
last=millis();
irrecv.resume(); // Receive the next value
}
delay(1000);
}