Skip to content

Instantly share code, notes, and snippets.

@ondrejh
Created May 25, 2021 12:43
Show Gist options
  • Save ondrejh/9540dd1a0d8c00d82cc3448d6c53a0b6 to your computer and use it in GitHub Desktop.
Save ondrejh/9540dd1a0d8c00d82cc3448d6c53a0b6 to your computer and use it in GitHub Desktop.
Arduino based water pump timer with signalisation and safety float switch.
/**
* Casovy spinac pro cerpadlo na skluzavku do bazenu. Cerpadlo se zapne na 15s po stisku tlacitka. Kontrolka sviti. Poslednich 5s blika.
* Pokud je malo vody (plovakovy spinac), cerpadlo se nezapne a kontrolka blika rychle.
*/
#define CAS_ZAPNUTI 15000 //ms
#define CAS_BLIKANI 5000 //ms
#define CAS_CHYBY 1000 //ms
#define BLIKANI_POMALE 500 //ms
#define BLIKANI_RYCHLE 100 //ms
#define PLOVAK_PIN 3
#define PLOVAK_PULLUP 4
#define CUDLIK_PIN 5
#define CUDLIK_PULLUP 6
#define CERPADLO_PIN 2
#define LED_ON() do{digitalWrite(LED_BUILTIN, HIGH);}while(0)
#define LED_OFF() do{digitalWrite(LED_BUILTIN, LOW);}while(0)
#define CERPADLO_ZAPNOUT() do{digitalWrite(CERPADLO_PIN, HIGH);}while(0)
#define CERPADLO_VYPNOUT() do{digitalWrite(CERPADLO_PIN, LOW);}while(0)
#define KONTROLKA_PIN 7
#define KONTROLKA_ON() do{digitalWrite(KONTROLKA_PIN, HIGH);}while(0)
#define KONTROLKA_OFF() do{digitalWrite(KONTROLKA_PIN, LOW);}while(0)
#define CUDLIK (digitalRead(CUDLIK_PIN) != 0) // cudlik je NC s pullupem
#define PLOVAK (digitalRead(PLOVAK_PIN) != 0) // plovak je OK (ma vodu) kdyz je rozepnuty (NC s pullupem)
void setup() {
// nastaveni vstupu a vystupu
pinMode(PLOVAK_PULLUP, OUTPUT);
digitalWrite(PLOVAK_PULLUP, HIGH);
pinMode(CUDLIK_PULLUP, OUTPUT);
digitalWrite(CUDLIK_PULLUP, HIGH);
pinMode(PLOVAK_PIN, INPUT);
pinMode(CUDLIK_PIN, INPUT);
pinMode(CERPADLO_PIN, OUTPUT);
CERPADLO_VYPNOUT();
pinMode(LED_BUILTIN, OUTPUT);
LED_OFF();
pinMode(KONTROLKA_PIN, OUTPUT);
KONTROLKA_OFF();
}
void loop() {
/*// test vstupu a vystupu
if (CUDLIK) {
CERPADLO_ZAPNOUT();
KONTROLKA_ON();
}
else {
CERPADLO_VYPNOUT();
KONTROLKA_OFF();
}
if (PLOVAK) LED_ON();
else LED_OFF();*/
static int s = 0;
static uint32_t t;
switch(s) {
case 0: // ceka na cudlik
if (CUDLIK) {
t = millis();
s = PLOVAK ? 1 : 10;
}
break;
case 1: // zapnuti cerpadla
CERPADLO_ZAPNOUT();
KONTROLKA_ON();
s ++;
break;
case 2: // cerpadlo bezi
if (!PLOVAK)
s = 10;
else if (CUDLIK) {
t = millis();
KONTROLKA_ON();
}
else {
uint32_t dt = millis() - t;
if (dt > CAS_ZAPNUTI) {
s ++; // timeout -> vypni
CERPADLO_VYPNOUT();
KONTROLKA_OFF();
}
else if (dt > (CAS_ZAPNUTI - CAS_BLIKANI)) {
if (dt / BLIKANI_POMALE & 1) KONTROLKA_ON(); else KONTROLKA_OFF();
}
}
break;
case 3: // vypnout zhasnout a zpet na start
if (!CUDLIK) {
s = 0;
}
break;
case 10: // chyba plovaku
CERPADLO_VYPNOUT();
t = millis();
s ++;
case 11: // cekat 2s a uvolneni cudliku, u toho zbesile blikat
uint32_t dt = millis() - t;
if (dt / BLIKANI_RYCHLE & 1) KONTROLKA_OFF(); else KONTROLKA_ON();
if ((dt > CAS_CHYBY) && (!CUDLIK)) {
KONTROLKA_OFF();
s = 0;
}
break;
default: // sem by to nemelo dojit
if (!CUDLIK) {
KONTROLKA_OFF();
CERPADLO_VYPNOUT();
s = 0;
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment