#include "U8g2lib.h"
#include "Bounce2.h"
int Minimum = 1;
int Maximum = 7;
int TASTER = 7;
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
// Bounce initialisieren
Bounce Wuerfel = Bounce();
int Radius = 5;
void setup()
{
pinMode(TASTER, INPUT_PULLUP);
// Taster Bounce zuordnen
Wuerfel.attach(TASTER);
Wuerfel.interval(20);
oled.begin();
// Zufallsgenerator starten
randomSeed(analogRead(A0));
// Farbe weiß
oled.setDrawColor(1);
// Position 90 Grad
oled.clearDisplay();
oled.setFont(u8g2_font_t0_22_te);
oled.enableUTF8Print();
// Hinweis anzeigen
oled.firstPage();
do
{
oled.setCursor(10, 20);
oled.print("Start");
oled.setCursor(10, 40);
oled.print("->");
oled.setCursor(10, 60);
oled.print("Taste");
}
while (oled.nextPage());
}
void Wuerfeln()
{
int Zahl = random(Minimum, Maximum);
oled.firstPage();
// Würfelaugen zeichnen
// 1
if (Zahl == 1)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(oled.getDisplayWidth() / 2, oled.getDisplayHeight() / 2, Radius);
}
while (oled.nextPage());
}
// 2
if (Zahl == 2)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(14, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, oled.getDisplayHeight() - 15, Radius);
}
while (oled.nextPage());
}
// 3
if (Zahl == 3)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(14, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() / 2, oled.getDisplayHeight() / 2, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, oled.getDisplayHeight() - 15, Radius);
}
while (oled.nextPage());
}
// 4
if (Zahl == 4)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(14, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, 14, Radius);
oled.drawDisc(14, oled.getDisplayHeight() - 15, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, oled.getDisplayHeight() - 15, Radius);
}
while (oled.nextPage());
}
// 5
if (Zahl == 5)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(14, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() / 2, oled.getDisplayHeight() / 2, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, 14, Radius);
oled.drawDisc(14, oled.getDisplayHeight() - 15, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, oled.getDisplayHeight() - 15, Radius);
}
while (oled.nextPage());
}
// 6
if (Zahl == 6)
{
do
{
oled.drawRFrame(0, 0, oled.getDisplayWidth(), oled.getDisplayHeight(), 5);
oled.drawDisc(14, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() / 2, 14, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, 14, Radius);
oled.drawDisc(14, oled.getDisplayHeight() - 15, Radius);
oled.drawDisc(oled.getDisplayWidth() / 2, oled.getDisplayHeight() - 15, Radius);
oled.drawDisc(oled.getDisplayWidth() - 15, oled.getDisplayHeight() - 15, Radius);
}
while (oled.nextPage());
}
}
void loop()
{
if (Wuerfel.update())
{
if (Wuerfel.read() == LOW)
{
// Würfeleffekt: Zufallszahlen in schneller Folge anzeigen
for (int i = 0; i < 5; i++)
{
int Zahl = random(Minimum, Maximum);
Wuerfeln();
delay(50);
}
}
}
}