This commit is contained in:
unlockable
2023-08-15 15:57:25 +08:00
parent 5942b7fa1f
commit c1f70a8b87

67
4By4/4By4.ino Normal file
View File

@@ -0,0 +1,67 @@
#include "TimerOne.h"
#define TIME_PER_LAYER_IN_US 750
#define ANODE_START_PIN 22
#define CATHODE_START_PIN 8
int LED_status[4] = {0}; // 16 bit * 4
byte current_layer = 0;
void setup() {
Serial.begin(115200);
Timer1.initialize();
Timer1.setPeriod(TIME_PER_LAYER_IN_US);
Timer1.attachInterrupt(drawOneLayer);
for (int i = 0; i < 16; i++) {
pinMode(ANODE_START_PIN + i, OUTPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(CATHODE_START_PIN + i, OUTPUT);
}
randomSeed(analogRead(0));
}
void loop() {
// for (int i = 0; i < 4; i++) {
// for (int j = 0; j < 16; j++) {
// LED_status[i] ^= 1 << j;
// delay(500);
// }
// }
int count = 0;
while (count < 64) {
int num = random(64);
if ((LED_status[num / 16] >> (num % 16) ) & 1) {
continue;
}
LED_status[num / 16] ^= 1 << (num % 16);
delay(50);
count++;
}
delay(1000);
while (count > 0) {
int num = random(64);
if ((LED_status[num / 16] >> (num % 16) ) & 1) {
LED_status[num / 16] ^= 1 << (num % 16);
delay(50);
count--;
}
}
delay(1000);
}
void drawOneLayer() {
digitalWrite(CATHODE_START_PIN + current_layer, LOW);
current_layer = (current_layer + 1) % 4;
for (int j = 0; j < 16; j++) {
digitalWrite(ANODE_START_PIN + j, (LED_status[current_layer] >> j) & 1);
}
digitalWrite(CATHODE_START_PIN + current_layer, HIGH);
}