92 lines
2.5 KiB
Plaintext
92 lines
2.5 KiB
Plaintext
#include <SPI.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Max72xxPanel.h>
|
|
|
|
int pinCS = 10; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
|
|
int numberOfHorizontalDisplays = 1;
|
|
int numberOfVerticalDisplays = 1;
|
|
|
|
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
|
|
|
|
const int pinRandom = A0;
|
|
|
|
const int wait = 100; // In milliseconds
|
|
const int length = 8;
|
|
|
|
int x[length], y[length];
|
|
int ptr, nextPtr;
|
|
|
|
void setup() {
|
|
matrix.setIntensity(4); // Set brightness between 0 and 15
|
|
|
|
// Adjust to your own needs
|
|
// matrix.setPosition(0, 0, 0); // The first display is at <0, 0>
|
|
// matrix.setPosition(1, 1, 0); // The second display is at <1, 0>
|
|
// matrix.setPosition(2, 2, 0); // The third display is at <2, 0>
|
|
// matrix.setPosition(3, 3, 0); // And the last display is at <3, 0>
|
|
// ...
|
|
// matrix.setRotation(0, 2); // The first display is position upside down
|
|
// matrix.setRotation(3, 2); // The same hold for the last display
|
|
|
|
// Reset all variables
|
|
for ( ptr = 0; ptr < length; ptr++ ) {
|
|
x[ptr] = numberOfHorizontalDisplays * 8 / 2;
|
|
y[ptr] = numberOfVerticalDisplays * 8 / 2;
|
|
}
|
|
nextPtr = 0;
|
|
|
|
randomSeed(analogRead(pinRandom)); // Initialize random generator
|
|
}
|
|
|
|
void loop() {
|
|
|
|
// Shift pointer to the next segment
|
|
ptr = nextPtr;
|
|
nextPtr = next(ptr);
|
|
|
|
matrix.drawPixel(x[ptr], y[ptr], HIGH); // Draw the head of the snake
|
|
matrix.write(); // Send bitmap to display
|
|
|
|
delay(wait);
|
|
|
|
if ( ! occupied(nextPtr) ) {
|
|
matrix.drawPixel(x[nextPtr], y[nextPtr], LOW); // Remove the tail of the snake
|
|
}
|
|
|
|
for ( int attempt = 0; attempt < 10; attempt++ ) {
|
|
|
|
// Jump at random one step up, down, left, or right
|
|
switch ( random(4) ) {
|
|
case 0: x[nextPtr] = constrain(x[ptr] + 1, 0, matrix.width() - 1); y[nextPtr] = y[ptr]; break;
|
|
case 1: x[nextPtr] = constrain(x[ptr] - 1, 0, matrix.width() - 1); y[nextPtr] = y[ptr]; break;
|
|
case 2: y[nextPtr] = constrain(y[ptr] + 1, 0, matrix.height() - 1); x[nextPtr] = x[ptr]; break;
|
|
case 3: y[nextPtr] = constrain(y[ptr] - 1, 0, matrix.height() - 1); x[nextPtr] = x[ptr]; break;
|
|
}
|
|
|
|
if ( ! occupied(nextPtr) ) {
|
|
break; // The spot is empty, break out the for loop
|
|
}
|
|
}
|
|
}
|
|
|
|
boolean occupied(int ptrA) {
|
|
for ( int ptrB = 0 ; ptrB < length; ptrB++ ) {
|
|
if ( ptrA != ptrB ) {
|
|
if ( equal(ptrA, ptrB) ) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int next(int ptr) {
|
|
return (ptr + 1) % length;
|
|
}
|
|
|
|
boolean equal(int ptrA, int ptrB) {
|
|
return x[ptrA] == x[ptrB] && y[ptrA] == y[ptrB];
|
|
}
|
|
|