成功点亮!
This commit is contained in:
78
8By8/8By8.ino
Normal file
78
8By8/8By8.ino
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include "TimerOne.h"
|
||||||
|
|
||||||
|
#define TIME_PER_LAYER_IN_US 700
|
||||||
|
#define BUS_START_PIN 22
|
||||||
|
#define CLOCK_START_PIN 30
|
||||||
|
#define SW_START_PIN 38
|
||||||
|
|
||||||
|
class Cube {
|
||||||
|
private:
|
||||||
|
static int layer_count;
|
||||||
|
static int brightness_count;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Every int represents a row of 8 LED status.
|
||||||
|
static int LED_status[8][8];
|
||||||
|
|
||||||
|
static void display() {
|
||||||
|
// Serial.println("Here");
|
||||||
|
if (brightness_count >= 2) {
|
||||||
|
digitalWrite(SW_START_PIN + layer_count, LOW);
|
||||||
|
layer_count = (layer_count + 1) % 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
brightness_count = (brightness_count + 1) % 3;
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
for (int j = 0; j < 8; j++) {
|
||||||
|
// In LED_status:
|
||||||
|
// 0 = off
|
||||||
|
// 4 = brightest
|
||||||
|
digitalWrite(BUS_START_PIN + j, ((LED_status[layer_count][i] >> (j * 2)) & 3) >= (3 - brightness_count));
|
||||||
|
}
|
||||||
|
digitalWrite(CLOCK_START_PIN + i, HIGH);
|
||||||
|
digitalWrite(CLOCK_START_PIN + i, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
digitalWrite(SW_START_PIN + layer_count, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set_status(int x, int y, int z, int brightness) {
|
||||||
|
x %= 8;
|
||||||
|
y %= 8;
|
||||||
|
z %= 8;
|
||||||
|
brightness %= 4;
|
||||||
|
LED_status[z][x] = (LED_status[z][x] & (~(3 << (y * 2)))) | (brightness << (y * 2));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int Cube::layer_count = 0;
|
||||||
|
int Cube::brightness_count = 0;
|
||||||
|
int Cube::LED_status[8][8] = {0};
|
||||||
|
int bright = 3;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
for (int i = 22; i < 46; i++) {
|
||||||
|
pinMode(i, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer1.initialize();
|
||||||
|
Timer1.setPeriod(TIME_PER_LAYER_IN_US);
|
||||||
|
Timer1.attachInterrupt(Cube::display);
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
for (int i = 7; i >= 0; i--) {
|
||||||
|
for (int j = 0; j < 8; j++) {
|
||||||
|
for (int k = 0; k < 8; k++) {
|
||||||
|
Cube::set_status(i, j, k, bright);
|
||||||
|
delay(5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bright = (bright + 1) % 4;
|
||||||
|
}
|
||||||
107
Test/Test.ino
107
Test/Test.ino
@@ -1,85 +1,50 @@
|
|||||||
#include "TimerOne.h"
|
byte status = 0;
|
||||||
|
byte layer = 0;
|
||||||
const byte cath_low = 3, cath_high = 2;
|
|
||||||
const byte anode_start = 4;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(cath_low, OUTPUT);
|
Serial.begin(115200);
|
||||||
pinMode(cath_high, OUTPUT);
|
for (int i = 0; i < 8; i++) {
|
||||||
for (int i = 0; i < 4; i++) {
|
pinMode(22 + i, OUTPUT);
|
||||||
pinMode(anode_start + i, OUTPUT);
|
}
|
||||||
|
// pinMode(30, OUTPUT);
|
||||||
|
// pinMode(31, OUTPUT);
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
pinMode(30 + i, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.begin(115200);
|
for (int i = 0; i < 8; i++) {
|
||||||
|
pinMode(38 + i, OUTPUT);
|
||||||
Timer1.initialize();
|
}
|
||||||
Timer1.setPeriod(1000);
|
|
||||||
Timer1.attachInterrupt(displayOneLayer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte LED_status = 0;
|
int column = 0;
|
||||||
byte currentDisplayLayer = 0;
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// if (Serial.available()) {
|
if (Serial.available()) {
|
||||||
// char input = Serial.read();
|
int num = Serial.read();
|
||||||
// if (input >= '0' && input < '9') {
|
if (num >= '0' && num <= '7') {
|
||||||
// int LED_num = input - '0';
|
num = num - '0';
|
||||||
// LED_status ^= 1 << LED_num;
|
status ^= 1 << num;
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// for (int i = 0; i < 8; i++) {
|
|
||||||
// LED_status ^= 1 << i;
|
|
||||||
// delay(75);
|
|
||||||
// }
|
|
||||||
|
|
||||||
int count = 0;
|
for (int i = 0; i < 8; i++) {
|
||||||
while (true) {
|
digitalWrite(22 + i, status >> i & 1);
|
||||||
randomSeed(analogRead(0));
|
}
|
||||||
int num = random(0, 8);
|
|
||||||
if (!(LED_status & (1 << num))) {
|
|
||||||
count++;
|
|
||||||
LED_status ^= 1 << num;
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
if (count >= 8) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(500);
|
Serial.println(status, BIN);
|
||||||
|
} else if (num >= 'a' && num <= 'h') {
|
||||||
while (true) {
|
num = num - 'a';
|
||||||
randomSeed(analogRead(0));
|
column = num;
|
||||||
int num = random(0, 8);
|
Serial.print("LOAD");
|
||||||
if (LED_status & (1 << num)) {
|
Serial.print(num);
|
||||||
count--;
|
Serial.println(status, BIN);
|
||||||
LED_status ^= 1 << num;
|
digitalWrite(30 + column, HIGH);
|
||||||
delay(100);
|
digitalWrite(30 + column, LOW);
|
||||||
}
|
}
|
||||||
|
else if (num == 'z') {
|
||||||
if (count <= 0) {
|
digitalWrite(38 + layer, LOW);
|
||||||
break;
|
layer = (layer + 1) % 8;
|
||||||
|
digitalWrite(38 + layer, HIGH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
void displayOneLayer() {
|
|
||||||
// Serial.println(LED_status, BIN);
|
|
||||||
currentDisplayLayer = (currentDisplayLayer + 1) % 2;
|
|
||||||
if (currentDisplayLayer == 0) {
|
|
||||||
digitalWrite(cath_high, LOW);
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
digitalWrite(anode_start + i, (LED_status >> i) & 1);
|
|
||||||
}
|
|
||||||
digitalWrite(cath_low, HIGH);
|
|
||||||
} else {
|
|
||||||
digitalWrite(cath_low, LOW);
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
digitalWrite(anode_start + i, (LED_status >> (i + 4)) & 1);
|
|
||||||
}
|
|
||||||
digitalWrite(cath_high, HIGH);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user