/* Filename: 8x8dice.c Turns on and off LEDs at to display the roll of 2 dice in an 8x8 matrix. Pins 0-7 are the row pins, on if HIGH. Pins 8-15 are the column pins, on if LOW. Pin 16 is a pushbutton switch. It is connected between pin 16 and ground. There is a 10K pull up resistor connected between pin 16 and +3.3 Volts. */ #include #include #include #include /* 3x3 matrix arrays for dice */ int die_1[3][3] = { {0,0,0}, // Die rolled 1 {0,1,0}, {0,0,0} }; int die_2[3][3] = { {1,0,0}, // Die rolled 2 {0,0,0}, {0,0,1} }; int die_3[3][3] = { {1,0,0}, // Die rolled 3 {0,1,0}, {0,0,1} }; int die_4[3][3] = { {1,0,1}, // Die rolled 4 {0,0,0}, {1,0,1} }; int die_5[3][3] = { {1,0,1}, // Die rolled 5 {0,1,0}, {1,0,1} }; int die_6[3][3] = { {1,0,1}, // Die rolled 6 {1,0,1}, {1,0,1} }; int rowPin[8] = {0,1,2,3,4,5,6,7}; int columnPin[8] = {8,9,10,11,12,13,14,15}; int LED[8][8]; //8 rows, 8 columns - used to pass data between threads. void setup(void) { int i; if (geteuid() != 0) { fprintf (stderr, "You need to be root to run this program. (sudo?)\n") ; exit(0) ; } if (wiringPiSetup() == -1) { printf("wiringPiSetup error\n"); exit(1) ; } for(i = 0; i < 16; i++) { pinMode(i,OUTPUT); // Set pins 0 - 15 for output. } pinMode(16,INPUT); // Set pin 16 for input. srand(millis()); // Seed random number generator. } /* This thread reads the LED array and performs the multiplex. It turns on the pins for one LED at a time. Row 1 LED 1 - 2 - 3 ... Row 2 LED 1 - 2 - 3 ... The code here runs concurrently with the main program in an infinite loop. */ PI_THREAD (display) { int i,j; // int x = piHiPri(1); // Raise priority. while(1) { for(i = 0; i < 8; i++) // Row, HIGH is on. { if(i==0) digitalWrite(rowPin[7],0); // Turn off previous row. else digitalWrite(rowPin[i-1],0); digitalWrite(columnPin[7],1); // Turn off column 7 pin. digitalWrite(rowPin[i],1); // Turn on current row. for(j = 0; j < 8; j++) // Column, LOW is on. { if(j==0) digitalWrite(columnPin[7],1); // Turn off previous column. else digitalWrite(columnPin[j-1],1); if (LED[i][j]==1) { digitalWrite(columnPin[j],0); // Turn on current column. } } } } } /* Function to display the first die in the upper left corner of 8x8 LED matrix. */ void dspDie1(int die[3][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { LED[i][j] = die[i][j]; //Move die to LED for display } } } /* Function to display the Second die in the bottom left corner of 8x8 LED matrix. */ void dspDie2(int die[3][3]) { int i,j; for(i=5;i<8;i++) { for(j=0;j<3;j++) { LED[i][j] = die[i-5][j]; //Move die to LED for display } } } /* Displays random points in 8x8 matrix to simulate rolling dice. */ void rolling(void) { int i,j; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { int pick = rand(); // Get random number if((pick % 4) == 0) // 1 in 4 chance. { LED[i][j] = 1; // If random number is divisable by 4 set HIGH } else { LED[i][j] = 0; // Else set LOW } } } delay(10); } /* The loop function lights random LEDS to simulate rolling dice. Then it clears the matrix, rolls the dice and passes the correct array to the display dice functions. */ void loop() { int i,j; rolling(); // Display random LEDs to simulate rolling dice. for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { LED[i][j] = 0; // Turn off all LEDs. } } srand(millis()); // Seed random number generator. int pick = rand(); // Get random number int die1 = (pick % 6) + 1; // First die 1 - 6 pick = rand(); // Get random number int die2 = (pick % 6) + 1; // Second die 1 - 6 switch(die1) // Display first die. { case 1: dspDie1(die_1); break; case 2: dspDie1(die_2); break; case 3: dspDie1(die_3); break; case 4: dspDie1(die_4); break; case 5: dspDie1(die_5); break; case 6: dspDie1(die_6); break; } switch(die2) // Display second die. { case 1: dspDie2(die_1); break; case 2: dspDie2(die_2); break; case 3: dspDie2(die_3); break; case 4: dspDie2(die_4); break; case 5: dspDie2(die_5); break; case 6: dspDie2(die_6); break; } } int main(void) { int x = piThreadCreate (display) ; // Start second thread. if (x != 0) printf ("It didn't start.\n"); setup(); int count = 0; while(1) { while(digitalRead(16)==LOW) { loop(); count++; if(count==10) { srand(millis()); // Seed random number generator. count=0; } } } return(0); }