I would greatly appreciate if some one could look at my code and point out what I don't need or where it can be simplified.
I have an arduino uno, terminal shield and a 16x2 lcd shield all stacked.
I will be completely honest here, I have cut, shut merged and modified some codes to do what I require.
Overview = check and control output water temperature from a reflux still.
Water on at 52 degrees C (temperature normal), water on faster at 55 degrees C (temperature hot) and water off at below 50 degrees C (temperature cold).
Servo connected directly to tap and calibrated to be closed at 120 degrees(servo angle) (cold), open fast at 40 degrees(servo angle) and normal flow at 70 degrees (servo angle).
LCD reads Temp Now @(current temp from dallas 18b20 with 4k7 resistor) and Temp Set @ 50-55. If temperature travels over 65 degrees lcd will display CHECK WATER.
I have road tested the above with the tap setup for my still and a cup of hot water for the temp and can confirm it works.
Problems,
I have an external power supply plugged in to the arduino but the lcd flashes upon update/refresh even with and without the usb plugged in. (I will get a new one better suited)
The servo jitters even when at room temp 28 degrees C.
Sometimes it loses the plot over 55degrees with the lcd becoming scrambled and the servo looses the plot too. I guess its my code.
I will be using this temperature based code for a couple of other things too being in the hot Australian climate.
Also take it easy on me as its my first challenge of arduino and code for me.
Thank you,
Russell.
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal.h>
int DS18S20_Pin = 3; // Set temperature sensor in pin 0
int thresholdcold = 50; // Threshold for cold temperature
int thresholdhot = 55; // Threshold for hot temperature
int thresholdnormal = 52; // Threshold for normal temperature
int servocold = 120; // ***** Water should be off ******
int servohot = 40; // **** water flow approx 1200ml per minute ****
int servonormal = 70; // *** normal water flow eg. 700ml per minute ***
//int servotrickle = 100; // *** trickle water flow eg. 200ml per minute ***
int previousPosition = 70;
int ServoDelay = 0; // Servo delay
int led = 13; // Red LED
int Relay_Pin = 12; //only if required **** not used for this application****
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
Servo servo1;
float Temp1 = 0;
float Temp2 = 0;
float AverageTemp = 0;
#define ONE_WIRE_BUS 3// Data wire is plugged into pin 3 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
void setup()
{
Serial.begin(9600); // Begin communicating with computer
servo1.attach(10); // Attaches servo to specified pin
pinMode(13, OUTPUT);
//pinMode(12, OUTPUT);
}
void loop() {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
// Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.println(sensors.getTempCByIndex(1)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
// delay(2000);
Temp1 = sensors.getTempCByIndex(1); //Store sensor 1 value
Temp2 = sensors.getTempCByIndex(0); //Store sensor 2 value
AverageTemp = (Temp1 + Temp2) / 2; //Average value of sensor 1 & sensor 2
Serial.print("Average temp is...");
Serial.println (AverageTemp);
lcd.begin( 16, 2 );
//Print some initial text to the LCD.
lcd.setCursor( 0, 0 ); //top left
// 1234567890123456
lcd.print( "Temp Now @:"); lcd.println(sensors.getTempCByIndex(1));
//
lcd.setCursor( 0, 1 ); //bottom left
// 1234567890123456
lcd.print( "Temp Set @:50-55");
if (Temp1 > 65 || Temp2 > 65 ) {
lcd.clear();
lcd.print( "**CHECK WATER**");
digitalWrite(led, HIGH); // Testing spikes
Serial.println ("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
delay(200);
if (AverageTemp <= thresholdcold) // If temperature is above the threshold, activate sequence
{
if (servocold != previousPosition) {
for (previousPosition != 0; previousPosition < servocold; previousPosition += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servocold);
previousPosition = servocold;
}
}
else if (AverageTemp >= thresholdhot) // If temperature is above the threshold, activate sequence
{
if (servohot != previousPosition) {
for (previousPosition != thresholdhot; previousPosition -= 1;) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servohot);
previousPosition = servohot;
}
digitalWrite(led, HIGH);
digitalWrite(Relay_Pin, HIGH);
}
else if (AverageTemp >= thresholdnormal) // If Fract is above the threshold, activate sequence
{
while (previousPosition != servonormal) {
if (previousPosition < servonormal) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition++) ; // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
else // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition--); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servonormal);
previousPosition = servonormal;
digitalWrite(led, LOW);
digitalWrite(Relay_Pin, LOW);
}
}
}