Skip to content

Commit

Permalink
DayNight: use NightLight wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
epccs committed May 14, 2018
1 parent a8d4274 commit 891449e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .vscode/c_cpp_properties.json
Expand Up @@ -4,8 +4,8 @@
"name": "AVR",
"includePath": [
"${workspaceRoot}",
"Y:/lib/avr-libc/include",
"Y:/lib/gcc-avr/5.4.0/include"
"Z:/lib/avr-libc/include",
"Z:/lib/gcc-avr/5.4.0/include"
],
"defines": [],
"intelliSenseMode": "clang-x64",
Expand Down
2 changes: 1 addition & 1 deletion DayNight/README.md
Expand Up @@ -4,7 +4,7 @@

Use a red LED's photovoltaic voltage on ADC2 to approximately tell if it is day or night.

Note: use the Day_AttachDayWork() and Night_AttachWork() functions to set a callback that will be run at the start of each day. This framework is how I debuged the day-night stat machine, it is not expected to be useful.
Note: use the Day_AttachDayWork() and Night_AttachWork() functions to set a callback that will be run at the start of each day. This framework is how I debuged the day-night stat machine.


## Firmware Upload
Expand Down
51 changes: 25 additions & 26 deletions DayNight/day_night.c
Expand Up @@ -34,8 +34,8 @@ For a copy of the GNU General Public License use
#define STARTUP_DELAY 1000UL
#define EVENING_THRESHOLD 20
// 900,000 millis is 15 min
#define EVENING_DEBOUCE 9000UL
#define MORNING_DEBOUCE 9000UL
#define EVENING_DEBOUCE 900000UL
#define MORNING_DEBOUCE 900000UL
#define DAYNIGHT_TO_LONG 72000000UL
// 72 Meg millis() is 20hr, which is past the longest day or night so somthing has went wrong.
/* note the UL is a way to tell the compiler that a numerical literal is of type unsigned long */
Expand All @@ -52,7 +52,11 @@ For a copy of the GNU General Public License use
# error ADC hysteresis of 9 should be allowed
#endif


uint8_t red_led_sensor = RED_LED_SENSOR;
int morning_threshold = MORNING_THRESHOLD;
int evening_threshold = EVENING_THRESHOLD;
unsigned long evening_debouce = (unsigned long)EVENING_DEBOUCE;
unsigned long morning_debouce = (unsigned long)MORNING_DEBOUCE;
uint8_t dayState = 0;
unsigned long dayTmrStarted;

Expand All @@ -66,7 +70,6 @@ typedef void (*PointerToWork)(void);
static PointerToWork dayState_atDayWork = callback_default;
static PointerToWork dayState_atNightWork = callback_default;

#define SERIAL_PRINT_DELAY_MILSEC 60000UL
static unsigned long serial_print_started_at;

/* register (i.e. save a referance to) a function callback that does somthing
Expand All @@ -81,7 +84,7 @@ void Night_AttachWork( void (*function)(void) )
dayState_atNightWork = function;
}

void Day(void)
void Day(unsigned long serial_print_delay_milsec)
{
if ( (command_done == 10) )
{
Expand Down Expand Up @@ -138,7 +141,7 @@ void Day(void)
else if ( (command_done == 12) )
{ // delay between JSON printing
unsigned long kRuntime= millis() - serial_print_started_at;
if ((kRuntime) > ((unsigned long)SERIAL_PRINT_DELAY_MILSEC))
if ((kRuntime) > (serial_print_delay_milsec))
{
command_done = 10; /* This keeps looping output forever (until a Rx char anyway) */
}
Expand All @@ -149,23 +152,21 @@ void Day(void)
dayState
0 = default at startup, if above Evening Threshold set day, else set night.
1 = day: wait for evening threshold, set for evening debounce.
2 = evening_debounce: wait for EVENING_DEBOUCE to finish, then set night, however if debouce fails set back to day.
3 = night: wait for morning threshold, set morning_debounce.
4 = morning_debounce: wait for MORNING_DEBOUCE to finish, then set irrigation, however if debouce fails set back to night.
5 = work: do some work.
6 = fail: fail state.
2 = evening_debounce: wait for debounce time, do night_work, however if debouce fails set back to day.
3 = night_work: do night callback and set night.
4 = night: wait for morning threshold, set morning_debounce.
5 = morning_debounce: wait for debounce time, do day_work, however if debouce fails set back to night.
6 = day_work: do day callback and set for day.
7 = fail: fail state.
*/
void CheckDayLight(void)
{
int Morning_Threshold = MORNING_THRESHOLD;
int Evening_Threshold = EVENING_THRESHOLD;

{
if(dayState == DAYNIGHT_START_STATE)
{
unsigned long kRuntime= millis() - dayTmrStarted;
if ((kRuntime) > ((unsigned long)STARTUP_DELAY))
{
if(analogRead(RED_LED_SENSOR) > Evening_Threshold )
if(analogRead(red_led_sensor) > evening_threshold )
{
dayState = DAYNIGHT_DAY_STATE;
dayTmrStarted = millis();
Expand All @@ -181,7 +182,7 @@ void CheckDayLight(void)

if(dayState == DAYNIGHT_DAY_STATE)
{ //day
if (analogRead(RED_LED_SENSOR) < Evening_Threshold )
if (analogRead(red_led_sensor) < evening_threshold )
{
dayState = DAYNIGHT_EVENING_DEBOUNCE_STATE;
dayTmrStarted = millis();
Expand All @@ -197,10 +198,10 @@ void CheckDayLight(void)

if(dayState == DAYNIGHT_EVENING_DEBOUNCE_STATE)
{ //evening_debounce
if (analogRead(RED_LED_SENSOR) < Evening_Threshold )
if (analogRead(red_led_sensor) < evening_threshold )
{
unsigned long kRuntime= millis() - dayTmrStarted;
if ((kRuntime) > ((unsigned long)EVENING_DEBOUCE))
if ((kRuntime) > (evening_debouce))
{
dayState = DAYNIGHT_NIGHTWORK_STATE;
dayTmrStarted = millis();
Expand All @@ -215,16 +216,15 @@ void CheckDayLight(void)
}

if(dayState == DAYNIGHT_NIGHTWORK_STATE)
{
//do the work (if it got registered), e.g. load night light settings at the start of a night
{ //do the night work callback, e.g. load night light settings at the start of a night
if (dayState_atNightWork != NULL) dayState_atNightWork();
dayState = DAYNIGHT_NIGHT_STATE;
return;
}

if(dayState == DAYNIGHT_NIGHT_STATE)
{ //night
if (analogRead(RED_LED_SENSOR) > Morning_Threshold )
if (analogRead(red_led_sensor) > morning_threshold )
{
dayState = DAYNIGHT_MORNING_DEBOUNCE_STATE;
dayTmrStarted = millis();
Expand All @@ -240,10 +240,10 @@ void CheckDayLight(void)

if(dayState == DAYNIGHT_MORNING_DEBOUNCE_STATE)
{ //morning_debounce
if (analogRead(RED_LED_SENSOR) > Morning_Threshold )
if (analogRead(red_led_sensor) > morning_threshold )
{
unsigned long kRuntime= millis() - dayTmrStarted;
if ((kRuntime) > ((unsigned long)MORNING_DEBOUCE))
if ((kRuntime) > (morning_debouce))
{
dayState = DAYNIGHT_DAYWORK_STATE;
}
Expand All @@ -256,8 +256,7 @@ void CheckDayLight(void)
}

if(dayState == DAYNIGHT_DAYWORK_STATE)
{
//do the work (if it got registered),, e.g. load irrigation settings at the start of a day
{ //do the day work callback, e.g. load irrigation settings at the start of a day
if (dayState_atDayWork != NULL) dayState_atDayWork();
dayState = DAYNIGHT_DAY_STATE;
return;
Expand Down
8 changes: 7 additions & 1 deletion DayNight/day_night.h
@@ -1,7 +1,7 @@
#ifndef DayNight_H
#define DayNight_H

extern void Day(void);
extern void Day(unsigned long);

extern void CheckDayLight(void);
extern uint8_t DayState(void);
Expand All @@ -10,6 +10,12 @@ extern uint8_t DayState(void);
extern void Day_AttachWork( void (*)(void) );
extern void Night_AttachWork( void (*)(void) );

extern uint8_t red_led_sensor;
extern int morning_threshold;
extern int evening_threshold;
extern unsigned long evening_debouce;
extern unsigned long morning_debouce;

#define DAYNIGHT_START_STATE 0
#define DAYNIGHT_DAY_STATE 1
#define DAYNIGHT_EVENING_DEBOUNCE_STATE 2
Expand Down
14 changes: 9 additions & 5 deletions DayNight/main.c
Expand Up @@ -31,9 +31,9 @@ For a copy of the GNU General Public License use
#define ADC_DELAY_MILSEC 200UL
static unsigned long adc_started_at;

// 22mA current sources enabled with CS0_EN and CS1_EN which are defined in ../lib/pins_board.h
#define STATUS_LED CS0_EN
#define DAYNIGHT_STATUS_LED CS1_EN
//pins are defined in ../lib/pins_board.h
#define STATUS_LED DIO13
#define DAYNIGHT_STATUS_LED DIO12
#define DAYNIGHT_BLINK 500UL
static unsigned long day_status_blink_started_at;

Expand All @@ -50,7 +50,7 @@ void ProcessCmd()
}
if ( (strcmp_P( command, PSTR("/day?")) == 0) && ( (arg_count == 0 ) ) )
{
Day(); // day_night.c
Day(5000UL); // day_night.c: show every 5 sec until terminated
}
}

Expand Down Expand Up @@ -112,9 +112,13 @@ void setup(void)
blink_delay = BLINK_DELAY/4;
}

// set callback(s). See Solenoid for another example, where it loads the EEPROM values used at the start of each day
// register callback(s) to do the work.
Day_AttachWork(callback_for_day_attach);
Night_AttachWork(callback_for_night_attach);

// default debounce is 15 min (e.g. 900,000 millis)
evening_debouce = 18000UL; // 18 sec
morning_debouce = 18000UL;
}

void blink(void)
Expand Down
2 changes: 1 addition & 1 deletion NightLight/main.c
Expand Up @@ -122,7 +122,7 @@ void ProcessCmd()
}
if ( (strcmp_P( command, PSTR("/day?")) == 0) && ( (arg_count == 0 ) ) )
{
Day(); // ../DayNight/day_night.c
Day(60000UL); // ../DayNight/day_night.c: show every 60 sec until terminated
}
if ( (strcmp_P( command, PSTR("/charge?")) == 0) && ( (arg_count == 0 ) ) )
{
Expand Down
2 changes: 1 addition & 1 deletion PwrMgt/main.c
Expand Up @@ -100,7 +100,7 @@ void ProcessCmd()
}
if ( (strcmp_P( command, PSTR("/day?")) == 0) && ( (arg_count == 0 ) ) )
{
Day(); // ../DayNight/day_night.c
Day(60000UL); // ../DayNight/day_night.c: show every 60 sec until terminated
}
if ( (strcmp_P( command, PSTR("/charge?")) == 0) && ( (arg_count == 0 ) ) )
{
Expand Down

0 comments on commit 891449e

Please sign in to comment.