Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "schedule.h"
- const int floatSensorPin = A1;
- const int refillSensorPin = A0;
- const int dosingRelay1 = 7;
- const int dosingRelay2 = 3;
- const int dosingRelay3 = 4;
- const int dosingRelay4 = 5;
- const int cleanerRelay = 2;
- const int circulationRelay = 6;
- bool dosingRelaysActivated = false;
- // set cleaner on period
- // set cleaner off period to 24 hours - cleaner on period
- const unsigned long cleanerOnInterval = 15*1000; // 15 seconds on
- const unsigned long cleanerOffInterval = 24*60*60*1000 - cleanerOnInterval;
- // circulation on & off periods
- const unsigned long circulationOnTime = 60*1000; // 1 minute in milliseconds
- const unsigned long circulationOffTime = 2*60*1000; // 2 minutes in milliseconds
- // check sensors every 1 second
- const unsigned long sensorReadInterval = 1*1000; // 1 second in milliseconds
- // Individual dosing relay durations (in milliseconds)
- const unsigned long dosingRelayDuration1 = 75*1000; // Water refill duration
- const unsigned long dosingRelayDuration2 = 10*1000; // Nutrient A duration
- const unsigned long dosingRelayDuration3 = 10*1000; // Nutrient B duration
- const unsigned long dosingRelayDuration4 = 10*100; // Acid duration
- // create the scheduler object, 4 slots and no abort handler
- Schedule schedule(4);
- // event handler to turn circulation off for two minutes
- void circ_event_off(void)
- {
- // turn off circulation relay, schedule turn-off in one minute
- digitalWrite(circulationRelay, HIGH);
- schedule.schedule(circulationOffTime, circ_event_off);
- }
- // event handler to run circulation for circulationOnTime period
- void circ_event_on(void)
- {
- // Read the float sensor value
- int floatSensorValue = analogRead(floatSensorPin);
- Serial.print("Float Sensor Value: ");
- Serial.println(floatSensorValue);
- // Check if the float sensor is low (assuming a threshold value for low condition)
- if (floatSensorValue < 500)
- {
- // If the float sensor is low, do not activate the circulation pump
- digitalWrite(circulationRelay, HIGH);
- // and schedule turn-on again after a period (try again)
- schedule.schedule(circulationOffTime, circ_event_on);
- }
- else
- {
- // turn on circulation relay, schedule turn-off in circulationOnTime millis
- digitalWrite(circulationRelay, LOW);
- schedule.schedule(circulationOnTime, circ_event_off);
- }
- }
- void read_sensors(void)
- {
- // Read the float sensor value
- int floatSensorValue = analogRead(floatSensorPin);
- Serial.print("Float Sensor Value: ");
- Serial.println(floatSensorValue);
- // Read the refill sensor value
- int refillSensorValue = analogRead(refillSensorPin);
- Serial.print("Refill Sensor Value: ");
- Serial.println(refillSensorValue);
- // Check if the refill sensor is non-buoyant
- if (refillSensorValue < 500) { // Assuming a threshold value for non-buoyant condition
- // Deactivate dosing relays
- deactivateDosingRelays();
- } else {
- // Check if the float sensor indicates a loss of buoyancy
- if (floatSensorValue < 500) { // Assuming a threshold value for loss of buoyancy
- if (!dosingRelaysActivated) {
- dosingRelaysActivated = true;
- activateDosingRelays();
- }
- } else {
- // If the float sensor is buoyant, deactivate dosing relays
- deactivateDosingRelays();
- }
- }
- }
- void cleaner_on(void)
- {
- // run the cleaner for a period, then turn off
- digitalWrite(cleanerRelay, LOW);
- schedule.schedule(cleanerOnInterval, cleaner_off);
- }
- void cleaner_off(void)
- {
- // turn the cleaner off, schedule turn-on for later
- digitalWrite(cleanerRelay, HIGH);
- schedule.schedule(cleanerOffInterval, cleaner_on);
- }
- void activateDosingRelays()
- {
- // Activate the dosing relays with their specified durations
- digitalWrite(dosingRelay1, LOW);
- delay(dosingRelayDuration1);
- digitalWrite(dosingRelay1, HIGH);
- delay(100); // Delay to prevent interference
- digitalWrite(dosingRelay2, LOW);
- delay(dosingRelayDuration2);
- digitalWrite(dosingRelay2, HIGH);
- delay(100); // Delay to prevent interference
- digitalWrite(dosingRelay3, LOW);
- delay(dosingRelayDuration3);
- digitalWrite(dosingRelay3, HIGH);
- delay(100); // Delay to prevent interference
- digitalWrite(dosingRelay4, LOW);
- delay(dosingRelayDuration4);
- digitalWrite(dosingRelay4, HIGH);
- // Reset the dosing relay activation state
- dosingRelaysActivated = false;
- }
- void deactivateDosingRelays()
- {
- // Deactivate all dosing relays
- digitalWrite(dosingRelay1, HIGH);
- digitalWrite(dosingRelay2, HIGH);
- digitalWrite(dosingRelay3, HIGH);
- digitalWrite(dosingRelay4, HIGH);
- // Delay to prevent interference
- delay(100);
- }
- void setup()
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Initialize relay pins as OUTPUT and set them to LOW (normally closed configuration)
- pinMode(dosingRelay1, OUTPUT);
- pinMode(dosingRelay2, OUTPUT);
- pinMode(dosingRelay3, OUTPUT);
- pinMode(dosingRelay4, OUTPUT);
- pinMode(cleanerRelay, OUTPUT);
- pinMode(circulationRelay, OUTPUT);
- digitalWrite(dosingRelay1, HIGH);
- digitalWrite(dosingRelay2, HIGH);
- digitalWrite(dosingRelay3, HIGH);
- digitalWrite(dosingRelay4, HIGH);
- digitalWrite(cleanerRelay, HIGH);
- pinMode(floatSensorPin, INPUT_PULLUP); // Use internal pull-up for float sensor
- pinMode(refillSensorPin, INPUT_PULLUP); // Use internal pull-up for refill sensor
- // schedule initial events
- // this can be changed to start circulation after a delay, etc, if you want
- schedule.schedule(0, circ_event_on); // start circulation immediately
- schedule.schedule(sensorReadInterval, read_sensors); // read sensors every minute
- schedule.schedule(cleanerOffInterval, cleaner_on); // start cleaner in roughly 24 hours
- }
- void loop()
- {
- // check if it's time for something to happen
- schedule.tick();
- }
Add Comment
Please, Sign In to add comment