Hi all,
I have a sketch with a temp sensor and sending signal to leds and relais whenever the temp hits a certain value.
So the main point is that when the temp is above 30 the red led gets a signal and relais turns off, when the temp is below 23 the yellow led gets a signal and relais turns on and when in between the green led gets signal and relais turns off.
What i want to do now is add another which is when temp goes below 15 sending red led signal and turn relais off. Now how to add a fourth criterium to the formula?
// Program: DS18B20 temperature sensor
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define RELAY1 A1 // Relay heating
#define RELAY2 A2 // Relay heating
int green = 8; // green LED warm
int yellow = 9; // yellow LED cold
int red = 10; // red LED alarm
const int analogInPin = A0;
int sensorValue = 1;
unsigned long int avgValue;
float b;
int buf[10],temp;
// DS18B20 signal pin port
#define ONE_WIRE_BUS 3
// Defines an instance of oneWire to communicate with the sensor
OneWire oneWire(ONE_WIRE_BUS);
// Stores minimum and maximum temperatures
float tempMin = 999;
float tempMax = 0;
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1;
// Initializes the LCD
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
void setup(void)
{
Serial.begin(9600);
sensors.begin();
// Finds and displays sensor addresses
Serial.println("Locating DS18B20 sensors...");
Serial.print("Were found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" sensors.");
if (!sensors.getAddress(sensor1, 0))
Serial.println("Sensors not found !");
// Shows the address of the sensor found on the bus
Serial.print("Sensor address: ");
show_sensor_address (sensor1);
Serial.println();
Serial.println();
lcd.begin(20, 4);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
}
void show_sensor_address(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// Add zeros if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void loop()
{
// Read sensor information
sensors.requestTemperatures();
float tempC = sensors.getTempC(sensor1);
// Updates minimum and maximum temperatures
if (tempC < tempMin)
{
tempMin = tempC;
}
if (tempC > tempMax)
{
tempMax = tempC;
}
// Shows data on the serial monitor
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Min : ");
Serial.print(tempMin);
Serial.print(" Max : ");
Serial.println(tempMax);
// Shows Temp data on the LCD
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp.: ");
//Degree symbol
lcd.write(223);
lcd.print("C");
lcd.setCursor(7,0);
lcd.print(tempC);
lcd.setCursor(0,1);
lcd.print("L: ");
lcd.setCursor(3,1);
lcd.print(tempMin,2);
lcd.setCursor(12,1);
lcd.print("H: ");
lcd.setCursor(15,1);
lcd.print(tempMax,2);
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
for(int i=0;i<10;i++)
{
buf[i]=analogRead(analogInPin);
delay(10);
}
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++)
avgValue+=buf[i];
float pHVol=(float)avgValue*5.0/1024/6;
float phValue = -5.70 * pHVol + 21.34;
Serial.print("sensor = ");
Serial.println(phValue);
delay(20);
// Shows pH data on the LCD
lcd.setCursor(0,3);
lcd.print("pH: ");
lcd.setCursor(4,3);
lcd.print(phValue,2);
float temperature = sensors.getTempCByIndex(0);
if(temperature > 30)
{
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,HIGH);
lcd.setCursor(1,2);
lcd.print("ALARM ALARM ALARM");
digitalWrite(RELAY1,1);
digitalWrite(RELAY2,1);
}
else if(temperature > 23)
{
digitalWrite(green,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
lcd.setCursor(0,2);
lcd.print("Heating off");
digitalWrite(RELAY1,1);
digitalWrite(RELAY2,1);
}
else {
digitalWrite(green,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(red,LOW);
lcd.setCursor(0,2);
lcd.print("Heating on");
digitalWrite(RELAY1,0);
digitalWrite(RELAY2,0);
}
}