Count­down mit einem OLED-Display

Lese­zeit: 7 Minu­ten

Lösung
Seite als PDF

Auf dem ➨OLED-Dis­play wird eine Stopp­uhr als Start­sym­bol ange­zeigt, ein Druck auf den Tas­ter star­tet den Count­down. Eine Ziel­flag­ge zeigt das Ende des Count­downs an.

So sieht es aus:

width=150
width=150width=150

Benö­tig­te Bauteile:

  • Tas­ter
  • OLED-Dis­play
  • Lei­tungs­dräh­te

Benö­tig­te Bibliotheken:

oder: Sketch -> Biblio­thek ein­bin­den -> Biblio­the­ken verwalten

Funk­tio­nen der Bibi­lo­thek u8g2

Bin­de die benö­tig­ten Biblio­the­ken ein und defi­nie­re die Variablen.

# include "U8g2lib.h"
# include "Bounce2.h"

// 1,3 Zoll SH1106
// U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);

// 0,96 Zoll SSD1306
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);

// Bildschirmgröße
int BildschirmBreite = oled.getDisplayWidth();
int BildschirmHoehe = oled.getDisplayHeight();

int TASTER = 7;

// Stoppuhr als Objekt der Bibliothek Bounce
Bounce StoppUhr = Bounce();

Die xbm-Bil­der:

// Bild Stoppuhr
# define StoppuhrBreite 50
# define StoppuhrHoehe 56
static unsigned char Stoppuhr[] =
{
  0x00, 0x00, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0xe0, 0x0f, 0x80,
  0x1f, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x78,
  0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0xe0, 0x01, 0x00,
  0x00, 0x0f, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x07, 0x00, 0x00, 0x04,
  0x07, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x0e, 0x00, 0xc0, 0x01, 0x01,
  0x00, 0x00, 0x1c, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x70,
  0x00, 0x00, 0x02, 0x00, 0x38, 0x00, 0x30, 0x00, 0x00, 0x02, 0x00, 0x70,
  0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x98, 0x00, 0x00, 0x02,
  0x00, 0xe2, 0x00, 0x1c, 0x00, 0x00, 0x03, 0x00, 0xc0, 0x00, 0x1c, 0x00,
  0x00, 0x02, 0x00, 0xc0, 0x00, 0x0c, 0x00, 0x00, 0x03, 0x00, 0xc0, 0x01,
  0x0e, 0x00, 0x00, 0x03, 0x00, 0x80, 0x01, 0x0e, 0x00, 0x00, 0x07, 0x00,
  0x80, 0x01, 0x06, 0x00, 0x00, 0x07, 0x00, 0x80, 0x03, 0x06, 0x00, 0x00,
  0x07, 0x00, 0x80, 0x01, 0x06, 0x00, 0xc0, 0x07, 0x00, 0x00, 0x03, 0x06,
  0x00, 0x80, 0x07, 0x00, 0x80, 0x01, 0x96, 0x00, 0x80, 0x0f, 0x00, 0x80,
  0x03, 0x06, 0x00, 0x00, 0x1f, 0x00, 0x80, 0x01, 0x06, 0x00, 0x00, 0x3e,
  0x00, 0x00, 0x03, 0x06, 0x00, 0x00, 0xf8, 0x00, 0x80, 0x03, 0x06, 0x00,
  0x00, 0xf0, 0x01, 0x80, 0x01, 0x0e, 0x00, 0x00, 0x80, 0x01, 0x80, 0x01,
  0x0e, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x0e,
  0x80, 0x01, 0x0c, 0x00, 0x00, 0x00, 0x18, 0xc1, 0x00, 0x1c, 0x04, 0x00,
  0x00, 0x20, 0xe0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x38,
  0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x70,
  0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0xe0, 0x00, 0x00, 0x00,
  0x00, 0x3c, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x80, 0x01,
  0x00, 0x00, 0x00, 0x0f, 0x00, 0x80, 0x87, 0x00, 0x02, 0x08, 0x07, 0x00,
  0x00, 0x1f, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x3c, 0x00, 0x00, 0xf0,
  0x01, 0x00, 0x00, 0x78, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0xf0, 0x01,
  0x00, 0x3e, 0x00, 0x00, 0x00, 0xc0, 0x7f, 0xe0, 0x0f, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x7f, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// Bild Zielflagge
# define ZielBreite 50
# define ZielHoehe 53
static unsigned char Zielflagge[] =
{
  0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xfe, 0xff, 0x01, 0x00, 0xfe,
  0xff, 0x03, 0xfe, 0x7f, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00,
  0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe,
  0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff,
  0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00,
  0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff,
  0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03,
  0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc,
  0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00,
  0x00, 0xfc, 0xff, 0x03, 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02,
  0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00,
  0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff,
  0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00,
  0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02,
  0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01,
  0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe,
  0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02,
  0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00,
  0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff,
  0x01, 0x00, 0x02, 0x02, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x02, 0x02, 0x00,
  0xaa, 0xaa, 0x00, 0x00, 0x02, 0x06, 0x44, 0x00, 0x00, 0x54, 0xd5, 0x03,
  0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc,
  0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00,
  0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe,
  0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff,
  0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00,
  0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff,
  0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03,
  0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc,
  0xff, 0x03, 0xfe, 0xff, 0x00, 0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0x01,
  0x00, 0xfc, 0xff, 0x03, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03
};

Der set­up-Teil. Beach­te die Kommentare.

void setup()
{
  StoppUhr.attach(TASTER);

  // Display starten
  oled.begin();
  
  // Kontrast maximal 255
  oled.setContrast(200);
  pinMode(TASTER, INPUT_PULLUP);

  // Stoppuhr anzeigen
  oled.firstPage();
  do
  {
    oled.drawXBM(40, 1, StoppuhrBreite, StoppuhrHoehe, Stoppuhr);
  }
  while (oled.nextPage());
}

Der loop-Teil. Beach­te die Kommentare.

void loop()
{
  // Farbe weiß
  oled.setDrawColor(1);
  
  // Beginn des Countdowns
  int Nummer = 9;
  int TasterLesen = digitalRead(TASTER);

  // Taster gedrückt
  if (StoppUhr.update())
  {
    if (StoppUhr.read() == LOW)
    {
      oled.setFont(u8g2_font_logisoso62_tn);
      while (Nummer > 0)
      {
        oled.firstPage();
        do
        {
          oled.setFontDirection(0);
          oled.setCursor(50, BildschirmHoehe);
          oled.print(Nummer);
        }
        while (oled.nextPage());
        delay(1000);
        Nummer --;
      }

      // Ziel anzeigen
      oled.firstPage();
      do
      {
        oled.drawXBM(40, 5, ZielBreite, ZielHoehe, Zielflagge);
      }
      while (oled.nextPage());
    }
  }
}

Startseite
Aufgaben A-Z
Suchen
Downloads
Fehlermeldungen
Seite als PDF

Ver­wand­te Aufgaben:


Letzte Aktualisierung: 9. Okt 2023 @ 17:17