element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog [Christmas Wreath of Things] Internet of Holiday Lights: software side note - dodgy advent logic
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 29 Dec 2014 12:48 AM Date Created
  • Views 697 views
  • Likes 3 likes
  • Comments 1 comment
  • yun
  • wreath_of_things
  • iot_holidaylights
  • rgb
  • internet-of-things
  • iot
  • shield
  • arduino_yun
  • arduino
  • stepper_motors
  • linux
Related
Recommended

[Christmas Wreath of Things] Internet of Holiday Lights: software side note - dodgy advent logic

Jan Cumps
Jan Cumps
29 Dec 2014

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.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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:

 

image

  • Sign in to reply

Top Comments

  • mcb1
    mcb1 over 10 years ago +1
    So long as it works, nothing more can be expected. I see lots of code that is optimized/minimized to reduce size or run a little faster when there is no real benefit and its hard for others to follow.…
  • mcb1
    mcb1 over 10 years ago

    So long as it works, nothing more can be expected. image

     

    I see lots of code that is optimized/minimized to reduce size or run a little faster when there is no real benefit and its hard for others to follow.

     

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube