25. Januar 2025

Baue dein eigenes Airbus Chronometer (Part 1)

https://youtu.be/NlULbY8Ecs8

Elektronische Bauteile:

Software:

Der Arduino Code:

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2_a(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

// Font for Airbus Chronometer:
const uint8_t AirbusA320LCDfont_26pt[317] U8G2_FONT_SECTION("AirbusA320LCDfont_26pt") = 
  "\15\0\4\4\4\5\5\4\6\16\27\1\0\27\0\27\0\0\0\0\0\1 .\7\63,\302\220\0/\37"
  "~#\302+\226\350P\242C\211\16%:v\254x\220b'%:\224\350P\242C\311\212\5\60\32~"
  "#\302\241f\205\220\207\366a\321\261!\204\26\264\17\207\260P!B\15\0\61\16s\71\302\21\342\1\13"
  "A\17\134\204\0\62\30~#\302\241f\205\20\266\363\33\24B\224\210@\63v\276V\263\11\0\63\31~"
  "#\302\241f\205\20\266\363\223\24\42\330\244\20;\77y\200B\204\32\0\64\26~#\302\34\42\250A\373"
  "a\11\24BT\241\20;\377\70\4\0\65\31~#\302\261\204\221\222\261\363\255\10T\252P\210\235\237<"
  "@!B\15\0\66\34~#\302\261\204\221\222\261\363\255\10\64!\224\210@Q\320>\34\302B\205\10\65"
  "\0\67\25~#\302\241f\205\220\207\366aQ\301\241\305\316\77\16\1\0\70\34~#\302\241f\205\220\207"
  "\366a\11\24#V\210@Q\320>\34\302B\205\10\65\0\71\32~#\302\241f\205\220\207\366a\11\24"
  "B\26\241\20;\77y\200B\204\32\0:\11\323l\303\220\36X\2\0\0\0\4\377\377\0";

const int PIN_BUTTON_START = 4;
const int PIN_BUTTON_RESET = 7;

char timeString[9]; // String für die aktuelle Uhrzeit (UTC)
char chroString[5]; // String für den Timer

byte timerOn = false;
byte timerRunning = false;

byte time_minute = 0;
byte time_hour = 0;
byte time_second = 0;

byte chro_minute = 0;
byte chro_second = 0;

unsigned long currentMillis = 0;
unsigned long elapsedButtonMillis = 0;
unsigned long previousButtonMillis = 0;
int elapsedTimeUpdateMillis = 0;
unsigned long lastTimeUpdateMillis = 0;

const int BUTTON_DEBOUNCE_TIME = 300; // ggf. anpassen

// Serielle Kommunikation:
String incomingByte = "0";
int numberIncomingByte = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);

  u8g2_a.setFont(AirbusA320LCDfont_26pt);
  u8g2_a.begin();

  pinMode(PIN_BUTTON_START, INPUT_PULLUP);
  pinMode(PIN_BUTTON_RESET, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:

  // Hour: 1XX 115
  // Minute: 3XX 312
  // Second: 2XX

  // Get correct time from Computer:
  if (elapsedTimeUpdateMillis > 1000) {
    while (Serial.available() != 0) {
      incomingByte = Serial.readString();
      int checkIfZero = incomingByte.toInt();
      if (checkIfZero != 0) {
        int subIncomingByte = incomingByte.substring(0, 1).toInt();
        numberIncomingByte = incomingByte.substring(1,3).toInt();
        switch (subIncomingByte) {
          case 1:
            time_hour = numberIncomingByte;
            break;
          case 2:
            time_second = numberIncomingByte;
            break;
          case 3: 
            time_minute = numberIncomingByte;
            break;
        }
      }
    }
  }


  currentMillis = millis();
  elapsedTimeUpdateMillis = currentMillis - lastTimeUpdateMillis;
  elapsedButtonMillis = currentMillis - previousButtonMillis;

  if (elapsedButtonMillis > BUTTON_DEBOUNCE_TIME) {
    if (digitalRead(PIN_BUTTON_START) == LOW) {
      timerOn = true; // Time soll angezeigt werden
      timerRunning = !timerRunning; // Timer soll laufen
      previousButtonMillis = currentMillis;
    }
    if (digitalRead(PIN_BUTTON_RESET) == LOW) {
      chro_second = 0;
      chro_minute = 0;
      if (timerRunning == false) {
        timerOn = false;
      }
      previousButtonMillis = currentMillis;
    }
  }


  // Calculate current UTC and Chrono:
  if (time_second == 60) {
    time_second = 0;
    time_minute++;
  }
  if (time_minute == 60) {
    time_minute == 0;
    time_hour++;
  }
  if (time_hour == 24) {
    time_hour = 0;
  }

  if (chro_second == 60) {
    chro_second = 0;
    chro_minute++;
  }
  if (chro_minute > 99) {
    chro_minute= 0;
  }


  if (elapsedTimeUpdateMillis > 1000) {
        // Draw Strings on Display
    u8g2_a.firstPage();
    do {
      sprintf_P(timeString, PSTR("%02d:%02d:%02d"), time_hour, time_minute, time_second); // 00:00:00
      u8g2_a.drawStr(0,64,timeString);
      if (timerOn) {
        sprintf_P(chroString, PSTR("%02d:%02d"), chro_minute, chro_second); // 00:00
        u8g2_a.drawStr(24,26,chroString);
      }
    } while (u8g2_a.nextPage());

    time_second++;
    if (timerRunning) {
      chro_second++;
    }
    lastTimeUpdateMillis = currentMillis;
  }
} 

Schreibe einen Kommentar