Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LedControl.h>
- #define CYCLE_TIME 100
- #define LENGTH 4
- #define GROW_STEP 5
- struct pos {
- int row;
- int col;
- }typedef Led_pos;
- LedControl lc = LedControl(4, 3, 5, 1);
- struct pos[LENGTH];
- void setup() {
- /*
- The MAX72XX is in power-saving mode on startup,
- we have to do a wakeup call
- */
- lc.shutdown(0,false);
- /* Set the brightness to a medium values */
- lc.setIntensity(0, 15);
- /* and clear the display */
- lc.clearDisplay(0);
- }
- void loop() {
- start();
- }
- void start() {
- Led_pos snake[LENGTH];
- int i,j;
- int len = 8;
- int count = 1;
- int dir = 1;
- int row = 0;
- int col = -1;
- int currLength = 0;
- int chuj = -1;
- Led_pos last;
- last.col= -LENGTH-1;
- last.row = 0;
- for(i = 0; i<LENGTH; i++){
- snake[i].col = -1-i;
- snake[i].row = 0;
- }
- for(i = 0; i < 128; i++) {
- lc.setLed(0, last.row, last.col, false);
- switch(dir) {
- case 1:
- col++;
- break;
- case 2:
- row++;
- break;
- case 3:
- col--;
- break;
- case 4:
- row--;
- break;
- }
- lc.setLed(0, row, col, true);
- last = snake[LENGTH-1];
- for(j=LENGTH-1; j>=1;j--){
- snake[j] = snake[j-1];
- }
- snake[0].row = row;
- snake[0].col = col;
- delay(CYCLE_TIME);
- currLength++;
- if(currLength == len) {
- currLength = 0;
- count++;
- dir = dir % 4 + 1;
- if(count == 2) {
- len += chuj;
- count = 0;
- }
- }
- if(len == 0) {
- chuj = -chuj;
- len = 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement