Left and right dice from one TM1637 and arduino uno

My idea to roll 2 dice was to repeat the initial code twice , just changing the output to tm.display(0,1) and then tm.display(3,1), but no joy.
Below code just shows number twice. I want to different random rolls, by delaying the same repeated code with just 2 quick button presses.

#include <TM1637.h>

//declare pins
int CLK = D2;
int DIO = D3;
int DIC = D8;

int buttonState = 0;
int randNum = 0;

TM1637 tm(CLK, DIO);

void setup() {
  // put your setup code here, to run once:

  //define input ouptut pins
  pinMode(DIC, INPUT);

  //initialize serial comm @ 9600
  Serial.begin(115200);

  //initializes the pseudo-random number generator
  randomSeed(analogRead(0));

  //initialize TM1637 seven segment display
  tm.init();

  //set brightness; 0-7
  tm.set(2);
  tm.init();
}

void loop() {

  //read button
  buttonState = digitalRead(DIC);
  //decision taking
  if (buttonState == LOW) {
    randNum = throwDice();    //call thowDice function
    Serial.println(randNum);  //serial print for debugging

    //switch case
    // tm.display(position, character);
    switch (randNum) {
      case 1:
        tm.display(0, 1);
        tm.display(3, 1);
        delay(5000);
        randNum = 0;
        break;
      case 2:
        tm.display(0, 2);
        tm.display(3, 2);
        delay(5000);
        randNum = 0;
        break;
      case 3:
        tm.display(0, 3);
        tm.display(3, 3);
        delay(5000);
        randNum = 0;
        break;
      case 4:
        tm.display(0, 4);
        tm.display(3, 4);
        delay(5000);
        randNum = 0;
        break;
      case 5:
        tm.display(0, 5);
        tm.display(3, 5);
        delay(5000);
        randNum = 0;
        break;
      case 6:
        tm.display(0, 6);
        tm.display(3, 6);

        randNum = 0;
        break;
    }
    buttonState = 0;  //rule out false triggering
    randNum = 0;      //clear last digit

  }
  //waits for swith to be pressed
  else {

    tm.display(0, 0);
    tm.point(1);
    tm.display(3, 0);

    buttonState = 0;
    randNum = 0;
  }
}
//function to generate randowm number
int throwDice() {

  int numRand = random(1, 7);  // min=1 and max= max-1, 6
  return numRand;
}
#include <TM1637.h>

//declare pins
int CLK = D2;
int DIO = D3;
int DIC = D8;

TM1637 tm(CLK, DIO);

void setup() {
  //define input ouptut pins
  pinMode(DIC, INPUT_PULLUP);

  //initializes the pseudo-random number generator
  randomSeed(analogRead(0));

  //initialize TM1637 seven segment display
  tm.init();

  //set brightness; 0-7
  tm.set(2);
  tm.init();
}

void loop() {
  while (digitalRead(DIC));

  tm.display(0, random(1, 7));
  tm.display(3, random(1, 7));
  delay(5000);
}

Thanks for the quick reply Kolaha, greatly shortened and simplified my beginner coding, BUT, still displays same number, even after I put a delay between the two while loop displays. Guess it is using same random number.
I have my button between D8 and grnd

I repeated your while loop and took out the last delay and it seems to be working!

#include <TM1637.h>

//declare pins
int CLK = D2;
int DIO = D3;
int DIC = D8;

TM1637 tm(CLK, DIO);

void setup() {
  //define input ouptut pins
  pinMode(DIC, INPUT_PULLUP);

  //initializes the pseudo-random number generator
  randomSeed(analogRead(0));

  //initialize TM1637 seven segment display
  tm.init();

  //set brightness; 0-7
  tm.set(2);
  tm.init();
}

void loop() {
  while ( ! digitalRead(DIC)); // stop here until input is HIGH

  tm.display(0, random(1, 7));
  tm.display(3, random(1, 7));
  delay(5000);  // stop to prevent very quick change to next numbers. we can use here same Func as above, inverted of course
//while (  digitalRead(DIC)); // stop here until input is LOW
}

A 'not' operand, thanks very much for your time!
Greg

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