Coffee Timer

coffeetimerwide

Pulling a perfect coffee requires tight control of a number of variables. The grinder dispenses a fixed dose of the correct grind into a portafilter basket. The portafilter fits into the coffee machine and machine is run. If all is well there should be a pause of four seconds before the coffee starts flowing then the coffee should flow for twenty five seconds dispensing a 1.5 oz shot.

To measure the two times I’m putting together this coffee shot timer. The white button is the timer button. Press once to start, twice to start the second timer and a third time to stop. The red button is to reset.

The project is based round an Arduino Uno and uses an OLED SSD1306 which I bought for a few pounds on eBay. I used the Adafruit libraries with the help of this YouTube tutorial to drive the OLED.

Here’s the board layout

coffeetimer_bb

// Coffee Timer
// (cc)(BY SA) Rob Ives 2015 
http://www.robives.com

/*********************************************************************
Adafruit library for Monochrome OLEDs based on SSD1306 drivers
https://github.com/adafruit/Adafruit_SSD1306
Adafruit GFX library:
https://github.com/adafruit/Adafruit-GFX-Library  
*********************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

//------------------------------------------------------ eo Adafruit header
//define the four timer states.
#define WAITING 0 // waiting to start
#define TIMERONE 1 // timer one running
#define TIMERTWO 2 // timer two running
#define FINISHED 3 // timer complete

const long minute = 60000; // 60000 milliseconds in a minute
const int resetPin = 2; 
const int buttonPin = 3; 
const int ledPin =  13;      // the number of the LED pin

long startTime = 0;
long timeOne = 0;
long timeTwo = 0;
int timer = WAITING; // timer state. Initially waiting for first button press
int reset = 0;  // reset button state
int button = 0; // main button state

void setup()   {    
  pinMode(buttonPin, INPUT); //init button pin to input
  pinMode(resetPin, INPUT);  //init reset pin to input
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: 
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3c
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  printDigits(0);
  
}

void loop() {
   //-------- main loop -------
 switch ( timer){
   case WAITING: // initial state - waiting for first button press.
     display.clearDisplay();
     display.setCursor(10,00); 
     printDigits(0);
     display.display(); // display screen showing 00:00s
     button = digitalRead(buttonPin);
     if(button){ // button is pressed.
       timer = TIMERONE; // start the first timer 
       startTime = millis();//read the time the button is pressed.
       while (digitalRead(buttonPin)){
         //--- wait for button to be released.
       }
     }
   break;
   case TIMERONE: //running first timer
     timeOne = millis();
     button = digitalRead(buttonPin);
     reset = digitalRead(resetPin);
     display.clearDisplay();
     display.setCursor(10,00);
     printDigits(timeOne - startTime);
     display.display();
     //-----------------------------------
     if(reset){ //reset button was pressed. Clear screeen and start again
       timer = WAITING;
       while (digitalRead(resetPin)){
         //--- wait for reset button to be released.
       }
     }
     //------------------------------------
     if(button){ // button was pressed
         timer = TIMERTWO;  // start the second timer
         //timeTwo = millis(); //record the time for the start of the second timer
         while (digitalRead(buttonPin)){
           //--- wait for button to be released.
         }
       }
   break;
   case TIMERTWO: //running first timer
     timeTwo = millis();
     button = digitalRead(buttonPin);
     reset = digitalRead(resetPin);
     display.clearDisplay();
     display.setCursor(10,00);
     printDigits(timeOne - startTime); // display the first time time
     display.setCursor(10,32);
     printDigits(timeTwo - timeOne); // display the second timer on the line below
     display.display();
     //------------------------------------
     if(reset){ //reset button was pressed. Clear screeen and start again.32w
       timer = WAITING;
       while (digitalRead(resetPin)){
         //--- wait for reset button to be released.
       }
     }
     //------------------------------------
     if(button){ // button was pressed
         timer = FINISHED;  // start the second timer
         while (digitalRead(buttonPin)){
           //--- wait for button to be released.
         }
       }
     
   break;
    case FINISHED: //running first timer
     reset = digitalRead(resetPin);
     if(reset){ //reset button was pressed. Clear screeen and start again
       display.clearDisplay();
       display.setCursor(10,0);
       printDigits(0);
       display.display();
       timer = WAITING;
       while (digitalRead(resetPin)){
         //--- wait for reset button to be released.
       }
     } 
   break;  
   } // eo switch
  }// eo void loop
  
void printDigits(long timeInMillis){ 
  // convert the time from milliseconds to four digit SS:HH 
  // seconds and hundredths and display at current cursor position
  int seconds;
  int hundredths;
  seconds = timeInMillis /1000;
  seconds = seconds % 60; // seconds never more than 60
  hundredths = (timeInMillis % 1000)/10;
  
  if (seconds <10){ // print leading zero if necessary
    display.print("0");
  }
  display.print(seconds);
  display.print(":");
  if (hundredths <10){ //print leading zero if necessary
  display.print("0");
  
  }
  display.print(hundredths);
  display.print("s");
  //display.display();
}

Here’s the YouTube video showing the finished prototype in action. Next step will be to connect to some sort of battery and fit it into a case. After than I might have a look at using higher res fonts.

0

Leave a Reply

Your email address will not be published. Required fields are marked *