Arduino dice version 2

Arduino Dice version 2

I have now improved on my Arduino dice by adding a second digit. The code can be found below. I have streamlined the code a bit but I left the constants in place so that it is easy to keep track of which pin is connected to which led segment. I get confused easily so I have to make it simple to change if neccessary!

(Just a note on the formatting of the code in this blog. I’m using WordPress and I have no idea what the logic is behind the way the code is formatted. If I find out how to properly format code one day, I’ll update the code snippets. However, if you copy the code and past it into the Arduino IDE, you should be able to easily format it by pressing control-t.)


#define LED1_A 6
#define LED1_B 5
#define LED1_C A3
#define LED1_D 12
#define LED1_E A0
#define LED1_F 7
#define LED1_G 8
#define LED1_H 13
#define LED2_A 2
#define LED2_B 3
#define LED2_C A5
#define LED2_D A4
#define LED2_E A2
#define LED2_F 9
#define LED2_G 10
#define LED2_H 11
#define BUTTON 4

const int leds[2][8] = {
{LED1_A, LED1_B, LED1_C, LED1_D, LED1_E, LED1_F, LED1_G, LED1_H},
{LED2_A, LED2_B, LED2_C, LED2_D, LED2_E, LED2_F, LED2_G, LED2_H}
};

char digits[10][7] = {
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW},
{LOW, HIGH, HIGH, LOW, LOW, LOW, LOW},
{HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH},
{HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH},
{LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH},
{HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH},
{HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH},
{HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW},
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH},
{HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH}
};
int seed1;
int seed2;
int num1 = 0;
int num2 = 0;
int buttonState = 0;
const int delaid = 400;

void setup() {
Serial.begin(9600);
pinMode(A1, INPUT); // random seed generator
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 8; j++) {
pinMode(leds[i][j], OUTPUT);
digitalWrite(leds[i][j], LOW);
}
}
seed1 = analogRead(1);
pinMode(BUTTON, INPUT);
}

void loop() {
buttonState = digitalRead(BUTTON);
Serial.println(buttonState);
if (buttonState == HIGH) {
num1 = random(1, 7);
num2 = random(1, 7);

} else {
disp(0, digits[num1]);
disp(1, digits[num2]);
}
}

 

void disp(int led, char segment[]) {
for (int i = 0; i < 7; i++) {
digitalWrite(leds[led][i], segment[i]);
}
}