LED Dice not working

I just pressed the button.


Wait I can add a delay so it doesnt print one million numbers

Ok, good.

But please don't post images of serial monitor. Copy the text from serial monitor and paste it into your reply, between code tags.

Then digitalRead(button); does not equal zero

Try this

void loop()
{
    byte buttonState = digitalRead(button);
    Serial.println(buttonState);
    if (buttonState == 0)
//etc

Alright sorry

yes i know i forgot to ACTUALLY press the button :expressionless:

void loop() {
  if(digitalRead(button) == 0) {
    num = random(1, 7);
    Serial.print(num);
  }
  delay(1000);
}

I don't think i did it right it's not printing anything now

Actually, you could use the amount of time that the button is pressed as a truly random number. You can time how long the button is held down to the microsecond. No human can press a button for an exact amount of time, so it's actually more random than the Arduino random() function, which isn't truly random at all.

But let's come to that later, when your basic sketch is working.

Yes, but i need a number from 1 to 6

You will need to hold down the button for at least a second.

That can be achieved with a little maths.

Starting!
226353136216134622

It's working now, i put the delay at 300, which feels normal for me as i don't accidentally press it too long and print more than 1 number.
So can I try my original code now?
Wait the original code with the new modifications is working!

Huh? When i put:

num = random(1, 7);

none of the LEDs light up when i press and even hold the button

Post your latest code.

#define button 8
#define a 2
#define b 3
#define c 4
#define d 5
#define e 6
#define f 7
int num;
void setup() {
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  pinMode(button, INPUT_PULLUP);
}
void loop() {
  if(digitalRead(button) == 0) {
    num = random(1, 7);
    if(num == 1) {
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if(num == 2) {
      digitalWrite(b, HIGH);
      digitalWrite(a, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if(num == 3) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if(num == 4) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if(num == 5) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
    } else if(num == 6) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
    }
  }
}

You seem to have lost the Serial.print() again.

ooooopsies

Ok

#define button 8
#define a 2
#define b 3
#define c 4
#define d 5
#define e 6
#define f 7
int num;
void setup() {
  Serial.begin(115200);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  pinMode(button, INPUT_PULLUP);
}
void loop() {
  if (digitalRead(button) == 0) {
    num = random(1, 6);
    Serial.println(num);
    if (num == 1) {
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if (num == 2) {
      digitalWrite(b, HIGH);
      digitalWrite(a, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if (num == 3) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if (num == 4) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
    } else if (num == 5) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
    } else if (num == 6) {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
    }
  }
  delay(300);
}

But its still not working when i push the button even with random(1, 6);

But is it printing anything?

Is anything being printed in setup(). If not, could that be because you lost some other lines of code?

By the way, to display a dice, I would say you need 7 LEDs

OOO
 O 
OOO

1

...
 O
...

2

O..
 .
..O 

...
5

O.O
 O
O.O

etc...

1 Like

Try this code:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.