new — Xiao Nrf52840 Sense simulation

An interactive Xiao Nrf52840 Sense circuit simulation you can run free in your browser on Velxio, by melimelum586.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Clock.h"
#include "TimeSetup.h"

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDRESS 0x3C

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

// Кнопки
const int buttonPlusPin = D1;   // +
const int buttonMinusPin = D2;  // -
const int buttonOKPin = D7;     // OK
// D0 поки не використовується

int lastDisplayedSecond = -1;

// Стан для усунення брязкоту контактів
struct ButtonState {
  int lastReading = HIGH;
  int stableState = HIGH;
  unsigned long lastChange = 0;
};
ButtonState okState;

// Повертає true один раз при новому натисканні
bool pressedEvent(int pin, ButtonState &state) {
  int reading = digitalRead(pin);

  if (reading != state.lastReading) {
    state.lastChange = millis();
    state.lastReading = reading;
  }

  if (millis() - state.lastChange >= 25) {
    if (reading != state.stableState) {
      state.stableState = reading;

      if (state.stableState == LOW) {
        return true;
      }
    }
  }

  return false;
}

void drawClock();

enum Screen {
  SET_TIME,
  CLOCK,
  MENU_ALARM,
  ALARM
};

Screen currentScreen = SET_TIME;

void setup() {
  pinMode(buttonPlusPin, INPUT_PULLUP);
  pinMode(buttonMinusPin, INPUT_PULLUP);
  pinMode(buttonOKPin, INPUT_PULLUP);

  Wire.begin();

  if (!display.begin(
        SSD1306_SWITCHCAPVCC,
        OLED_ADDRESS
      )) {
    while (true) {
      delay(10);
    }
  }

  Serial.begin(115200);

  initClock();

  // Чекаємо, поки кнопка OK буде відпущена
  while (digitalRead(buttonOKPin) == LOW) {
    delay(10);
  }

  // Синхронізуємо початковий стан кнопки
  okState.lastReading = HIGH;
  okState.stableState = HIGH;
  okState.lastChange = millis();

  delay(50);

  // Відкриваємо налаштування часу
  beginTimeSetup(display);
}

void loop() {
  Serial.println(digitalRead(buttonOKPin));
  delay(300);
  // НАЛАШТУВАННЯ ЧАСУ
  if (isTimeSetupActive()) {
    static bool plusWasPressed = false;
    static bool minusWasPressed = false;

    static unsigned long plusStartTime = 0;
    static unsigned long minusStartTime = 0;

    static unsigned long plusLastRepeat = 0;
    static unsigned long minusLastRepeat = 0;

    const unsigned long repeatDelay = 400;
    const unsigned long repeatInterval = 100;

    bool plusPressed =
      digitalRead(buttonPlusPin) == LOW;

    bool minusPressed =
      digitalRead(buttonMinusPin) == LOW;

    unsigned long now = millis();

    // Кнопка +
    if (plusPressed) {
      if (!plusWasPressed) {
        // Перша зміна відразу після натискання
        handleTimeSetupButton(SETUP_PLUS, display);

        plusStartTime = now;
        plusLastRepeat = now;
        plusWasPressed = true;
      } else if (
        now - plusStartTime >= repeatDelay &&
        now - plusLastRepeat >= repeatInterval
      ) {
        // Повторення при утриманні
        handleTimeSetupButton(SETUP_PLUS, display);
        plusL

More projects by melimelum586