Ben Dixon
Published © MIT

Cap Touch Set-able Timer with OLED and CAP1296 using XinaBox

I built this project to help make our office meetings quicker and more efficient. Based on XinaBox running an ESP8266 with Capacitive Touch.

BeginnerFull instructions provided6 minutes900
Cap Touch Set-able Timer with OLED and CAP1296 using XinaBox

Things used in this project

Hardware components

CW01
XinaBox CW01
×1
IP01
XinaBox IP01
×1
OD01
XinaBox OD01
×1
XinaBox SH01
×1
XC10
XinaBox XC10
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Cap Touch Timer

Arduino
#include <xCore.h>
#include <Wire.h>
#include <xOD01.h>
#include <xSH01.h>

#include "xVARIABLES.h"

xSH01 SH01;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Wire.begin(2, 14);
  OLED.begin();
  SH01.begin();
  Print_OLED();
}

void loop()
{
  SCHEDULER();
}

void SCHEDULER(void) {
  if (millis() > (tick_Time + 1000)) {
    Tick();
    tick_Time = millis();
  }
  if (millis() > (tick_Time + 100)) {
    Press();
    tick_Press = millis();
  }
  if (millis() > (tick_Expired + 100)) {
    Timer_Expired();
    tick_Expired = millis();
  }
}

void Tick(void)
{
  if (timer_run)
  {
    S--;

    if (S < 0)
    {
      M--;
      S = 59;
    }
    if (M < 0)
    {
      H--;
      M = 59;
    }
    if (H < 0)
    {
      S = 0;
      M = 0;
      H = 0;
      timer_run = 0;
      timer_expired = 1;
    }

    Print_OLED();
  }
}

void Timer_Expired(void)
{
  if (timer_expired)
  {
    if (expired_count == 1)
    {
      Print_OLED_Expired();
      digitalWrite(LED_BUILTIN, HIGH);
    } else if (expired_count == 2)
    {
      digitalWrite(LED_BUILTIN, LOW);
    } else if (expired_count == 3)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      
    } else if (expired_count == 4)
    {
      digitalWrite(LED_BUILTIN, LOW);
    } else if (expired_count == 5)
    {
      digitalWrite(LED_BUILTIN, HIGH);
    } else if (expired_count == 6)
    {
      digitalWrite(LED_BUILTIN, LOW);
    } else if (expired_count == 7)
    {

    } else if (expired_count == 8)
    {

    } else if (expired_count == 9)
    {

    } else if (expired_count == 10)
    {
      expired_count = 0;
    }

    expired_count++;

  } else {
    expired_count = 1;
  }
}

void Press(void)
{
  SH01.poll();
  if (SH01.touchDetected()) {
    if (SH01.triangleTouched()) {
      trianglePress();
    } else if (SH01.circleTouched()) {
      circlePress();
    } else if (SH01.squareTouched()) {
      squarePress();
    } else if (SH01.crossTouched()) {
      crossPress();
    }
  }
  else {
    cross_latch = 0;
    triangle_latch = 0;
    square_latch = 0;
    circle_latch = 0;
  }
}

void trianglePress(void)
{
  if (!triangle_latch && timer_expired)
  {
    triangle_latch = 1;
    timer_expired = 0;
    Print_OLED();
  } else if (!triangle_latch && !timer_run) {
    triangle_latch = 1;
    S = set_S;
    M = set_M;
    H = set_H;
    timer_expired = 0;
    Print_OLED();
  }
}

void crossPress(void)
{
  if (!cross_latch)
  {
    cross_latch = 1;
    if (timer_expired)
    {
      timer_expired = 0;
      Print_OLED();
    } else {
      if (timer_run)
      {
        timer_run = 0;
      } else {
        timer_run = 1;
      }
    }
  }
}

void squarePress(void)
{
  if (!square_latch && !timer_run)
  {
    square_latch = 1;

    if (set_S == 0)
    {
      set_M--;
      set_S = 30;
    }
    else
    {
      set_S = 0;
    }

    if (set_M < 0)
    {
      set_H--;
      set_M = 59;
    }

    if (set_H < 0)
    {
      set_H = 0;
      set_M = 0;
      set_S = 0;
    }

    Print_OLED_Settings();
  }
}

void circlePress(void)
{
  if (!circle_latch && !timer_run)
  {
    circle_latch = 1;

    if (set_S == 30)
    {
      set_M++;
      set_S = 0;
    }
    else
    {
      set_S = 30;
    }

    if (set_M < 0)
    {
      set_H--;
      set_M = 59;
    }

    Print_OLED_Settings();
  }
}

void Print_OLED(void)
{
  OD01.clear();
  OD01.set2X();
  OD01.println("Timer:");
  if (H < 10)
  {
    OD01.print("0");
    OD01.print(H);
  }
  else
  {
    OD01.print(H);
  }
  OD01.print(":");
  if (M < 10)
  {
    OD01.print("0");
    OD01.print(M);
  }
  else
  {
    OD01.print(M);
  }
  OD01.print(":");
  if (S < 10)
  {
    OD01.print("0");
    OD01.print(S);
  }
  else
  {
    OD01.print(S);
  }
}

void Print_OLED_Settings(void)
{
  OD01.clear();
  OD01.set2X();
  OD01.println("Set:");
  if (set_H < 10)
  {
    OD01.print("0");
    OD01.print(set_H);
  }
  else
  {
    OD01.print(set_H);
  }
  OD01.print(":");
  if (set_M < 10)
  {
    OD01.print("0");
    OD01.print(set_M);
  }
  else
  {
    OD01.print(set_M);
  }
  OD01.print(":");
  if (set_S < 10)
  {
    OD01.print("0");
    OD01.print(set_S);
  }
  else
  {
    OD01.print(set_S);
  }
}

void Print_OLED_Expired(void)
{
  OD01.clear();
  OD01.set2X();
  OD01.println("TIME'S UP!");
  if (H < 10)
  {
    OD01.print("0");
    OD01.print(H);
  }
  else
  {
    OD01.print(H);
  }
  OD01.print(":");
  if (M < 10)
  {
    OD01.print("0");
    OD01.print(M);
  }
  else
  {
    OD01.print(M);
  }
  OD01.print(":");
  if (S < 10)
  {
    OD01.print("0");
    OD01.print(S);
  }
  else
  {
    OD01.print(S);
  }
}

xVARIABLES.h

C Header File
#ifndef xVARIABLES_h
#define xVARIABLES_h

long tick_Time = 0;
long tick_Press = 0;
long tick_Expired = 0;

int S = 0; // count seconds
int M = 5; // count minutes
int H = 0; // count hours

int set_S = 0; // set seconds
int set_M = 5; // set minutes
int set_H = 0; // set hours

bool cross_latch = 0;
bool triangle_latch = 0;
bool square_latch = 0;
bool circle_latch = 0;
bool timer_run = 0;
bool timer_expired = 0;

int expired_count = 1;

#endif

Credits

Ben Dixon
5 projects • 12 followers
I am an electronic engineer with a true love for the art of electronics. I live for anything technology. Yes, I'm a geek just like you!
Contact

Comments

Please log in or sign up to comment.