Skip to content

Instantly share code, notes, and snippets.

@gorenje
Last active December 24, 2015 20:09
Show Gist options
  • Save gorenje/6855720 to your computer and use it in GitHub Desktop.
Save gorenje/6855720 to your computer and use it in GitHub Desktop.
// -*- c -*-
#define MINUTES(x) x * 60L * 1000L
#define HOURS(x) x * MINUTES(60L)
/*
* Pin 10 is the light
* Pin 9 is the pump
*/
#define PUMP_PIN 9
#define LIGHTS_PIN 10
#define HOURS_2 120
#define HOURS_24 1440
#define HOURS_16 960
#define HOURS_8 480
/*
* Uses https://github.com/JChristensen/Timer for the timer stuff.
*
* But remember the issues with Long tyes for specifying times:
* http://www.doctormonk.com/2012/01/arduino-timer-library.html?showComment=1341895106916#c3768593763946936055
*
* That is also why this is alot more verbose as could be, redid this so that
* it works and then discovered the long bug :(
*
* Current setup is that the lights are off for 8 hours and then on for 16
* hours. The pump is off for 115 minutes and then on for 5 minutes. Rinse
* and repeat.
*
* Relay circuit was built using the following:
* http://forum.arduino.cc/index.php?topic=157018.0
* Which works really well.
*/
#include "Timer.h"
Timer t;
/*
* HIGH is off and LOW is on for the relays. But using the other pins on
* the relay, so this is actually reversed.
*/
int pumpActionCounter = 0;
int lightsActionCounter = 0;
boolean allPowerUp = false;
boolean allPowerUpSwitch = false;
int resetButtonCounter = 0;
boolean doingReset = false;
void everyMinute() {
pumpAction();
lightsAction();
}
void lightsAction() {
Serial.print( "lights action: " );
Serial.println( lightsActionCounter );
if ( lightsActionCounter >= 1440 ) {
lightsActionCounter = 0;
Serial.print( "lights action (clock sync): " );
Serial.println( lightsActionCounter );
}
if ( lightsActionCounter < 480 ) {
powerUpLights();
}
if ( lightsActionCounter >= 480 && lightsActionCounter < 960) {
powerDownLights();
}
if ( lightsActionCounter >= 960 && lightsActionCounter < 1440) {
powerUpLights();
}
lightsActionCounter++;
}
void pumpAction() {
Serial.print( "pump action: " );
Serial.println( pumpActionCounter );
if ( pumpActionCounter >= HOURS_2 ) {
pumpActionCounter = 0;
Serial.print( "pump action (clock sync): " );
Serial.println( pumpActionCounter );
}
if ( pumpActionCounter < 115 ) {
powerDownPump();
} else {
powerUpPump();
}
pumpActionCounter++;
}
/*
* Switch things on
*/
void powerUpPump() {
Serial.println( "power up pump" );
digitalWrite(PUMP_PIN, HIGH);
}
void powerUpLights() {
Serial.println( "power up lights" );
digitalWrite(LIGHTS_PIN, HIGH);
}
/*
* Switch things off
*/
void powerDownPump() {
Serial.println( "power down pump" );
digitalWrite(PUMP_PIN, LOW);
}
void powerDownLights() {
Serial.println( "power down lights" );
digitalWrite(LIGHTS_PIN, LOW);
}
void tickTock() {
Serial.println( "ticktock" );
Serial.println( millis() / 1000 );
}
void setup() {
Serial.begin(9600);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(4, LOW);
digitalWrite(7, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
int tickEvent = t.every(20L*1000L, tickTock);
Serial.println(tickEvent);
tickEvent = t.every(MINUTES(1L), everyMinute);
Serial.println(tickEvent);
}
void loop() {
/*
* Toggle to reset the time
*/
if ( digitalRead(6) == HIGH && !doingReset ) {
resetButtonCounter++;
digitalWrite(4, HIGH);
if ( resetButtonCounter > 20000 ) {
resetButtonCounter = 0;
doingReset = true;
pumpActionCounter = 0;
lightsActionCounter = 0;
t.oscillate(4, 500, LOW, 3);
}
}
if ( digitalRead(6) == LOW && resetButtonCounter > 0 ) {
digitalWrite(4, LOW);
}
if ( digitalRead(6) == LOW ) {
doingReset = false;
resetButtonCounter = 0;
}
/*
* Switch to turn on everything
*/
if ( digitalRead(5) == HIGH ) {
allPowerUpSwitch = true;
digitalWrite(7, HIGH);
powerUpPump();
powerUpLights();
} else {
if ( allPowerUpSwitch ) {
powerDownPump();
powerDownLights();
digitalWrite(7, LOW);
allPowerUpSwitch = false;
}
}
t.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment