Instantly share code, notes, and snippets.

Embed
What would you like to do?
Randomly turn pixels on/off on 3 8x8 LED displays
/**
* Randomly turn pixels on/off on 3 8x8 LED displays
* @author Mike Almond - @mikedotalmond | github.com/mikedotalmond
*/
#include "LedControl.h"
const int LED_DATA = 2;
const int LED_LOAD = 3; // CS
const int LED_CLK = 4;
const int LED_DISPLAY_COUNT = 3;
LedControl lc = LedControl(LED_DATA, LED_CLK, LED_LOAD, LED_DISPLAY_COUNT);
const byte LED_MAX_INTENSITY[LED_DISPLAY_COUNT] = {4,15,4};
const byte LED_MIN_INTENSITY[LED_DISPLAY_COUNT] = {1,11,1};
byte lastFrame[LED_DISPLAY_COUNT][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
void setup() {
randomSeed(analogRead(A0));
for(byte j=0;j<LED_DISPLAY_COUNT;j++){
lc.shutdown(j, false); /* Wake up - the MAX72XX is in power-saving mode on startup */
lc.clearDisplay(j);
lc.setIntensity(j, LED_MIN_INTENSITY[j]); // address, intensity (0-15)
}
}
void loop() {
randomiseDisplay(0, 10, 36);
randomiseDisplay(1, 8, 16);
randomiseDisplay(2, 10, 36);
}
void randomiseDisplay(byte displayIndex, byte fillRate, byte clearRate){
fillRate = 255 - fillRate;
clearRate = 255 - clearRate;
// each row
for(byte i=0;i<8;i++){
byte previous = lastFrame[displayIndex][i];
byte rOn = 0;
byte rOff = 0;
// each pixel
for(byte j=0;j<8;j++){
if((byte)random(0,255) > clearRate){
rOff |= ((byte)random(0,2) << j);
}
if((byte)random(0,255) > fillRate){
rOn |= ((byte)random(0,2) << j);
}
}
// new state for this row
byte r = rOn | (previous & ~rOff);
//
lc.setRow(displayIndex, i, r);
lastFrame[displayIndex][i] = r;
}
// brightness change
if(random(100)>96) {
lc.setIntensity(displayIndex, random(LED_MIN_INTENSITY[displayIndex],LED_MAX_INTENSITY[displayIndex]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment