Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PIN DEFINITIONS
- #define SENS_WL_1 A0 // water level sensor for tank 1 (analog in pin)
- #define SENS_WL_2 A1 // water level sensor for tank 2 (analog in pin)
- #define SENS_WL_3 A2 // water level sensor for tank 3 (analog in pin)
- #define PUMP_1 2 // pump for tank 1 (digital pin, can be PWM)
- #define PUMP_2 3 // pump for tank 1 (digital pin, can be PWM)
- #define PUMP_3 4 // pump for tank 1 (digital pin, can be PWM)
- // END: PIN DEFINITIONS
- //#define DEBUG // debug - sensor states and motor outputs
- //#define DEBUG_V // verbose debug - analog data
- #define RES_CNT 3 // number of tanks
- #define TIME_LOOP 500 // don't run at maximum speed, it is not needed
- #define TIME_TRIG 2000 // wait this amount of time after trigger
- #define TIME_RUN 30000 // run pump for this amount of millis
- #define TIME_MAX 14400000 // 4 hours - a "bit" less then 4294967295
- const unsigned long sensorDebounce = TIME_TRIG;
- const unsigned long pumpRunTime = TIME_RUN;
- int i;
- int aread;
- bool state;
- unsigned long millisNow;
- int pumps[] = { PUMP_1 , PUMP_2 , PUMP_3 };
- int sensors[] = { SENS_WL_1, SENS_WL_2, SENS_WL_3 };
- int sensorLevels[] = { 500 , 500 , 500 };
- bool sensorStates[] = { false , false , false };
- bool lastSensorStates[] = { false , false , false };
- unsigned long sensorDebounces[] = { 0 , 0 , 0 };
- unsigned long pumpTimes[] = { 0 , 0 , 0 };
- void setup() {
- for (i = 0; i < RES_CNT; i++) {
- pinMode(pumps[i], OUTPUT);
- digitalWrite(pumps[i], LOW);
- }
- #ifdef DEBUG
- Serial.begin(9600);
- #endif
- }
- void(* restartController) (void) = 0;
- void loop() {
- // restart controller if it was running for more then 4 hours
- millisNow = millis() + pumpRunTime;
- if (millisNow >= TIME_MAX) {
- restartController();
- }
- for (i = 0; i < RES_CNT; i++) {
- // Sensor reading with delay from first detection
- aread = analogRead(sensors[i]);
- state = aread < sensorLevels[i];
- #ifdef DEBUG_V
- Serial.print("Sensor ");
- Serial.print(i);
- Serial.print(", value ");
- Serial.print(aread);
- Serial.print("\n");
- #endif
- if (state != lastSensorStates[i]) {
- sensorDebounces[i] = millisNow;
- }
- if ((millisNow - sensorDebounces[i]) > sensorDebounce && state != sensorStates[i]) {
- // sensor was on for at least TIME_TRIG
- sensorStates[i] = state;
- if (state && pumpTimes[i] == 0) {
- pumpTimes[i] = millisNow;
- }
- }
- lastSensorStates[i] = state;
- // END: Sensor reading
- // Pump control
- if (shouldMotorRun(i)) {
- digitalWrite(pumps[i], HIGH);
- }
- else {
- pumpTimes[i] = 0;
- digitalWrite(pumps[i], LOW);
- }
- // END: Pump control
- }
- #ifdef DEBUG
- sendDebug();
- #endif
- delay(TIME_LOOP);
- }
- bool shouldMotorRun(int i) {
- return millisNow < pumpTimes[i] + pumpRunTime;
- }
- #ifdef DEBUG
- void sendDebug() {
- Serial.println("S1 M1 S2 M2 S3 M3");
- for (i = 0; i < RES_CNT; i++) {
- Serial.print(' ');
- Serial.print(sensorStates[i] ? '+' : '-');
- Serial.print(' ');
- Serial.print(' ');
- Serial.print(shouldMotorRun(i) ? '+' : '-');
- Serial.print(' ');
- }
- Serial.println("");
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement