8×8×8led立体点阵实现三维动态效果数字的代码
时间: 2023-10-11 14:06:41 浏览: 114
以下是一个基于Arduino平台的8x8x8 RGB LED立方体的代码,可以实现三维动态效果数字的显示:
```C++
#include <FastLED.h>
#define LED_PIN 6
#define LED_COUNT 512
CRGB leds[LED_COUNT];
int cube[8][8][8]; // 8x8x8 cube array
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT);
FastLED.setBrightness(50);
Serial.begin(9600);
initCube(); // initialize cube with 0
}
void loop() {
int num = 123; // number to display
clearCube(); // clear the cube
drawNumber(num); // draw the number on the cube
showCube(); // show the cube
delay(500);
}
// initialize the cube with 0
void initCube() {
for(int z = 0; z < 8; z++) {
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 8; x++) {
cube[x][y][z] = 0;
}
}
}
}
// clear the cube
void clearCube() {
for(int i = 0; i < LED_COUNT; i++) {
leds[i] = CRGB::Black;
}
}
// draw a number on the cube
void drawNumber(int num) {
int digits[3];
digits[0] = num % 10; // ones digit
digits[1] = (num / 10) % 10; // tens digit
digits[2] = (num / 100) % 10; // hundreds digit
for(int i = 0; i < 3; i++) {
drawDigit(digits[i], i * 3, 0, 0); // draw each digit on the cube
}
}
// draw a digit on the cube
void drawDigit(int digit, int x, int y, int z) {
int segments[7][3];
switch(digit) {
case 0:
segments[0][0] = x+1; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y+1; segments[1][2] = z;
segments[2][0] = x; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x; segments[3][1] = y+1; segments[3][2] = z+1;
segments[4][0] = x; segments[4][1] = y; segments[4][2] = z+1;
segments[5][0] = x+1; segments[5][1] = y; segments[5][2] = z+1;
break;
case 1:
segments[0][0] = x+1; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y+1; segments[1][2] = z;
break;
case 2:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x; segments[3][1] = y+1; segments[3][2] = z;
segments[4][0] = x; segments[4][1] = y+1; segments[4][2] = z+1;
segments[5][0] = x+1; segments[5][1] = y+1; segments[5][2] = z+1;
break;
case 3:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y+1; segments[3][2] = z+1;
segments[4][0] = x; segments[4][1] = y+1; segments[4][2] = z+1;
segments[5][0] = x; segments[5][1] = y+1; segments[5][2] = z;
break;
case 4:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x; segments[1][1] = y+1; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y+1; segments[3][2] = z+1;
break;
case 5:
segments[0][0] = x; segments[0][1] = y+1; segments[0][2] = z;
segments[1][0] = x; segments[1][1] = y; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y+1; segments[3][2] = z;
segments[4][0] = x+1; segments[4][1] = y+1; segments[4][2] = z+1;
segments[5][0] = x; segments[5][1] = y+1; segments[5][2] = z+1;
break;
case 6:
segments[0][0] = x; segments[0][1] = y+1; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y+1; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y; segments[3][2] = z+1;
segments[4][0] = x; segments[4][1] = y; segments[4][2] = z+1;
segments[5][0] = x; segments[5][1] = y+1; segments[5][2] = z+1;
break;
case 7:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x+1; segments[1][1] = y; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
break;
case 8:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x; segments[1][1] = y+1; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y+1; segments[3][2] = z+1;
segments[4][0] = x+1; segments[4][1] = y; segments[4][2] = z+1;
segments[5][0] = x; segments[5][1] = y; segments[5][2] = z+1;
segments[6][0] = x; segments[6][1] = y+1; segments[6][2] = z+1;
break;
case 9:
segments[0][0] = x; segments[0][1] = y; segments[0][2] = z;
segments[1][0] = x; segments[1][1] = y+1; segments[1][2] = z;
segments[2][0] = x+1; segments[2][1] = y+1; segments[2][2] = z;
segments[3][0] = x+1; segments[3][1] = y+1; segments[3][2] = z+1;
segments[4][0] = x+1; segments[4][1] = y; segments[4][2] = z+1;
segments[5][0] = x; segments[5][1] = y; segments[5][2] = z+1;
break;
}
// set the cube array values for the segments of the digit
for(int i = 0; i < 7; i++) {
cube[segments[i][0]][segments[i][1]][segments[i][2]] = 1;
}
}
// show the cube on the LED matrix
void showCube() {
int index = 0;
for(int z = 0; z < 8; z++) {
for(int y = 0; y < 8; y++) {
for(int x = 0; x < 8; x++) {
if(cube[x][y][z] == 1) {
leds[index] = CRGB::White;
}
else {
leds[index] = CRGB::Black;
}
index++;
}
}
}
FastLED.show();
}
```
该代码使用FastLED库控制LED,初始化8x8x8的cube数组,并定义了drawDigit()函数,在其中设置了每个数字的7个线段的坐标,并将这些坐标在cube数组中标记为1。showCube()函数则将cube数组中的值映射到LED上,显示数字。在主循环中,可以通过调用drawNumber()函数,将数字显示在立方体上。
阅读全文