Thermostat LCD - Help ME

Hello everyone, I have decided to develop to maturity tesina a thermostat to 3 independent zones, with instant display of temperature on a 2x16 LCD, while every hour 24 hours running average of the three temperature sensors, and sends Serial to PC that stores the data in a data logger in the form of text.
Unfortunately, it is a problem, like any thermostat, must turn on / off or a stove or a conditioner, to simulate the operation I thought led to 6, 2 for each zone, which indicate whether the activation of heating / cooling I was only a 3 door, however, and son forced me to use a mux (HCF4051BE) in this way I got up to 8 outputs with 3 inputs selection.
I reported the operation led to the program, but it seems this piece of software does not go down to the remainder.
The fact is that the program does not work as expected ... someone can help me?
Thanks for your attention
Luca

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10);
//LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)

int potPin = 4;
int potPin2 = 5;
int potPin3 = 3;
float temperature=0;
float temperature2=0;
float temperature3=0;
float t1min,t2min,t3min,t1max,t2max,t3max;
int val;
int val2;
int val3;
float valtrim;
int puls = 3;
int puls2 = 5;
int trimmer =2;
int state=0;
int state2=0;
int state3=0;
int state4=0;
int menumode = 0;
int i,cost;
int A = 13;
int B = 6;
int C = 4;

void setup() {
pinMode(potPin,INPUT);
pinMode(potPin2,INPUT);
pinMode(potPin3,INPUT);
pinMode(puls,INPUT);
pinMode(puls2,INPUT);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
Serial.begin(9600);
}

void loop() {

val = analogRead(potPin);
temperature = ((5.0 * val * 100.0)/1023.0);

val2 = analogRead(potPin2);
temperature2 = ((5.0 * val2 * 100.0)/1023.0);

val3 = analogRead(potPin3);
temperature3 = ((5.0 * val3 * 100.0)/1023.0);

menu();
delay(1500);

state = digitalRead(puls);
delay(100);
if (state == HIGH) {
if (menumode == 0){
menumode = 1;
} else {
if (menumode == 1){
menumode = 2;
}else{
if (menumode == 2){
menumode = 0;
} } } }

if (state == LOW) {
if (menumode == 0){
menumode = 0;
} else {
if (menumode == 1){
menumode = 1;
}else{
if (menumode == 2){
menumode = 2;
} } } }

menu();

state2 = digitalRead(puls2);
delay(100);
if (state2 == HIGH) {
if (menumode == 0){
menumode = 3;
while(state3==0 || state4==0){
menu();
if(analogRead(trimmer)<512){
cost=0;
menu();
if(digitalRead(puls)==HIGH && digitalRead(puls2)==0){
while(digitalRead(puls2)==0){
t1min=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
if(analogRead(trimmer)>=512){
cost=1;
menu();
if(digitalRead(puls)==HIGH){
while(digitalRead(puls2)==0){
t1max=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
state3 = digitalRead(puls);
state4 = digitalRead(puls2);
delay(1500);
} menumode=0;state3=0;state4=0;
}

else {
if (menumode == 1){
menumode = 4;
while(state3==0 || state4==0){
menu();
if(analogRead(trimmer)<512){
cost=0;
menu();
if(digitalRead(puls)==HIGH && digitalRead(puls2)==0){
while(digitalRead(puls2)==0){
t2min=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
if(analogRead(trimmer)>=512){
cost=1;
menu();
if(digitalRead(puls)==HIGH){
while(digitalRead(puls2)==0){
t2max=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
state3 = digitalRead(puls);
state4 = digitalRead(puls2);
delay(1500);
} menumode=0;state3=0;state4=0;
}

else{
if (menumode == 2){
menumode = 5;
while(state3==0 || state4==0){
menu();
if(analogRead(trimmer)<512){
cost=0;
menu();
if(digitalRead(puls)==HIGH && digitalRead(puls2)==0){
while(digitalRead(puls2)==0){
t3min=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
if(analogRead(trimmer)>=512){
cost=1;
menu();
if(digitalRead(puls)==HIGH){
while(digitalRead(puls2)==0){
t3max=analogRead(trimmer)/10;
menu();
delay(100);
}
menu();
}}
state3 = digitalRead(puls);
state4 = digitalRead(puls2);
delay(1500);
} menumode=0;state3=0;state4=0;
}
} } }

delay(50);
}
int led(){
if (temperature <= t1min) {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
if (temperature >= t1max) {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}
if (temperature2 <= t2min) {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}
if (temperature2 >= t2max) {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
if (temperature3 <= t3min) {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
if (temperature3 >= t3max) {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
}
int menu(){

if (menumode == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Temperatura 1");
lcd.setCursor(0,1);
lcd.print(temperature);
lcd.print(" C");
}
if (menumode == 1) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Temperatura 2");
lcd.setCursor(0,1);
lcd.print(temperature2);
lcd.print(" C");
}
if (menumode == 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Temperatura 3");
lcd.setCursor(0,1);
lcd.print(temperature3);
lcd.print(" C");
}
if (menumode == 3) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp MIN:");
lcd.print(t1min);
lcd.print("C");
if(cost==0){
lcd.print("<-");
}
lcd.setCursor(0,1);
lcd.print("Temp MAX:");
lcd.print(t1max);
lcd.print("C");
if(cost==1){
lcd.print("<-");
}
}
if (menumode == 4) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp MIN:");
lcd.print(t2min);
lcd.print("C");
if(cost==0){
lcd.print("<-");
}
lcd.setCursor(0,1);
lcd.print("Temp MAX:");
lcd.print(t2max);
lcd.print("C");
if(cost==1){
lcd.print("<-");
}
}
if (menumode == 5) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp MIN:");
lcd.print(t3min);
lcd.print("C");
if(cost==0){
lcd.print("<-");
}
lcd.setCursor(0,1);
lcd.print("Temp MAX:");
lcd.print(t3max);
lcd.print("C");
if(cost==1){
lcd.print("<-");
}
}}

Is there a particular part that does not work?

The part of the program that examines whether the limits tempMAX - tempMIN are higher or lower than the temperature detected by the sensor, I can not turn on the LED signaling.
In a program apart from the multiplexer works perfectly ... I do not understand ...
thanks
luca

As a general comment, I'd say you might find it easier to see the structure of your program with consistent indentation.

With the use of a few arrays instead of "temperature[2|3]" and "t[1|2|3]min" and "t[1|2|3]max", you could factor a lot of code and make it a lot simpler and shorter.

Just £0.02

Can help me? :cry:

The part of the program that examines whether the limits tempMAX - tempMIN are higher or lower than the temperature detected by the sensor

I assume you're talking about the code in "led ()"?

It isn't clear to me what you intend this code to do.

If, for example "temperature" is less than "t1min", you switch on LEDA and LEDB, and LEDC is off.

However, if then "temperature3" is greater than "t3max" (and I don't suppose there is any reason it couldn't be), you turn off LEDA and LEDB, and turn on LEDC.

So what you did in the first condition is completely reversed.

Is that what you intended?

[EDIT] Sorry, missed the bit about the mux earlier.

what you say is right.
the problem is that the program without the code led the work, while, it does not work ...
understand?

In what way does it not work?
Does it freeze somehow?
Does it simply not do what you expect it to do, and if this is the case, what do you expect it to do?

I've rearranged some of your code, and changed the individual variables that held the current/min/max values into a structure called Temperature.

This way you can assign values in this manner:

Temperature temp;

temp.current = 20;
temp.max = 25;
temp.min = 15;

I've created a function called getTemperature(), it returns a float representing the current temperature.

http://arduino.pastebin.com/f48c770e1

If I where you, I'd actually rewrite the program.

You've got the ideas right, how to do things, but this code was extreamly difficult to comprehend if you're not the author.

An important rule, for me, is to never copy-paste code. That is, if you find yourself writing the same code more then once, there's probably a better way of doing it.

If you separate the logic from the code that handles the visuals even more, it's a start.

It's also almost always best to get rid of as many delay() statements as possible.

As I see it, I presume your program need to do these actions, preferebly in this sequence:

//Read temperatures
//Set min/max values
//Select menu mode
//Print current menu
//Set LED/actuator states
//Write to Serial

If you made an array of "Temperature", you'd simplify your menu code too.

what does not work, does not turn the led ...
if excess temp1max not turn the corresponding LED, now I have changed some combinations, I noted that were in conflict with the program, but the problem persists ...

Alphabeta thanks but there is something wrong in your program;)

if (temperature < t1min) {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
if (temperature > t1max) {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
if (temperature2 < t2min) {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}
if (temperature2 > t2max) {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
if (temperature3 < t3min) {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
if (temperature3 > t3max) {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}

Is it just that you're turning on the right LED, and then immediately turning it off because another condition is met?

Alphabeta thanks but there is something wrong in your program;)

Actually, I did not expect my program to work, as it is identical to your program. I've just knocked of quite a bit or RAM usage, and added a simple function.
:wink:

What is the functionality you're after?

I cannot seem to understand what you want your program to do.

ok, the program performs well:

  • detects temperature.

  • shows temperature Menu1.

  • if I press P1 shows menu2 MENU3 (the other 2 sensors)

  • if I press P2 on Menu1 opens Menu1-2 (where required temp1min, temp1max)

  • set temp1min and temp1max with trimmer

  • active in the cases led temperature1 <temp1min, temperature2> temp1max etc.

helpppp i need it for tomorrow!!!!! :o :cry:

You said it was working:

ok, the program performs well

what is not working?

The led not working...
i do no why...
the code is ok, but still off :-[

But with the arrangement you've got, you can only have one LED lit at a time, can't you?

With the software you've got, it is going to be difficult to multiplex the LEDs fast enough.

Have you run out of pins?
You know that you can use analogue pins as digital.

To set an analogue pin as a digital output, analogue pin zero is pin number 14, analogue pin one is number 15 and so on.
so, to set analogue pin zero as an output, "pinMode (14, OUTPUT)".

Maybe best to put your lights on these - it'll be quicker with the program as you've written it.