Just_some_dude:
I was talking about the last code I posted. Dynamic patterns make it pretty hard to make out the ghosting effect, that's why I chose a static pattern as an example. It is not a big deal, but the fact it is still there bothers me.
Should I post my code again in a more readable way?
OK, so edited to a plausibly readable version and slightly adjusted, this was the last code you posted:
#include <SPI.h>
#define t 1 //on-time of the LEDs
#define latchPin 10
#define outputenable 3
//Array with hexadecimal values to turn on the LED according to the index, "hex[x][y]"
byte hex[5][5] = {
{0, 0, 0, 0, 0}, // So that the indexes can start with 1
{0x00, 0x01, 0x02, 0x04, 0x08}, // Row 1
{0x00, 0x10, 0x20, 0x40, 0x80}, // Row 2
{0x00, 0x01, 0x02, 0x04, 0x08}, // Row 3 = Row 1, avoiding if-statement in the "buffer_write" function
{0x00, 0x10, 0x20, 0x40, 0x80}, // Row 4 = Row 2
};
//Array to buffer the current pattern, again one bigger than needed for easier use of indexes
byte buffer_pattern[5][5][5];
//Function to store the LED to be turned on to the buffer
void buffer_write(byte x, byte y, byte z) {
buffer_pattern[x][y][z] = hex[x][y];
}
//Function to shift out the buffer. Each latch-cycle displays one level (8 LEDs for each register),
//meaning that the registers need to latch 4 times to display the whole cube one time
void buffer_shift() {
for (byte level = 1; level < 5; level++) { //z, iterating through all levels
byte cache_8 = 0; //needs to be reset with every iteration, storing final pattern for columns 1-8
byte cache_16 = 0; //columns 9-16
//Column 1-8: "creating" byte for the first register by adding the bytes of each LED to get the final pattern
for (byte row = 1; row < 3; row++) { //x, iterating through first 2 rows
for (byte column = 1; column < 5; column++) { //y, iterating through the 4 columns in each row
cache_8 = cache_8 | buffer_pattern[row][column][level];
}
}
//Column 8-16: Same procedure as before, now for the 2nd register
for (byte row = 3; row < 5; row++) { //x, iterating through last 2 rows
for (byte column = 1; column < 5; column++) { //y, iterating through the 4 columns in each row
cache_16 = cache_16 | buffer_pattern[row][column][level];
}
}
shift(cache_8, cache_16 , level); //shifting the pattern for the whole level, 4 times for the whole cube
delay(t); //delay, so that the LEDs are on for longer, makes them shine brighter
//delayMicroseconds(t);
}
}
//Clearing the buffer. Necessary because only 1s get filled into the buffer, but no 0s
//Meaning that existing 1s only get overwritten by another 1 if buffer does not get cleared
void buffer_clear() {
for (byte x = 1; x < 5; x++) {
for (byte y = 1; y < 5; y++) {
for (byte z = 1; z < 5; z++) {
buffer_pattern[x][y][z] = 0;
}
}
}
}
//Shifting the pattern from function "buffer_shift"
void shift(byte cache_8, byte cache_16, byte level) {
digitalWrite(outputenable, HIGH);
digitalWrite(latchPin, LOW);
SPI.transfer(hex[1][level]);
SPI.transfer(cache_16);
SPI.transfer(cache_8);
digitalWrite(latchPin, HIGH);
digitalWrite(outputenable, LOW);
}
void setup() {
pinMode(outputenable, OUTPUT);
digitalWrite(outputenable, HIGH); //Disabling output, preventing whole cube from lighting up on startup
SPI.begin();
SPI.beginTransaction(SPISettings(800000, MSBFIRST, SPI_MODE0));
pinMode(latchPin, OUTPUT);
randomSeed(analogRead(0));
//Clearing registers to be sure everything is off
buffer_clear();
buffer_shift(); //Clearing registers
digitalWrite(outputenable, LOW); //Enabling output, startup complete
}
void loop() {
//cube_outline();
//moving_plane();
//bouncing_plane();
//raindrops();
//diagonal line
for (byte i = 1; i < 5; i++) {
buffer_write(i, i, i);
}
buffer_shift();
buffer_clear();
}
(Right, so I actually checked it in the IDE!)
So I haven't quite figured it all out, but explain to me, this is a static pattern, what exactly is this "ghosting" effect? Have a picture?