First of all, look at this line of code:
if (analogRead(BUTTON_PIN) == HIGH)
You probably want to use digitalRead()
. Note that it is okay to use digitalRead() on an analog pin, just make sure to use the right pin number (A0
, etc.)
From the Arduino Source:
#define HIGH 0x1
#define LOW 0x0
(Ignore the 0x
, that is just specifying that it's hexadecimal. However, 0 and 1 are the same in any base)
So, really, this function is only activated when analogRead()
returns 1, which could possibly cause a problem later if it doesn't check at the right time. Note: now is a good time to remind you about debouncing...
For your actual code, since you want it to be simple, I suggest you forget that code that you wrote. Arduino has a built in random library, which works unless you have some special need for a random number.
Your final code should look like something like this in the end:
void loop() {
if (analogRead(BUTTON_PIN) == HIGH) {
paintDieFace(random(1, 6);
}
}
Note that you should also use this code here in your setup (make sure that analog pin is floating: nothing should be attached to it):
randomSeed(analogRead(A1));
It actually isn't random, just pseudo-random. That means it relies on an equation to generate what seem to be random numbers. Because there has to be a default value of where the equation starts (the seed), it will repeat the same numbers every time you run it.
If you're wondering how this works, know that the analog pin is affected by a ton of external factors when there is no other electric signal attached.