2 relay and 2 sensors .??

hi
i have build a uno with 2 temp sensos, for controling 2 relays
but i only have code for 1 temp sensor and 1relay.

code
#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay =7;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(relay,OUTPUT);
}

void loop() {
reading = analogRead(sensorPin);
int celsius = reading/2;
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.setCursor(0,1);

lcd.print(celsius, DEC);
lcd.print((char)223);
lcd.print("C");
if (celsius >35) {
digitalWrite(7,HIGH);
} else {
digitalWrite(7,LOW);
}
delay(500);
lcd.clear();
}
// code stop

in my hardware
a0=in (censor)
a2=in (censor)
d7=out (relay)
d8=out (relay

i want the 2.relay to do the same
but under25 degrees

i hope and pry for help
poul

but i only have code for 1 temp sensor and 1relay.

Why? You are free to add code to deal with the other relay and temperature sensor.

Start by at least defining the other relay pin and adding a pinMode statement for it.

Then, read the other temperature sensor pin, and use the reading the same way you use the first one.

Hi and welcome,

please read the forum rules and how to put code in code tags </>.

You have in your code:

int sensorPin = A0;
int relay =7;

So add:

int sensorPin2 = A2;
int relay2 =8;

and below the line with pinMode(relay...:

  pinMode(relay2,OUTPUT);

Then in the main loop you have to check the temperature range where you want to have an action with your relays.
Something like:

if (celsius <25) {
    digitalWrite(8,HIGH);
  } else {
    digitalWrite(8,LOW);
  }

That might work, but isn't good coding.
You will be better off using states. Have a look at "state machine". There is a tutorial here on the site.

thanks for repply
your right i coud have send the code i tryed to make myself
i just coppyed the lines and made diffrent pins,but coud not make it work
thats why i posted the original code.
ok ill try agen
thanks

It should be easy to convert if you use arrays, something like the following.

#include <LiquidCrystal.h>
int reading[] = {0, 0};
int sensorPin[] = {A0, A2};
int relay[] = {7, 8};

const int NUM_SENSORS = 2;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  for (int i = 0; i < NUM_SENSORS; i++)
  {
    pinMode(relay[i], OUTPUT);
  }

}

// return true if relay should turn on
//
bool checkOn(int reading, int sensorNum)
{
  bool retVal = false;

  switch (sensorNum)
  {
    case 0:
      retVal = reading > 35;
      break;

    case 1:
      retVal = reading < 25;
      break;

    default:
      break;

  } // switch

  return (retVal);


}
void loop() {

  for (int i = 0; i < NUM_SENSORS; i++)
  {
    reading[i] = analogRead(sensorPin[i]);
    int celsius = reading[i] / 2;
    lcd.setCursor(0, 0);
    lcd.print("Temperature: ");
    lcd.setCursor(0, 1);

    lcd.print(celsius, DEC);
    lcd.print((char)223);
    lcd.print("C");

    if (checkOn(celsius, i) ) {
      digitalWrite(relay[i], HIGH);
    } else {
      digitalWrite(relay[i], LOW);
    }
    delay(500);
    lcd.clear();

  }

}
// code stop

It should be easy to convert if you use arrays, something like the following.

There are sometimes advantages to using arrays, but I don't think this is such a case.

The temperature sensors have meaning from where they are located/what they are measuring, so having pin numbers in variables like insideTempPin and outsideTempPin makes more sense to me.

The relays control something. relayPin[0] does not tell me anything about what the relay controls. Names like intakeFanRelayPin and exhaustFanRelayPin do.

You could create array index variables with meaningful names. Then, relayPin[INSIDE] and tempSensorPin[OUTSIDE] convey sufficient information.

But, arrays are not always the answer, when the size of the array is small. Now, if you were trying to deal with 500 LEDs, then arrays are the only way to go.

PaulS:
You could create array index variables with meaningful names.

sorry for stupid question

can you please give me example of this

anilkunchalaece:
sorry for stupid question

can you please give me example of this

#define INSIDE 0
#define OUTSIDE 1

also you could use a constant