timer — Arduino Nano simulation

tes program

An interactive Arduino Nano circuit simulation you can run free in your browser on Velxio, by aripaintco.

Components

Sketch code

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

// =====================================
// KONFIGURASI OLED
// =====================================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define OLED_ADDRESS 0x3C

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  OLED_RESET
);

// =====================================
// PIN ARDUINO PRO MINI
// =====================================
#define ENC_CLK 2
#define ENC_DT  3
#define ENC_SW  4

#define RELAY_PIN 8
#define BUZZER_PIN 9

#define RELAY_ACTIVE_LOW true

Encoder encoder(ENC_CLK, ENC_DT);

// =====================================
// PENGATURAN TIMER
// =====================================
const unsigned long MAX_TIME = 5999;
const unsigned long STEP_TIME = 1;

unsigned long setTime = 60;
unsigned long remainingTime = 60;

unsigned long previousMillis = 0;
unsigned long finishedMillis = 0;

const unsigned long TIMER_INTERVAL = 1000;
const unsigned long AUTO_RESET_DELAY = 3000;

// =====================================
// PRESET TIMER
// =====================================
const unsigned long presets[] = {
  60,
  300,
  600,
  900,
  1800
};

const int presetCount =
  sizeof(presets) / sizeof(presets[0]);

int presetIndex = 0;

// =====================================
// STATUS TIMER
// =====================================
bool timerRunning = false;
bool timerPaused = false;
bool timerFinished = false;
bool presetMenu = false;

// =====================================
// ROTARY ENCODER
// =====================================
long oldEncoderPosition = 0;

// =====================================
// BUTTON
// =====================================
bool lastButtonState = HIGH;
bool buttonPressed = false;

unsigned long buttonPressTime = 0;
unsigned long lastClickTime = 0;

int clickCount = 0;

const unsigned long DEBOUNCE_DELAY = 40;
const unsigned long LONG_PRESS_TIME = 1000;
const unsigned long DOUBLE_CLICK_TIME = 350;

// =====================================
// RELAY
// =====================================
void relayOn() {
  digitalWrite(
    RELAY_PIN,
    RELAY_ACTIVE_LOW ? LOW : HIGH
  );
}

void relayOff() {
  digitalWrite(
    RELAY_PIN,
    RELAY_ACTIVE_LOW ? HIGH : LOW
  );
}

// =====================================
// FORMAT WAKTU
// =====================================
void formatTime(unsigned long seconds, char *buffer) {
  unsigned int minutes = seconds / 60;
  unsigned int secs = seconds % 60;

  sprintf(
    buffer,
    "%02u:%02u",
    minutes,
    secs
  );
}

// =====================================
// TAMPILAN TIMER UTAMA
// TANPA ICON DAN PROGRESS BAR
// ANGKA TEPAT DI TENGAH
// =====================================
void drawTimerScreen() {
  display.clearDisplay();

  // Tampilan khusus ketika timer dijeda
  if (timerPaused) {
    display.setTextSize(3);
    display.setTextColor(SSD1306_WHITE);

    int16_t x1, y1;
    uint16_t textWidth, textHeight;
    display.getTe

More projects by aripaintco