Try Embedded

"C basics: an electronic die"

Press the button to roll: the face is drawn on seven LEDs with a switch statement, and a tally checks the die is fair.

Basic · Beginner

In a real product: Electronic board game die (Consumer electronics): A game shows each throw on LEDs laid out like a die's pips.

Seven LEDs, laid out like the pips of a die, can show any face from 1 to 6. This sketch rolls when you press the button: it flickers through random faces while slowing down, then stops on one. It keeps a tally of every throw, so you can see whether the die is fair.

What you'll learn

  • switch and case for choosing between fixed values, and why each case needs its break
  • if/else, while and for together in one small program
  • random(1, 7), which never returns 7, and seeding it with randomSeed()
  • An array as a tally: counts[face]++

How it works

The pips are numbered like this: 0 top left, 1 top right, 2 middle left, 3 centre, 4 middle right, 5 bottom left and 6 bottom right. showFace() turns them all off, then a switch lights the right ones. Face 3 is two corners and the centre. Face 6 is both columns.

Some faces share pips, so the code could fall through from one case to the next. That saves little and is easy to get wrong, so each case here sets its own pips and ends with break.

A roll shows 12 random faces with a delay that grows each time (delay(30 + i * i * 2)), like a die slowing down. The last one counts. randomSeed(analogRead(A5)) seeds the generator from a floating analog pin, so the sequence differs from run to run.

The board rolls once at start-up, and then each time the button goes from released to pressed.

Try this

  • Remove one break and see which faces go wrong.
  • Roll 600 times in a loop without showing anything and print the tally: each face should come up about 100 times.
  • Make a long press roll two dice, one after the other, and print their sum.

sketch.ino

// An electronic die: press the button on pin 2 to roll. Pips: 0 top-left, 1 top-right, 2 middle-left, 3 centre,
// 4 middle-right, 5 bottom-left, 6 bottom-right.
const int PIPS[7] = { 3, 4, 5, 6, 7, 8, 9 };
const int BUTTON = 2;
unsigned counts[7];               // counts[1] to counts[6]

void showFace(int face) {
  for (int i = 0; i < 7; i++) digitalWrite(PIPS[i], LOW);
  switch (face) {
    case 1: digitalWrite(PIPS[3], HIGH); break;
    case 2: digitalWrite(PIPS[0], HIGH); digitalWrite(PIPS[6], HIGH); break;
    case 3: digitalWrite(PIPS[0], HIGH); digitalWrite(PIPS[3], HIGH); digitalWrite(PIPS[6], HIGH); break;
    case 4:
      digitalWrite(PIPS[0], HIGH); digitalWrite(PIPS[1], HIGH);
      digitalWrite(PIPS[5], HIGH); digitalWrite(PIPS[6], HIGH);
      break;
    case 5:
      digitalWrite(PIPS[0], HIGH); digitalWrite(PIPS[1], HIGH); digitalWrite(PIPS[3], HIGH);
      digitalWrite(PIPS[5], HIGH); digitalWrite(PIPS[6], HIGH);
      break;
    case 6:
      digitalWrite(PIPS[0], HIGH); digitalWrite(PIPS[1], HIGH); digitalWrite(PIPS[2], HIGH);
      digitalWrite(PIPS[4], HIGH); digitalWrite(PIPS[5], HIGH); digitalWrite(PIPS[6], HIGH);
      break;
    default: break;               // 0: all off
  }
}

int roll() {
  int face = 1;
  for (int i = 0; i < 12; i++) {
    face = random(1, 7);          // 1 to 6
    showFace(face);
    delay(30 + i * i * 2);        // slowing down
  }
  return face;
}

void printTally() {
  Serial.print("tally:");
  for (int f = 1; f <= 6; f++) {
    Serial.print(' ');
    Serial.print(f);
    Serial.print('=');
    Serial.print(counts[f]);
  }
  Serial.println();
}

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 7; i++) pinMode(PIPS[i], OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  randomSeed(analogRead(A5));
  int face = roll();
  counts[face]++;
  Serial.print("rolled ");
  Serial.println(face);
  printTally();
}

void loop() {
  static bool wasPressed = false;
  bool pressed = digitalRead(BUTTON) == LOW;
  if (pressed && !wasPressed) {
    int face = roll();
    counts[face]++;
    Serial.print("rolled ");
    Serial.println(face);
    printTally();
  }
  wasPressed = pressed;
  delay(10);
}

Lessons behind it