Last active
July 8, 2023 19:35
-
-
Save crazycoder1999/18499b57fd40afc4341b7175500a32ea to your computer and use it in GitHub Desktop.
ArduinoCollisionAnimation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino_LED_Matrix.h" | |
ArduinoLEDMatrix matrix; | |
uint8_t frame[8][12] = { | |
{ 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } | |
}; | |
uint8_t row = 0; | |
uint8_t col = 0; | |
uint8_t nextRow = 0; | |
uint8_t nextCol = 0; | |
const char SOUTH = 'S'; | |
const char NORTH = 'N'; | |
const char WEST = 'W'; | |
const char EAST = 'E'; | |
char direction; | |
bool turnOn = true; | |
uint8_t coloredCount; | |
void reset() { | |
coloredCount = 95; | |
row = 0; | |
col = 0; | |
direction = SOUTH; | |
} | |
void setup() { | |
matrix.begin(); | |
reset(); | |
} | |
bool hitSomething(uint8_t futureRow, uint8_t futureCol) { | |
uint8_t hitColor = turnOn?1:0; | |
if (frame[futureRow][futureCol] == hitColor) | |
return true; | |
if (futureRow < 0 || futureCol < 0 || futureRow >= 8 || futureCol >= 12 ) //outside map | |
return true; | |
return false; | |
} | |
void turnLeft() { | |
switch (direction) { | |
case NORTH: | |
direction = WEST; | |
break; | |
case WEST: | |
direction = SOUTH; | |
break; | |
case SOUTH: | |
direction = EAST; | |
break; | |
case EAST: | |
direction = NORTH; | |
break; | |
default: | |
break; | |
} | |
} | |
void calcNextMove() { | |
switch (direction) { | |
case NORTH: | |
nextRow = nextRow - 1; | |
break; | |
case WEST: | |
nextCol = nextCol - 1; | |
break; | |
case SOUTH: | |
nextRow = nextRow + 1; | |
break; | |
case EAST: | |
nextCol = nextCol + 1; | |
break; | |
default: | |
break; | |
} | |
} | |
void loop() { | |
nextRow = row; | |
nextCol = col; | |
calcNextMove(); | |
if (hitSomething(nextRow,nextCol)) { | |
turnLeft(); | |
} else { | |
row = nextRow; | |
col = nextCol; | |
coloredCount--; | |
delay(100); | |
} | |
frame[row][col] = turnOn?1:0; | |
matrix.renderBitmap(frame, 8, 12); | |
if(coloredCount==0) { | |
reset(); | |
turnOn = !turnOn; | |
frame[0][0] = turnOn?1:0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another little animation script