i've been working on this d20 code for a bit and i'm kind of at a loss.
i want it to, on a button press, quickly cycle through a series of random numbers before settling on one random number and then clear the screen and start over ie, wait for another button press.
so far it begins operating as planned on the initial button press, but starts cycling through numbers (much slower than i'd hoped) again after it clears the screen.
i'm sure i'm just leaving out something simple to get the while loop to exit and start the program again but i cannot seem to figure it out. my intuition was to use lbl and goto like i would have in days of old on tibasic, but i've since learned that that is frowned upon.
thanks
#include "ssd1306.h"
#include <Arduino.h>
#include <U8g2lib.h>
#include <TrueRandom.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
const byte buttonPin = 5;
int buttonState = 0;
int LED = 4;
void setup(void)
{
Wire.setClock(400000);
u8g2.begin();
pinMode(5, INPUT_PULLUP);
pinMode(LED, OUTPUT);
while (digitalRead(5) == HIGH ){}
}
void loop(void) {
int x = 0;
while(x<10){
int randVal = TrueRandom.random(1,21); //set randVal to a number between 1-20
String randString = String(randVal);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_fub20_tr); // choose a suitable font
u8g2.drawStr(50, 40, randString.c_str()); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
x = x+1;
u8g2.clear();
if (x == 10){
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_fub20_tr); // choose a suitable font
u8g2.drawStr(50, 40, randString.c_str()); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
digitalWrite(LED, HIGH);
delay(3000);
u8g2.clear();
digitalWrite(5,LOW);
digitalWrite(LED,LOW);
if(digitalRead(5) == LOW );
u8g2.clear();
}
}
}