Instructables

Mint-Sized Success Meter (quit smoking!) with Arduino

Featured

Step 3: Arduino Code

Picture of Arduino Code
code.png
There are three additional libraries required to get this running:
Time
Adafruit_GFX
Adafruit_PCD8544

The code was HEAVILY inspired by the Maker Faire Bay Area countdown timer by MaceTech, through whom you can also get the latest ChronoDot. The .ino file is attached (Arduino 1.0 required) and the raw code is below.

In this example, the quit date is April 1, 2012. In reality, the quit date is April 14, 2012 but that's in the future. The best way to test this is to wire up everything, but test the individual components (ChronoDot, LCD) with their example code before trying to put it all together. 

/*
Smoke-free success timer
Using a ChronoDot and a Nokia 5110 LCD.

Time library, Adafruit PCD libraries, RTC and Wire required.

The purpose of this sketch is to show a new non-smoker
how long they have been without cigarettes in days and
hours and a constantly growing number of the amount of
money saved by not smoking.

Much of the code was inspired by the Maker Faire Bay
Area countdown clock posted at
http://macetech.com/blog/node/115

April 2012, Daniel Gentleman, thoughtfix.com
*/

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h> // DS3231/ChronoDot works like DS1307

// LCD stuff:
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>


Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)

// Setting variables:
time_t quit; // Day/Time of the last smoke
time_t difftime; // difference between current and quitting time
int diff_seconds;
int diff_minutes;
int diff_hours;
int diff_days;
float PackCost = 6.50; // Cost per pack. US $6.50
float PackDay = 1.5; // Smoking frequency. 1.5 packs/day
float MinutesQuit;
float CostPerHour;
float CashSaved;

// Initialize RTC and LCD
void setup() {
  Serial.begin(9600); // set baud to 9600
  display.begin();
  display.setContrast(40);
  display.clearDisplay();
  display.display();
  setSyncProvider(RTC.get); // set sync to use the ChronoDot
  setSyncInterval(10); // sync every 10 seconds if possible

  // check whether sync worked
  if(timeStatus()!= timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  tmElements_t quit_elements; // elements array to date of quitting smoking.

  // Midnight on April 1, 2012
  quit_elements.Second = 0;
  quit_elements.Minute = 0;
  quit_elements.Hour = 0;
  quit_elements.Wday = 7;
  quit_elements.Day = 1;
  quit_elements.Month = 4;
  quit_elements.Year = 2012 - 1970;
  quit = makeTime(quit_elements); // Unix timestamp quit date/time
  delay(1000);
}
time_t systime; // holds current time for diff calculation

void loop() {

  if (systime != now()) { // wait for new second to do anything
    systime = now();
    difftime = systime - quit; // subtract quit time from current time
    diff_seconds = difftime % 60; // get seconds
    difftime /= 60; // convert to minutes
    diff_minutes = difftime % 60; // get minutes
    difftime /= 60; // convert to hours
    diff_hours = difftime % 24; // get hours
    difftime /= 24; // convert to days
    diff_days = difftime; // get days
  }
  DisplayTime();
  delay(1000);
  display.clearDisplay();
}

// modified routine from Time Library example
void DisplayTime(){
  MinutesQuit = ((systime - quit)/60);
  CashSaved = ( (PackCost * PackDay * MinutesQuit) / 1440);
  // First, the LCD
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.print(diff_days);
  display.print("d ");
  display.print(diff_hours);
  display.print(":");
  if(diff_minutes < 10)
    display.print('0');
  display.print(diff_minutes);
  display.print(":");
  if(diff_seconds < 10)
    display.print('0');
  display.print(diff_seconds);
  display.setCursor(0,10);
  display.println("Smoke free!");
  display.setCursor(0,30);
  display.print("$");
  display.print(CashSaved);
  display.println(" Saved ");
  display.display();
  //// Optionally: Serial
  //  Serial.print(diff_days);
  //  Serial.print("d ");
  //  Serial.print(diff_hours);
  //  Serial.print(":");
  //    if(diff_minutes < 10)
  //    Serial.print('0');
  //  Serial.print(diff_minutes);
  //  Serial.print(":");
  //    if(diff_seconds < 10)
  //    Serial.print('0');
  //  Serial.print(diff_seconds);
  //  Serial.println(" smoke free! ");
}
 
Remove these adsRemove these ads by Signing Up