// Count elements in array
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
/*
Matrix Effect Demo
November 19, 2017
Copyright (C) 2017 Jesse Campbell
All rights reserved.
A demo showing a Matrix movie effect
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
#include <Arduboy2.h>
// make an instance of arduboy used for many functions
Arduboy2 arduboy;
char m[16][8]; // 16 chars across, 8 chars high
int ySpeeds[16];
// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
// initiate arduboy instance
arduboy.begin();
arduboy.setFrameRate(60);
randomSeed(analogRead(3));
// fill 2D array with random characters
for(int i=0; i<COUNT_OF(m); i++){
for(int j=0; j<COUNT_OF(m[i]); j++){
m[i][j] = random(255);
}
}
// randomize the speed that columns move
for(int i=0; i<COUNT_OF(ySpeeds); i++){
ySpeeds[i] = random(8,200);
}
}
void loop() {
// pause render until it's time for the next frame
if (!(arduboy.nextFrame()))
return;
// first we clear our screen to black
arduboy.clear();
for(int i=0; i<COUNT_OF(m); i++){
for(int j=0; j<COUNT_OF(m[i]); j++){
// move the cursor to the right position with good spacing between chars
// draw the char partially off the screen for a smooth effect
arduboy.setCursor(i*8, (j*8+64+millis()/ySpeeds[i])%(64+ySpeeds[i]%48)-8);
if(random(1000) > 985){
m[i][j] = random(255); // randomly change the character once in a while
}
arduboy.print(m[i][j]); // print character in 2D array
}
}
// then we finally we tell the arduboy to display what we just wrote to the display
arduboy.display();
}