loading

Step 3: Arduino Code

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! ");
}
Let me be the first to say Hell yeah brother i know what your going through. and thank you because you have given me a project that i intend to do and habbit to break. <br> <br>good sir i wish you luck and god speed in your endevour.
Thanks! I'll post my progress as I make it.
<p>this is amazing. thank you for sharing.!!</p>
<p>i used to fu** like hell and smoke now i just fu** like hell</p>
<p>try update the nokia LCD to a TFT LCD! Don't worry about the coding of GUI display , here is a TFT LCD you can use with arduino without coding.</p><p>Here is a new UART TFT LCD, it support the WYSIWYG editor to build your UI in PC and download it via USB. It can be control by Arduino via UART, so just use the Serial.print() you can make it display many images. And do't worry about the font ! You can build it yourself and download to the TFT LCD.</p><p>Its indigogo page here with detailed in introduce:Amazing price!! 10USD 2.4&quot; with free shipping.....</p><p><a href="https://www.indiegogo.com/projects/nextion-a-cost-effective-high-performance-tft-hmi/x/4283045" rel="nofollow">https://www.indiegogo.com/projects/nextion-a-cost-...</a></p><p>Here is 2 blogs about using the Arduino to build the UI tutorial:</p><p><a href="http://blog.iteadstudio.com/product-preview-nextion-in-an-arduino-project-2/" rel="nofollow">http://blog.iteadstudio.com/product-preview-nextio...</a></p><p><a href="http://blog.iteadstudio.com/product-preview-nextion-in-an-arduino-project-3/" rel="nofollow">http://blog.iteadstudio.com/product-preview-nextio...</a></p>
<p>Try update it to TFT LCD display ! Don't worry about the code of display because there is no need for coding for GUI! </p><p>Here is a new UART TFT LCD, it support the WYSIWYG editor to build your UI in PC and download it via USB. It can be control by Arduino via UART, so just use the Serial.print() you can make it display many images. And do't worry about the font ! You can build it yourself and download to the TFT LCD.</p><p>Its indigogo page here with detailed in introduce:Amazing price!! 10USD 2.4&quot; with free shipping.....</p><p><a href="https://www.indiegogo.com/projects/nextion-a-cost-effective-high-performance-tft-hmi/x/4283045" rel="nofollow">https://www.indiegogo.com/projects/nextion-a-cost-...</a></p><p>Here is 2 blogs about using the Arduino to build the UI tutorial:</p><p><a href="http://blog.iteadstudio.com/product-preview-nextion-in-an-arduino-project-2/" rel="nofollow">http://blog.iteadstudio.com/product-preview-nextio...</a></p><p><a href="http://blog.iteadstudio.com/product-preview-nextion-in-an-arduino-project-3/" rel="nofollow">http://blog.iteadstudio.com/product-preview-nextio...</a></p>
http://www.plzzshop.com <br> !free shipping!<br>j0rdan sh0es........ 28 dollar<br>c0ach p-u-r-s-e...... 25 dollar<br>c00gi cl0thes........ 20 dollar<br>U.G.G B00ts.......... 39 dollar<br>Dear Friend:<br>We can supply all kind jersey with good quality and low price. contact me, let's talk details.<br>Delivery time: 5-6days, Shipping Method: EMS<br>Newest stock list: every 3-5days updated<br>Warm Regards,<br>Carolyn<br><br> http://www.plzzshop.com
http://www.plzzshop.com <br> !free shipping!<br>j0rdan sh0es........ 28 dollar<br>c0ach p-u-r-s-e...... 25 dollar<br>c00gi cl0thes........ 20 dollar<br>U.G.G B00ts.......... 39 dollar<br>Dear Friend:<br>We can supply all kind jersey with good quality and low price. contact me, let's talk details.<br>Delivery time: 5-6days, Shipping Method: EMS<br>Newest stock list: every 3-5days updated<br>Warm Regards,<br>Carolyn<br><br> http://www.plzzshop.com

About This Instructable

13,682 views

38 favorites

License:

More by thoughtfix: Mint-Sized Success Meter (quit smoking!) with Arduino Arduino powered Haunted Pumpkin Make your pet dishes tweet!
Add instructable to: