Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created April 26, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/dab381b4c9fadfe8785158a2628acef5 to your computer and use it in GitHub Desktop.
Save JeffersGlass/dab381b4c9fadfe8785158a2628acef5 to your computer and use it in GitHub Desktop.
//The three pins connected to their respective ports
//on the shift registers
int CLK_PIN = 10;
int DATA_PIN = 11;
int LATCH_PIN = 12;
///////////////////////Display Data//////////
//If a given row is to be "turned on" (in our case, pulled to ground),
//which bit in the shift registers does that correspond to?
int ROW0 = 9;
int ROW1 = 14;
int ROW2 = 8;
int ROW3 = 12;
int ROW4 = 1;
int ROW5 = 7;
int ROW6 = 2;
int ROW7 = 5;
const int NUMROWS = 8;
int rows[] = {ROW0, ROW1, ROW2, ROW3, ROW4, ROW5, ROW6, ROW7};
//If a given column is to be "turned on" (in our case, pulled high
//through a resistor), hich bit in the shift registers
//does that correspond to?
int COL0 = 13;
int COL1 = 3;
int COL2 = 4;
int COL3 = 10;
int COL4 = 6;
int COL5 = 11;
int COL6 = 15;
int COL7 = 16;
const int NUMCOLS = 8;
int cols[] = {COL0, COL1, COL2, COL3, COL4, COL5, COL6, COL7};
//If all of the rows and columns were off, what would the bits in the shift
//registers look like? We could build this data from the row/column data
//above; I have built it here manually
int allOff = (B11001011 * 256) + B10010100;
//The 2D array that will hold our board data. Starts as all 0's.
int board[NUMROWS][NUMCOLS] = { {1,1,0,0,0,0,0,0},
{0,0,0,0,1,1,0,1},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0}};
//A 1D Array for example:
//int numbers[] = {0, 1, 2, 3, 4, 5, 6 ,7, 8, 9};
long displayUpdatePeriod = 500; //Microseconds between updating lines of display
long lastDisplayUpdateTime = 0; //Last time (from micros()) that we updated the display
//We'll step through the display multiplexing row-by-row - this is the row we're currently on
int currentRow = 0;
void setup() {
//Configure out shift-register outputs
pinMode(CLK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
}
void loop() {
//Check input - have any buttons been pressed? Update variables to account for what buttons have been pressed.
//check gamestate - has enough time passed for us to do a "tick" of gamestate? If so update the appropriate variables
// and the board display
//check displayUpdate - has enough time passed to need to update the display? If so, take the appropriate actions
// to update the state of the board
checkInput();
checkGamestate();
displayUpdate();
}
void checkInput(){
}
void checkGamestate(){
}
void displayUpdate(){
//Check whether we need to update the display
if (micros() > lastDisplayUpdateTime + displayUpdatePeriod){
currentRow = (currentRow + 1) % NUMROWS; //Advance to the next row
displayRow(currentRow); //Display the current row
lastDisplayUpdateTime = micros();
}
}
void displayRow(int rowNum){
//Start with everything turned off
int value = allOff;
//Turn on the current row (in our case, by sending it low/0, so we need to clear a bit)
bitClear(value, 15-(rows[rowNum] - 1));
//For each dot in this row, if the associated row-value is non-zero,
//Turn the associated column-bit on(in our case, by sending it high/1, so we used bitSet)
for (int i = 0; i < NUMCOLS; i++){
if (board[rowNum][i] > 0){
bitSet(value, 15-(cols[i] - 1));
}
}
//output the generated data to the shiftRegisters
sendIntToDisplay(value);
}
void sendIntToDisplay(int input){
//Make sure clock and latch are low before we start, since they
//Trigger on a rising-edge
digitalWrite(CLK_PIN, LOW);
digitalWrite(LATCH_PIN, LOW);
//Clock out the data
shiftOut(DATA_PIN, CLK_PIN, LSBFIRST, lowByte(input));
shiftOut(DATA_PIN, CLK_PIN, LSBFIRST, highByte(input));
//Latch the data in
digitalWrite(LATCH_PIN, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment