First version of the new code, not fully tested on my Leonardo

Need to see the level for he kids
This commit is contained in:
Axel C
2015-11-25 22:27:43 +01:00
parent 2a823d0711
commit f57db0f1b2
3 changed files with 191 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
/*
*
*
*
* Clock mechanism to execut an action every houres
*
* TODO Switch action reset houre to 9
* TODO Safety extra sensor
*/
#include <TimerOne.h>
#include <avr/io.h>
#include <avr/interrupt.h>
// Pin definitions
#define LAMP 8 // Pin of the lamp relay
#define LED 13 // For Leonardo built in led
#define PUMP_IN 9 // Pin of the pump 1
#define PUMP_OUT 10 // Pin of the pump 2
#define WATER_UP 4 // Pin of the water sensor 1
#define WATER_DOWN 5 // Pin of the water sensor 2
// Time definition
#define DAY_TIME 14 // Number of hours the day lasts (LAMP ON)
#define DAY_START 9 // Hour of the day that the day start
#define PUMP_CYCLE 15 // Time between 2 pump cycles
#define WATER_UP_TIME 5 // Time to keep water up
#define SIMULATION true //Boolean to activate the simulation (reduce time 1mn = 1s)
// Time counter variables
volatile int hoursCounter; // 0 to 23
volatile int secondsCounter; // 0 to 59
volatile int minutesCounter; // 0 to 59
bool ledState = 0; // led status memory
bool waterLevelState = 0; // water level status memory
#define DOWN 0
#define FILLING 1
#define UP 3
#define CLEARING 4
/* void setup(void)
* Setup software run only once
* Allows to setup all the software and hardware
*/
void setup(void)
{
// Setup the pin function TODO select good pin
pinMode(LAMP, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(PUMP_IN, OUTPUT);
pinMode(PUMP_OUT, OUTPUT);
pinMode(WATER_UP, INPUT);
digitalWrite(WATER_UP, HIGH); //Setup pull up
pinMode(WATER_DOWN, INPUT);
digitalWrite(WATER_DOWN, HIGH); //Setup pull up
// initialize Timer1 for 1 second interval
Timer1.initialize(1000000); // Set 1 second to timer 1
Timer1.attachInterrupt(seconds_timer);
hoursCounter = DAY_START; // Init the time to DAY_START at startup
minutesCounter = 1;
secondsCounter = 1;
digitalWrite(LAMP, HIGH);
}
/* void loop(void)
* Main program automatically loaded
*
*/
void loop(void)
{
switch (waterLevelState) {
case DOWN:
//Wait for pump cycle time to activate pump
if(0 == (minutesCounter % PUMP_CYCLE))
{
digitalWrite(PUMP_IN, 1); // Turn ON the filling pump
waterLevelState = FILLING;
}
break;
case FILLING:
// wait for water up sensor to be activated
if(0 == digitalRead(WATER_UP))
{
digitalWrite(PUMP_IN, 0); // Turn OFF the filling pump
waterLevelState = UP;
}
break;
case UP:
//Wait for level up time passed to clear
if(0 == ((minutesCounter % PUMP_CYCLE) % WATER_UP_TIME))
{
digitalWrite(PUMP_OUT, 1); // Turn ON the clearing pump
waterLevelState = CLEARING;
}
break;
break;
case CLEARING:
// wait for water down sensor to be activated
if(0 == digitalRead(WATER_DOWN))
{
digitalWrite(PUMP_OUT, 0); // Turn OFF the clearing pump
waterLevelState = DOWN;
}
break;
default:
// default is optional
break;
}
}
/* seconds_timer(void)
* Timer executed every seconds for time count
* This function allows to make a basic clock which count hours, minutes and seconds
*/
void seconds_timer(void)
{
toggle_led(); //Toggle the card LED every seconds
// In case of SIMULATION increment minutes to go quicker
if (SIMULATION)
{
minutesCounter ++; // Increment the minutes counter
}
else
{// In case of normal use, increment seconds
secondsCounter++; // Increment the seconds counter
}
// Every 60s we increment the minutes counters (not used in SIMULATION)
if (0 == secondsCounter % 60)
{
minutesCounter ++; //Increment the minutes counter
secondsCounter = 0; // Clear the seconds counter
}
// Every 60min increment the hours counter
if (0 == minutesCounter % 60)
{
hoursCounter ++; // Increment the hours counter
minutesCounter = 0; // Clear the minutes counter
// At the end of the day reset the hours counter
if (23 < hoursCounter)
{
hoursCounter = 0; // Clear the hours counter
}
// Execute following code one time every hours
hours_action();
}
}
/* void hour_action(void)
* Program executed one time every hours
*
*/
void hours_action(void)
{
//Check if something has to be done
if (DAY_START == hoursCounter)
digitalWrite(LAMP, 1);
if ((DAY_START + DAY_TIME) == hoursCounter)
digitalWrite(LAMP, 0);
}
void toggle_led(void)
{
ledState = ledState ^ 1;
digitalWrite(LED, ledState);
}

View File

@@ -0,0 +1,6 @@
Librairies:
TimerOne
https://github.com/PaulStoffregen/TimerOne
Copy after unzip inside Arduino/hardware/libraries/

Binary file not shown.