Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //We always have to include the library
- #include "LedControl.h"
- /*
- Now we need a LedControl to work with.
- PD0 is connected to the DataIn
- PD1 is connected to the CS
- PD2 is connected to CLK
- ***** Please set the number of devices you have *****
- But the maximum default of 8 MAX72XX wil also work.
- */
- LedControl lc=LedControl(23,25,24,4);
- // set pin numbers:
- const int buttonPin = PUSH2; // the number of the pushbutton pin
- int buttonState = 0; // variable for reading the pushbutton status
- int lastButtonState = 0; // previous reading from the input pin
- int devices=lc.getDeviceCount();
- int row=1, col=1, dir=0, gameOver=0; // dir 0 = left 1 = right
- /* we always wait a bit between updates of the display */
- unsigned long delaytime=192;
- // Array to keep track of LED state -- mray
- unsigned char led_state[8] = {0,0,1,1,1,1,0,0};
- unsigned char prev_state[8] = {1,1,1,1,1,1,1,1};
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 15; // the debounce time; increase if the output flickers
- // Function to change LED state and store to array -- mray
- void setOn(int row, int col);
- void setOff(int row, int col);
- // Controls for the Bar
- void drawRow(int row, unsigned char *led_state);
- void shiftRow(int row, int dir, unsigned char *led_state);
- void clearScreen();
- void setup() {
- pinMode(buttonPin, INPUT_PULLUP);
- //we have already set the number of devices when we created the LedControl
- int devices=lc.getDeviceCount();
- //we have to init all devices in a loop
- for(int address=0;address<devices;address++) {
- /*The MAX72XX is in power-saving mode on startup*/
- lc.shutdown(address,false);
- /* Set the brightness to a medium values */
- lc.setIntensity(address,8);
- /* and clear the display */
- lc.clearDisplay(address);
- }
- bootScreen();
- clearScreen();
- }
- void loop() {
- // draw the row
- drawRow(row, led_state);
- delay(delaytime);
- // read the state of the switch into a local variable:
- int reading = digitalRead(buttonPin);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited
- // long enough since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only increment the row if the new button state is LOW
- if (buttonState == LOW) {
- // logic to increment row
- if (memcmp(led_state, prev_state, sizeof(led_state))){ // if new position does not
- drawRow(row, led_state); // equal old position
- delay(100); // flash the error
- drawRow(row, prev_state);
- delay(100);
- drawRow(row, led_state);
- delay(100);
- drawRow(row, prev_state);
- delay(100);
- drawRow(row, led_state);
- delay(100);
- drawRow(row, prev_state);
- delay(100);
- drawRow(row, led_state);
- delay(100);
- drawRow(row, prev_state);
- delay(100);
- }
- for(int iBS=0;iBS<8;iBS+=1){
- if(led_state[iBS]==1 && prev_state[iBS]==1){
- led_state[iBS]=1;
- } else {
- led_state[iBS]=0;
- }
- prev_state[iBS]=led_state[iBS];
- }
- drawRow(row, led_state); //clear hanging leds
- row=row+1;
- delaytime = delaytime - 4; //speed up
- }
- }
- }
- // save the reading. Next time through the loop,
- // it'll be the lastButtonState:
- lastButtonState = reading;
- shiftRow(row, dir, led_state);
- if (led_state[0] == 1) {
- dir=1;
- }
- if (led_state[7] == 1) {
- dir=0;
- }
- gameOver=0;
- for (int iGO=0;iGO<8;iGO+=1) {
- if (led_state[iGO]==0){
- gameOver+=1;
- }
- }
- if (row==33 || gameOver==8){
- gameOverScreen();
- row=1;
- delaytime=192;
- clearScreen();
- for(int i=0;i<8;i+=1){
- prev_state[i]=1;
- led_state[i]=1;
- }
- led_state[0] = 0;
- led_state[1] = 0;
- led_state[6] = 0;
- led_state[7] = 0;
- bootScreen();
- }
- }
- void drawRow(int row, unsigned char *led_state) {
- for(int col=0;col<8;col+=1) {
- if (led_state[col] == 0) {
- // setOn(row, col);
- // delay(10);
- setOff(row, col);
- } else if (led_state[col] == 1) {
- // setOff(row, col);
- // delay(10);
- setOn(row, col);
- }
- // delay(10);
- }
- }
- void shiftRow(int row, int dir, unsigned char *led_state) {
- if (dir==0) { // shift left
- led_state[0]=led_state[1];
- led_state[1]=led_state[2];
- led_state[2]=led_state[3];
- led_state[3]=led_state[4];
- led_state[4]=led_state[5];
- led_state[5]=led_state[6];
- led_state[6]=led_state[7];
- led_state[7]=0;
- } else { // shift right
- // led_state[8]=led_state[7];
- led_state[7]=led_state[6];
- led_state[6]=led_state[5];
- led_state[5]=led_state[4];
- led_state[4]=led_state[3];
- led_state[3]=led_state[2];
- led_state[2]=led_state[1];
- led_state[1]=led_state[0];
- led_state[0]=0;
- }
- }
- void clearScreen() {
- delay(1000);
- for(int ir=0;ir<33;ir+=1) {
- for (int ic=0;ic<8;ic+=1) {
- setOff (ir, ic);
- }
- }
- }
- void setOn(int row, int col) {
- int address = 0;
- if(row>24) {
- address = 3;
- }
- else if(row>16) {
- address = 2;
- }
- else if(row>8) {
- address = 1;
- }
- else {
- address = 0;
- }
- row -= 1;
- row = row % 8;
- switch(row) {
- case 0:
- row = 7;
- break;
- case 1:
- row = 6;
- break;
- case 2:
- row = 5;
- break;
- case 3:
- row = 4;
- break;
- case 4:
- row = 3;
- break;
- case 5:
- row = 2;
- break;
- case 6:
- row = 1;
- break;
- case 7:
- row = 0;
- break;
- }
- switch(col) {
- case 0:
- col = 7;
- break;
- case 1:
- col = 6;
- break;
- case 2:
- col = 5;
- break;
- case 3:
- col = 4;
- break;
- case 4:
- col = 3;
- break;
- case 5:
- col = 2;
- break;
- case 6:
- col = 1;
- break;
- case 7:
- col = 0;
- break;
- }
- lc.setLed(address,col,row,true);
- }
- void setOff(int row, int col) {
- int address = 0;
- if(row>24) {
- address = 3;
- }
- else if(row>16) {
- address = 2;
- }
- else if(row>8) {
- address = 1;
- }
- else {
- address = 0;
- }
- row -= 1;
- row = row % 8;
- switch(row) {
- case 0:
- row = 7;
- break;
- case 1:
- row = 6;
- break;
- case 2:
- row = 5;
- break;
- case 3:
- row = 4;
- break;
- case 4:
- row = 3;
- break;
- case 5:
- row = 2;
- break;
- case 6:
- row = 1;
- break;
- case 7:
- row = 0;
- break;
- }
- switch(col) {
- case 0:
- col = 7;
- break;
- case 1:
- col = 6;
- break;
- case 2:
- col = 5;
- break;
- case 3:
- col = 4;
- break;
- case 4:
- col = 3;
- break;
- case 5:
- col = 2;
- break;
- case 6:
- col = 1;
- break;
- case 7:
- col = 0;
- break;
- }
- lc.setLed(address,col,row,false);
- }
- void bootScreen() {
- clearScreen();
- unsigned char text[32][8]={
- {0,1,1,0,0,0,0,0} ,
- {1,0,0,0,0,0,0,0} ,
- {0,1,0,0,0,0,0,0} ,
- {0,0,1,0,0,0,0,0} ,
- {1,1,0,0,0,0,0,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,1,1,1,0,0,0,0} ,
- {0,0,1,0,0,0,0,0} ,
- {0,0,1,1,0,0,0,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,0,0,1,1,1,0,0} ,
- {0,0,1,0,0,1,0,0} ,
- {0,0,1,0,0,1,0,0} ,
- {0,0,0,1,1,1,0,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,0,0,0,1,1,0,0} ,
- {0,0,0,1,0,0,1,0} ,
- {0,0,0,1,0,0,0,0} ,
- {0,0,0,1,0,0,1,0} ,
- {0,0,0,0,1,1,0,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,0,0,0,1,0,0,0} ,
- {0,0,0,0,1,0,0,0} ,
- {0,0,0,0,1,0,1,0} ,
- {0,0,0,0,1,1,0,0} ,
- {0,0,0,0,1,1,0,0} ,
- {0,0,0,0,1,0,1,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,0,0,0,0,1,1,0} ,
- {0,0,0,0,0,1,0,1} ,
- {0,0,0,0,0,1,0,0} ,
- {0,0,0,0,0,1,0,0}
- };
- /* now display them one by one with a small delay */
- for (int i=0;i<32;i+=1){
- drawRow(32-i,text[i]);
- delay(50);
- }
- delay(250);
- clearScreen();
- }
- void gameOverScreen() {
- clearScreen();
- unsigned int scrollDelay=250;
- /* here is the data for the characters */
- unsigned char text1[32][8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,0,0,0,0,0,0,0} ,
- {0,1,1,1,1,1,1,0} ,
- {0,1,0,0,0,0,0,0} ,
- {0,1,1,1,1,0,0,0} ,
- {0,1,1,1,1,0,0,0} ,
- {0,1,0,0,0,0,0,0} ,
- {0,1,1,1,1,1,1,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,0,0,0,0,0,0,0} ,
- {0,1,1,1,1,1,0,0} ,
- {0,1,0,0,0,0,1,0} ,
- {0,1,0,0,0,0,1,0} ,
- {0,1,1,1,1,1,0,0} ,
- {0,1,0,0,0,0,1,0} ,
- {0,1,0,0,0,0,1,0} ,
- {0,0,0,0,0,0,0,0}
- };
- /* now display them one by one with a small delay */
- for (int i=0;i<32;i+=1){
- drawRow(32-i,text1[i]);
- delay(50);
- }
- delay(250);
- for (int i=0;i<32;i+=1){
- drawRow(32-i,text2[i]);
- delay(50);
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement