Close, but no cigar !
Naming the pin is a good idea but I don’t like the name you chose as it is not descriptive as to what the pin does, but it would work. Better might be
const byte relayPin
const because its value will never change, byte because it uses less memory than an int and relayPin because that’s what it is.
Then in setup() you need
This allows the pin to be set to HIGH or LOW, 5V or 0V respectively.
To change the state of the relayPin you use digitalWrite() as you guessed and it would go where the Serial.print()s are in my code as actions to be taken depending on the temperature. Leave the print commands there though as they could be useful for debugging later.
Next question : what sort of relay do you have and how is it connected to the Arduino ? Surprisingly you may need to set relayPin LOW to turn on the relay and vice versa.
Not using the one I original relay I had planned to use because I cant find it. So now ill be using a Sainsmart solid state relay.
this one → Sainsmart Solid State Relay
Here is my code so far. I feel I’m getting closer and its making a lot more sense. Thanks again UKHeliBob for all the help.
const byte relayPin(8);
temperature = sensors.getTempCByIndex(0);
if (temperature >= 8)
Serial.println("Temperature too high");
else if (temperature <= 7)
Serial.println("Temperature too low");