Lesson 29: How to Schedule and Execute Tasks


Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson I'll show you how you can schedule a task to run at a set date and time.


Top of the Code

#include <SPI.h>
#include <Andee.h>

// There are two ways to retrieve the date and time from your smartphone
// We'll create two objects to demonstrate these two ways.
AndeeHelper displaybox;
// Current Date/Time
int day;
int month;
int year;
int hour; 
int minute;
int second;

// Scheduled Date/Time. You can use the date/time input buttons
// to allow the user schedule the tasks
int scheduledDay = 31; // Go ahead and change the date/time to see this at work!
int scheduledMonth = 12;
int scheduledYear = 2013;
int scheduledHour = 23;
int scheduledMinute = 59;
int scheduledSecond = 0;
// Flag variable to indicate whether the scheduled command was executed
int executedFlag = 0; 

const int outputPin = 2; // LED connected to Pin 2

char tempString[30];


void setup()
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance

  pinMode(outputPin, OUTPUT);


void setInitialData()
  displaybox.setTitle("Current Date/Time");


void loop()
  if( Andee.isConnected() )
    // Retrieve date and store in variables: day, month, and year
    Andee.getDeviceDate(&day, &month, &year);
    // Retrieve time and store in variables: hour, minute, second
    Andee.getDeviceTime(&hour, &minute, &second);
    sprintf(tempString, "%d/%d/%d, %02d:%02d:%02d", day, month, year, hour, minute, second);
    // Execute at the scheduled date/time
    if(day == scheduledDay
     && month == scheduledMonth
     && year == scheduledYear
     && hour == scheduledHour
     && minute == scheduledMinute
     && second >= scheduledSecond // We're using >= instead just to prevent the occasion that Arduino hangs for a while and misses that crucial second
     && executedFlag == 0)
       digitalWrite(outputPin, HIGH);
       displaybox.setTitle("Scheduled Task Executed");
       executedFlag = 1; // To stop it from repeating again
  delay(500); // Always leave a short delay for Bluetooth communication
comments powered by Disqus