Serial Interface (easy for some one i hope)

Need a serial read out put for testing added to the code below I try and never seem to get it right. What I'm trying to do is print the temp sensed and if relays are high or low

/*
Controller For outdoor air control (Factor Default 4 fan)
 
Arudino Duemilanove port connections:
Digital Inputs
Push button TB2 at port
Push button TEST at port 

Analog Input
temperature sensors;
TempSens1 at port

Outputs
relay1 at port
relay2 at port
relay3 at port
*/

// Pin Assignments:
const int TempSens1 = 2;    // pin to which temperature sensor 1 is attached to
//const int TB2 = 2;          // pin to which TB2 button is conencted
//const int TEST = 3;         // pin to which TEST button is conencted
const int Relay1 = 12;        //pin to which relay 1 is conencted
const int Relay2 =11;        //pin to which relay 2 is conencted
const int Relay3 = 10;        //pin to which relay 3 is conencted
const unsigned char temp1=1;  //.5 degree*10mV*1024/5V
const unsigned char temp2=47;  //23 degree*10mV*1024/5V
const unsigned char temp3=59;  //29 degree*10mV*1024/5V
float temperature = 0; //added for serial read
// Variables:
int TempSens1_Reading= 0;         // temperature sensor 1 reading
//int TB2buttonState;             // the current reading from the input pin
//int TB2lastButtonState = LOW;   // the previous reading from the input pin
//long TB2lastDebounceTime = 0;  // the last time the output pin was toggled
//int TESTbuttonState;             // the current reading from the input pin
//int TESTlastButtonState = LOW;   // the previous reading from the input pin
//long TESTlastDebounceTime = 0;  // the last time the output pin was toggled
//long debounceDelay = 150;    // the debounce time; increase if the output flickers

//int read_TB2(){
  //int reading = digitalRead(TB2);
  //if (reading != TB2lastButtonState) {
    //TB2lastDebounceTime = millis();
  //} 
  //if ((millis() - TB2lastDebounceTime) > debounceDelay) {
    //TB2buttonState = reading;
  //}
  //TB2lastButtonState = reading;
//return TB2buttonState;
//}
//int read_TEST(){
  //int reading = digitalRead(TEST);
  //if (reading != TESTlastButtonState) {
    //TESTlastDebounceTime = millis();
  //} 
  //if ((millis() - TESTlastDebounceTime) > debounceDelay) {
    //TESTbuttonState = reading;
  //}
  //TESTlastButtonState = reading;
  //return TESTbuttonState;
//}
void setup() {
// Assign inputs and outputs
  //pinMode(TB2 , INPUT);      //set TB2 pin an input pin
  //pinMode(TEST , INPUT);    //set TEST pin an input pin
  pinMode(Relay1, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay2, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay3, OUTPUT);  //set Relay1 pin an ouput pin
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
}

void loop() {
  //while (read_TEST()==true){
  //digitalWrite(Relay1, HIGH);
  //digitalWrite(Relay2, HIGH);
  //digitalWrite(Relay3, HIGH);
  //}
  
  //while (read_TEST()==false && read_TB2()==false){
  //digitalWrite(Relay1, LOW);
  //digitalWrite(Relay2, LOW);
  //digitalWrite(Relay3, LOW);
 // }
  //while (read_TEST()==false && read_TB2()==true){
    TempSens1_Reading = analogRead(TempSens1);
    
    if (TempSens1_Reading >=temp1){  
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp2){  
      digitalWrite(Relay1, HIGH);      
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp3){  
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, HIGH);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading <=temp3){ 
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
    }
    if (TempSens1_Reading <=temp2){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
       }
    if (TempSens1_Reading <=temp1){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, LOW); 
    }
  }
//}

Go on give us a clue, what is it doing that you don't like.

Why don't you remove all the commented-out lines and repost it?

It would help a lot if you would explain what it is you're trying to do.

I'll guess.
Assuming you would like to see the value of the sensor on the serial monitor, there are a couple of example programs to look at in the Arduino IDE.

One is at: File->Examples->Analog->AnalogInOutSerial

As it shows, set up the serial connection in setup() using Serial.begin(9600)
and print the value in loop() using Serial.print or println as shown.

Open the Serial Monitor using the rightmost button in the Arduino IDE. If it prints unrecognisable characters, use the drop down, bottom right to set 9600 baud.

(Is there a playground page for this?)

I guessed, but I may be wrong.

HTH
GB

Below I took out the extra code commented out. The analog prints fine but I have no idea to print if a digital pin is high or low.

/*
Controller For outdoor air control (Factor Default 4 fan)
 
Arudino Duemilanove 
port connections:
Digital Inputs
Analog Input
temperature sensors;
TempSens1 at port

Outputs
relay1 at port
relay2 at port
relay3 at port
*/

// Pin Assignments:
const int TempSens1 = 2;    // pin to which temperature sensor 1 is attached to
//const int TB2 = 2;          // pin to which TB2 button is conencted
//const int TEST = 3;         // pin to which TEST button is conencted
const int Relay1 = 12;        //pin to which relay 1 is conencted
const int Relay2 =11;        //pin to which relay 2 is conencted
const int Relay3 = 10;        //pin to which relay 3 is conencted
const unsigned char temp1=1;  //.5 degree*10mV*1024/5V
const unsigned char temp2=47;  //23 degree*10mV*1024/5V
const unsigned char temp3=59;  //29 degree*10mV*1024/5V

// Variables:
int TempSens1_Reading= 0;         // temperature sensor 1 reading

void setup() {
  // start serial
  Serial.begin(9600);
  Serial.println("LM35");
  
  // Assign inputs and outputs
  pinMode(Relay1, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay2, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay3, OUTPUT);  //set Relay1 pin an ouput pin
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
}

void loop() {
    TempSens1_Reading = analogRead(TempSens1);
        Serial.println(TempSens1_Reading);
        delay(1000);
    if (TempSens1_Reading >=temp1){  
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp2){  
      digitalWrite(Relay1, HIGH);      
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp3){  
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, HIGH);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading <=temp3){ 
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
    }
    if (TempSens1_Reading <=temp2){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
       }
    if (TempSens1_Reading <=temp1){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, LOW); 
    }
  }

try another if statement like

if (realy1 = HIGH){
Serial.println("Relay1 State = HIGH")
}

not sure if that will work correctly but its worth a shot. thats how i would try it

Well, I wouldn't try value = HIGH, as that's assignment g, but you can probably make your life easier...

FWIW, your logic is kinda funny... It's like you're walking up a ladder, and then coming back down... You have to set it twice for every temp value less than temp3...

  byte relay1_st = HIGH;
  byte relay2_st = HIGH;
  byte relay3_st = HIGH;

  if( TempSens1_Reading >= temp2 && TempSens1_Reading < temp3 ) {
    relay2_st = LOW;
  }
   else if( TempSens1_Reading < temp2 ) {
    relay1_st = LOW;
    relay2_st = LOW;

    if( TempSens1_Reading < temp1 )
      relay3_st = LOW;

  }

  digitalWrite(Relay1, relay1_st);
  digitalWrite(Relay2, relay2_st);
  digitalWrite(Relay3, relay3_st);

  Serial.println(relay1_st, DEC);
  Serial.println(relay2_st, DEC);
  Serial.println(relay3_st, DEC);

HTH,

!c

Ok here is my newest version seems to be good. I'm going to start trying to add a dead-band now. It is short cycling when the temps are close to the activation points. Also Drone it makes sense what your saying but I did not understand your code.

/*
Controller For outdoor air control (Factor Default 4 fan)
 
Arudino Duemilanove port connections:
Digital Inputs
Push button TB2 at port
Push button TEST at port 

Analog Input
temperature sensors;
TempSens1 at port

Outputs
relay1 at port
relay2 at port
relay3 at port
*/

// Pin Assignments:
const int TempSens1 = 2;       // pin to which temperature sensor 1 is attached to
const int Relay1 = 12;         // pin to which relay 1 is conencted
const int Relay2 =11;          // pin to which relay 2 is conencted
const int Relay3 = 10;         // pin to which relay 3 is conencted
const unsigned char temp1=1;   // 1 degree*10mV*1024/5V
const unsigned char temp2=46;  // 23 degree*10mV*1024/5V
const unsigned char temp3=58;  // 29 degree*10mV*1024/5V

// Variables:
int TempSens1_Reading= 0;      // temperature sensor 1 reading
float temp= 0;
int val1= LOW; // State of Relay1
int val2= LOW; // State of Relay2
int val3= LOW; // State of Relay3

// Start Program
void setup() {
 
  // start serial
  Serial.begin(9600);
  Serial.println("LM35");
  
  // Assign inputs and outputs
  pinMode(Relay1, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay2, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay3, OUTPUT);  //set Relay1 pin an ouput pin
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
}

void loop() {
    TempSens1_Reading = analogRead(TempSens1);
      
    temp = (5.1*TempSens1_Reading*100/1024);

    if (TempSens1_Reading >=temp1){  
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp2){  
      digitalWrite(Relay1, HIGH);      
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading >=temp3){  
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, HIGH);
      digitalWrite(Relay3, HIGH);
    }
    if (TempSens1_Reading <=temp3){ 
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
    }
    if (TempSens1_Reading <=temp2){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, HIGH); 
       }
    if (TempSens1_Reading <=temp1){ 
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, LOW); 
    }
      Serial.print("TempSens1_Reading= ");

       Serial.print(TempSens1_Reading);
      delay(0000);
       Serial.print("\t");
       Serial.print("Temp C= ");
        
       Serial.println(temp);
       
//Read value of outputs and print     
      val1 = digitalRead(Relay1);
Serial.print("Relay1 State = ");
Serial.print(val1);
Serial.println("\t");

      val2 = digitalRead(Relay2);
Serial.print("Relay1 State = ");
Serial.print(val2);
Serial.println("\t");

      val3 = digitalRead(Relay3);
Serial.print("Relay1 State = ");
Serial.print(val3);
Serial.println("\t");

      delay(2000);        
  }

Ok here is my newest version seems to be good. I'm going to start trying to add a dead-band now. It is short cycling when the temps are close to the activation points. Also Drone it makes sense what your saying but I did not understand your code.

48s,

The problem you have is that your if chain is illogical, and generates unexpected consequences.

Consider that for values greater than Temp3 or less than Temp1, you will have tripped the coils on your relay 3 times in succession! Not only is this an unnecessary activity, it also makes your code difficult to follow (and therefore modify in the future), and may present issues with your relays, switching their states three times in rapid succession often.

You have:

  if >= temp1 : do something #1
  if >= temp2 : do something #2 
  if >= temp3 : do something #3
  if <= temp3 : do something #2
  if <= temp2 : do something #1
  if <= temp1 : do something #4

Do you see the problem here? Value = temp3 will match 4 of those if statements, value = temp1 will match four, values greater than temp3 will match three, and so on.

See the tests? If you already know that your value is >= temp3, why ask right after that if your value is <= temp3?

What I did in the code is this:

  1. Establish a base state of the pins (state equivalent to reading >= temp3)
  byte relay1_st = HIGH;
  byte relay2_st = HIGH;
  byte relay3_st = HIGH;
  1. Establish how pin states for the relays should vary from the base states, based on the temp reading - condensing your six tests to three. Basically, we're asking if our assumption that we're >= temp3 is wrong, and adjusting as-needed.
     // value is between temp2 and temp3
  if( TempSens1_Reading >= temp2 && TempSens1_Reading < temp3 ) {
    relay2_st = LOW;
  }
     // value is less than temp2
   else if( TempSens1_Reading < temp2 ) {
    relay1_st = LOW;
    relay2_st = LOW;

       // we know we're less than temp2, so it makes sense to ask if
       // less than temp1 here

    if( TempSens1_Reading < temp1 )
      relay3_st = LOW;

  }

Result =

READING                  RLY1      RLY2      RLY3
-----------            ------      ------      -------
value >= temp3            HIGH      HIGH      HIGH
temp3 > value >= temp2      HIGH      LOW      HIGH
temp2 > value >= temp1      LOW      LOW      HIGH
temp1 > value            LOW      LOW      LOW
  1. After establishing pin states, call digitalWrite() only once per relay pin (save lots of cycles, and possible wear/tear on mechanical relays)
  digitalWrite(Relay1, relay1_st);
  digitalWrite(Relay2, relay2_st);
  digitalWrite(Relay3, relay3_st);
  1. Print the values of each pin state (as you originally requested) without having to read back the pin values -- as there's no need to read the values, since you know what they are when you set them.
  Serial.println(relay1_st, DEC);
  Serial.println(relay2_st, DEC);
  Serial.println(relay3_st, DEC);

Be careful with if chains, if your states are mutually exclusive (which they are!) you should be either using if/else chains, or switch statements.

I want to stress that the reason I point these things out is to help you avoid bugs. I understand that you just wanted to know how to serial print a value, but I would hate to see you struggling with why something isn't working properly. While the changes I offer are more efficient, that's a side-benefit to being easier to fix when you want to make changes =)

Note, for a different way to do it, you can do a more straight-forward exclusive set of tests, but it ends up being more code:

if value >= temp3 {} 
  else if value >= temp2 {}
  else if value >= temp1 {}
  else { }

See, if the value is >= temp3, it can't possibly be less than temp3, so for values >= temp3, you'll only hit one block of code.

!c

I think I have followed all your directions. See what you think

/*
Controller For outdoor air control (Factor Default 4 fan)
 
Arudino Duemilanove port connections:

Analog Input
temperature sensors;
TempSens1 at port

Outputs
relay1 at port
relay2 at port
relay3 at port
*/

  // Pin Assignments:
const int TempSens1 = 2;       // pin to which temperature sensor 1 is attached to
const int Relay1 = 12;         // pin to which relay 1 is conencted
const int Relay2 =11;          // pin to which relay 2 is conencted
const int Relay3 = 10;         // pin to which relay 3 is conencted
const unsigned char temp1=1;   // 1 degree*10mV*1024/5V
const unsigned char temp2=46;  // 23 degree*10mV*1024/5V
const unsigned char temp3=58;  // 29 degree*10mV*1024/5V

  

  // Variables:
int TempSens1_Reading= 0;      // temperature sensor 1 reading
float temp= 0;



  // Start Program
void setup() {
    
  // start serial
  Serial.begin(9600);
  Serial.println("LM35");
  
  // Assign inputs and outputs
  pinMode(Relay1, OUTPUT);  //set Relay1 pin an ouput pin
  pinMode(Relay2, OUTPUT);  //set Relay2 pin an ouput pin
  pinMode(Relay3, OUTPUT);  //set Relay3 pin an ouput pin
  
}

void loop() {
  //Read and Calculate Temp, Setup Relay states 
    TempSens1_Reading = analogRead(TempSens1);
    temp = (5.1*TempSens1_Reading*100/1024);
   
    byte relay1_st= HIGH;
    byte relay2_st= HIGH;
    byte relay3_st= HIGH;
    
     // value is between temp2 and temp3
  if( TempSens1_Reading >= temp2 && TempSens1_Reading < temp3 ) {
    relay2_st = LOW;
    
  }
     // value is less than temp2
   else if( TempSens1_Reading < temp2 ) {
    relay2_st = LOW;
    relay3_st = LOW;

     // less than temp1 here
   if( TempSens1_Reading < temp1 )
    relay3_st = LOW;
   }    
 
      digitalWrite(Relay1, relay1_st);
      digitalWrite(Relay2, relay2_st);
      digitalWrite(Relay3, relay3_st);
   
      Serial.print("TempSens1_Reading= ");
      Serial.print(TempSens1_Reading);
      Serial.print("\t");
      Serial.print("Temp C= ");
      Serial.println(temp);
      Serial.print("Relay1_st= "); 
      Serial.println(relay1_st, DEC);
      Serial.print("Relay2_st= ");
      Serial.println(relay2_st, DEC);
      Serial.print("Relay_st= ");
      Serial.println(relay3_st, DEC);

      delay(2000);        
  }

You have a typo here:

  else if( TempSens1_Reading < temp2 ) {
    relay2_st = LOW;
    relay3_st = LOW;

That should probably be:

  else if( TempSens1_Reading < temp2 ) {
    relay1_st = LOW;
    relay2_st = LOW;

Otherwise it looks good. Does the code work for you?

!c

Drone Thank You for your help it looks like this will work great. I am designing one more program for the same machine (to test which is more effective) and if I run into snags I hope that your are around to assist.