My entry for the Internet of Holiday Lights is an electro-mechanical wreath.
I am now working on the software part of the 'advent state' logic.
In my design, there is a bottom layer (the wreath) with 4 lights that represent advent candles. They are always on.
On top of that, I'm mounting a circle with holes (the 'light filter'). That filter can turn and is driven by a motor.
Depending on what week of the advent we are, one, two, three or all four of the candles have to be shown.
There are 23 states:
- state 0: not an advent date
- state 1: first Sunday of advent, show one candle
- state 2: first Monday of the advent, still show one candle
- ...
- state 8: second Sunday of the advent, show two candles
- ...
- state 15: third Sunday, show three candles
- ...
- state 22: fourth Sunday of the advent, and any day thereafter until January 6, show four candles
So I need logic to convert the day of advent into a particular position of the filter.
In one of my official posts on this contest, I have discussed my solution to get that date from the Yun's linux part.
And in that post, I was talking about intelligent solutions that would calculate the state for that day with nifty algorithms.
But now that I'm really implementing the solution, on a Sunday night after 12, the brain doesn't want to play along. So I resorted to a cheeky implementation:
- I've hard-coded an array with advent days for the years 2014 and 2015
- I've provided a second array with the corresponding states for each day in the advent
- A lookup function that finds the correct state for any date within the covered period (let's call it the commercial guarantee period)
- A function that looks up the day in linux and converts it to a long, in its own dodgy way
Below is the code. If you're looking for good code practices, this would be the right time to avert the eyes.
// includes for advent intelligence
#include <Process.h>
// ================================ START ADVENT CALENDAR SECTION ===========================================================
/*
Advent intelligence
There are 23 states in my advent interpretation:
0: no advent
1: 1st sunday
2: 1st monday
...
8: 2nd sunday
...
15: 3rd sunday
22: 4rd sunday and any date between 25/12 and 6/1
For days between 25/12 and 6/1, state = 22 (same as 4rd advent Sunday
I have written the code this way to annoy you
to avoid any brain effort from my side, I have listed all dates from the first Sunday until January 6 in an array,
and the corresponding advent states in a second array. If you find the day, you can look up the advent state.
For days not found in the list, state = 0
The date is presented as a long, just a conversion of the string YYYYMMDD converted to numerical.
e.g.: date string "20141229" becomes long 20141229. Go figure :)
*/
// array with the advent intelligence lookup keys
long adventIntelligenceKey[] = {
// 2014
20141130, 20141201, 20141202, 20141203, 20141204, 20141205, 20141206, 20141207, 20141208, 20141209,
20141210, 20141211, 20141212, 20141213, 20141214, 20141215, 20141216, 20141217, 20141218, 20141219,
20141220, 20141221, 20141222, 20141223, 20141224, 20141225, 20141226, 20141227, 20141228, 20141229,
20141230, 20141231, 20150101, 20150102, 20150103, 20150104, 20150105, 20150106,
// 2015
20151129, 20151130, 20151201, 20151202, 20151203, 20151204, 20151205, 20151206, 20151207, 20151208,
20151209, 20151210, 20151211, 20151212, 20151213, 20151214, 20151215, 20151216, 20151217, 20151218,
20151219, 20151220, 20151221, 20151222, 20151223, 20151224, 20151225, 20151226, 20151227, 20151228,
20151229, 20151230, 20151231, 20160101, 20160102, 20160103, 20160104, 20160105, 20160106
};
// array with the corrsponding advent intelligence state
int adventIntelligenceValue[] = {
// 2014
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22,
// 2015
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22
};
/*
long getSysDate()
this function asks the Yun's linux part for the sysdate,a nd translates that to a long
retval: long - The current date as configured on the lini distro, just a conversion of the string YYYYMMDD converted to numerical
*/
long getSysDate() {
long lRetval = 0L;
Process date; // process used to get the date
Serial.println("invoking linux date");
if (!date.running()) {
date.begin("date");
date.addParameter("+%Y%m%d");
date.run();
}
Serial.println("linux date invoked");
while (date.available()>0) {
// lRetval = date.readString().toFloat();
String timeString = date.readString();
Serial.println(timeString);
lRetval = timeString.substring(0, 4).toInt() * 10000;
lRetval += timeString.substring(4, 6).toInt() * 100;
lRetval += timeString.substring(6, 8).toInt();
}
Serial.println(lRetval);
return lRetval;
}
/*
int getAdventState(long day)
this function looks up the state of the advent. See comment section "Advent intelligence" above for the different states.
If the day passed to this function is found in the array adventIntelligenceKey, the corresponding state in array adventIntelligenceValue is returned.
Else we assume that we are not in the advent, and we return state 0.
The commercial guarantee of the gizmo powered by this algorithm should expire at the latest at the last day represented in the array above.
param day: the Long representation of a day to process, just a conversion of the string YYYYMMDD converted to numerical
retval: long - The current date as configured on the lini distro, just a conversion of the string YYYYMMDD converted to numerical
*/
int getAdventState (long day) {
int i;
int iRetval = 0;
Serial.print ("day: ");
Serial.print(day);
for (i = 0; i < (sizeof(adventIntelligenceKey) / sizeof(adventIntelligenceKey[0])); i++) {
Serial.print (" checking against: ");
Serial.print (adventIntelligenceKey[i]);
if (day == adventIntelligenceKey[i]) {
iRetval = adventIntelligenceValue[i];
Serial.print (" found state");
break;
}
}
Serial.print (" : ");
Serial.println (iRetval);
return iRetval;
}
// ================================ END ADVENT CALENDAR SECTION ===========================================================
void setup() {
// general setup
Serial.begin(9600); // initialize serial
// advent intelligence setup
// the advent logic uses the lini part of the Yun, so Bridge needs to be initialised
Bridge.begin(); // initialize Bridge
// init motor
// init leds
}
void loop() {
int iState;
// get date
// get advent state
iState = getAdventState(getSysDate());
delay(10000); // TODO : remove debug code
// adjust to advent state
// do secret logic with MQTT
}
So all high standards have been thrown out of the door, but the code works:

Top Comments