   s7s.clearDisplay();  
 }
 
-void loop()
-{
-  if (hasButtonBeenPushed()) {
-    Serial.println("Button Pushed"); 
-    buttonPressCounter++;
-    Serial.println(buttonPressCounter); 
-  }
-  
-  switch (buttonPressCounter % 3) {
-    case 0:
-      setColor(0, 255, 0);
-      adjustTime();
-      break;
-    case 1:
+
+void loop() {
+  doButtonCheck();
+  
+  switch (mode) {
+    case MODE_CLOCK:
+      displayClock();
+      break;
+    case MODE_SET_COOK_TIME:
+      adjustCountdownTimer();
+      break;
+    case MODE_COOK:
       cooking();
       break;
-    case 2:
-      setColor(0, 0, 0);
-      s7s.clearDisplay();
-      displayValue = 0;
-      re_resetPulses();
+    case MODE_SET_CLOCK:
+      setClock();
       break;
   }  
 }
 
 
-void adjustTime() {
+void doButtonCheck() {
+  // Read the state of the button
+  buttonState = digitalRead(buttonPin);
+  if (buttonState == LOW) {
+    if (lastButtonState == HIGH) {
+      Serial.println("Button Pushed");
+      switchMode();
+    }
+  } else {
+    if (lastButtonState == LOW) {
+      // Record time button was pushed
+      buttonDownTime = millis();
+    }
+
+    // Check for long press
+    if (millis() - buttonDownTime >= buttonSetClockDuration) {
+        Serial.println("Button pressed for 10 seconds");
+        wasLongPress = true;
+
+        // Set time setup goes here
+        setColor(0, 128, 255);
+        re_resetPulses();
+    }
+  }
+  
+  // Save the previous state for the next loop
+  lastButtonState = buttonState;
+}
+
+
+void switchMode() {
+  if (wasLongPress) {
+    mode = MODE_SET_CLOCK;
+    wasLongPress = false;
+    
+  } else {
+    // Normal mode switch
+    mode++;
+    mode = mode % 3;
+    Serial.print("Now in mode: "); 
+    Serial.println(mode); 
+  }
+}
+
+
+void displayClock() {
+  setColor(0, 0, 0);
+  
+  now = millis() + offset + 86400000;
+  int seconds = (now / 1000) % 60;
+  int minutes = (now / 1000 / 60) % 60;
+  int hours = (now / 1000 / 60 / 60) % 12;
+  
+  if (hours == 0) {
+    hours = 12;
+  }
+  
+  sprintf(tempString, "%02d%02d", hours, minutes);
+  s7s.sendString(tempString);
+  
+  if (seconds % 2 == 0) {
+    s7s.setDecimals(0b00010000); 
+  } else {
+    s7s.setDecimals(0b00000000);
+  }
+}
+
+
+void setClock() {
+  setColor(0, 0, 255);
+
+  long pulses = (long) re_getPulses();
+  offset = (pulses / 4) * 1000 * 60;
+
+  displayClock();
+  s7s.setDecimals(0b00010000);
+}
+
+
+void adjustCountdownTimer() {
+  setColor(0, 255, 0);
+
   int pulses = re_getPulses();
   if (pulses < 0) {
     re_resetPulses();


// Piezo buzzer pin number
int buzzerPin = 7;


void setup() {

  pinMode(ledRedPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(ledBluePin, OUTPUT);  
  
  // Initialize hardware I2C pins for S7S
  Wire.begin();

  // Clear display, reset cursor
  s7s.clearDisplay();

  // Welcome
  s7s.sendString("-HI-");
  delay(2000);

