Hello, this is my first time posting to an online forum such as this although I have always browsed other peoples posts. Please excuse me if I do not follow the modern etiquette of posting as I will learn over time.
My current project has a ds1820b temperature sensor and a button as well as a 2 module relay to control the heater and pump for my aquarium. I also am using a 16 x 2 LCD to show the current selected target temperature along with the actual water temperature at the same time.
I use 12v into Vin on the nano directly from the power supply plugged into the wall. Then I put a 7508 to regulate the 12v down to 5v which powers the 2 relay module. The temp sensor pulls its 5v directly from the 5v output on the nano with a 4.7k resistor between the data line and 5v.
When it comes to the float switches, I have them wired in series, one switch at NC and the main switching float as NO. The idea is that if the first switch fails and welds shut to NC, the second switch will be a backup breaking the circuit. I have considered allowing the 5v powering the relay module to flow thru the NC switch so when that is triggered, it will literally power down the relay module rather then using them is series for only signal control.
On the AC side of things, both the AC components are hooked up to the NO side of each relay. The pump required a snubber so it is made of a .1uf cap, and a 470 ohm resistor.
What my problem is I have always had trouble understanding the debounce example for some reason. Right now I basically have it check the temp sensor every 15 seconds because if I do not do that the relay for the heater will bounce up and down as the water cools and it is on the edge of the setpoint reading.
What I really want is the temp sensor to be able to read at its regular interval based on the set resolution and only turn the heater on when the sensor value is below the setpoint for lets say 5-20 seconds. In its current programmed state, if I remove the sensor from the water it will not update the display for 15 seconds of course which I want immediate updating but delayed relay triggering. The same debounce could be used for the float switch because right now its simply "Do not check for state change on the float for 1.5 seconds" and in that case, sometimes when a wave bounces the switch the state change check could be at that time and the relay flips. What it should be is when the state changes it must change for 1.5 seconds before the relay pin goes low.
I began to use the debounce a button example but the example assumes, I am using a button to control the ledstate. I would appreciate if someone could help shed some light on how to transform the debounce into a temperature and float debounce so I could understand it a little better. I have posted my code below
/*temperature and ato controller with lcd display for changing the temp setpoint with
2 buttons for lcd control. the relay when high = pump or heater off. when low the pump and heater are on using a 2 relay module from amazon.*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const int heater = 3; //pin for 5v relay
const int pump = 4; //pin for 5v relay
const int floatSwitch = 5; //main waterlevel sensor
//temp screen control variables
const int buttonPin = 6; // the pin that the pushbutton is attached to
float setPoint = 76.00; // counter for the number of button presses
//int heaterState = HIGH;
int buttonState = 0; // current state of the button
int lastButtonState = 0;
//unsigned long lastDebounceTime = 0;
//unsigned long debounceDelay = 1000;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
const long interval = 15000;
const long interval2 = 1500;
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd (0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup(void)
{
lcd.init(); // initialize the lcd
lcd.backlight(); //turn on the backlight
pinMode(heater, OUTPUT);
pinMode(pump, OUTPUT);
digitalWrite (pump, HIGH);
digitalWrite (heater, HIGH);
pinMode(floatSwitch, INPUT_PULLUP); //using the pullup
pinMode(buttonPin, INPUT_PULLUP);
sensors.begin(); //turn on the temp sensor
sensors.setResolution(12); //set temp tolerance
Serial.begin(9600);
}
void loop(void)
{
int waterLevel = digitalRead(floatSwitch);
int buttonState = digitalRead(buttonPin);
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
setPoint += .25;
}
}
lastButtonState = buttonState;
if (setPoint > 85.00) {
setPoint = 72.00;
}
lcd.setCursor(0, 0);
lcd.print("Set:");
lcd.setCursor(11, 0);
lcd.print(setPoint);
lcd.setCursor(0, 1);
lcd.print("Actual:");
lcd.setCursor(11, 1);
lcd.print(sensors.getTempFByIndex(0));
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors.requestTemperatures();
if (sensors.getTempFByIndex(0) < setPoint) {
digitalWrite(heater, LOW); //turn on the heater relay
} else {
digitalWrite(heater, HIGH); //otherwise turn off the relay
}
}
if (currentMillis2 - previousMillis2 >= interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
if (waterLevel == HIGH) { //turn on the pump at this value
digitalWrite(pump, LOW);
} else {
digitalWrite(pump, HIGH); // otherwise turn off the pump normally
}
}
}