#include<SPI.h>
//LED 灯珠数量
#define LED_NUMBER 60
//模拟0码 1111 1100
uint8_t CODE0 = 0xFC;
//模拟1码 1100 0000
uint8_t CODE1 = 0XC0;
void setup() {
Serial.begin(115200);
SPI.begin();
//设置大小端
SPI.setBitOrder(LSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);
pinMode(D2, INPUT_PULLUP);
for (int i = 0; i < LED_NUMBER; i++) {
black();
}
}
void whiteAll() {
for (int i = 0; i < LED_NUMBER; i++) {
white();
}
}
void blackAll() {
for (int i = 0; i < LED_NUMBER; i++) {
black();
}
}
void white() {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 8; k++) {
SPI.transfer(CODE0);
}
}
}
void black() {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 8; k++) {
SPI.transfer(CODE1);
}
}
}
int o = 0;
int colorBuffer[LED_NUMBER] = {0x329E00, 0x47A700, 0x5BAF00, 0x70B700, 0x84C000, 0x99C800, 0xADD100, 0xD6E100, 0xEBEA00, 0xFFF200, 0xFFDC00, 0xFFC600, 0xFFB000, 0xFF9A00, 0xFF8400, 0xFF6E00, 0xFF5800, 0xFF4200, 0xFF2C00, 0xFF1600};
void drawMarquee() {
o = o % LED_NUMBER;
for (int i = 0; i < LED_NUMBER; i++) {
setColor(colorBuffer[(i + o) % 20]);
}
o++;
delay(50);
}
void voiceDance() {
int audio = analogRead(A0);
int result = audio - 43;
//Serial.println(audio);
//b r g
if (result >= 2) {
for (int i = 0; i < 2; i++) {
setColor(0x1E9600);
}
}
if (result >= 5) {
for (int i = 0; i < 2; i++) {
setColor(0x5EB000);
}
}
if (result >= 13) {
for (int i = 0; i < 6; i++) {
setColor(0xAED100);
}
}
if (result >= 20) {
for (int i = 0; i < 10; i++) {
setColor(0xFFF200);
}
}
if (result >= 30) {
for (int i = 0; i < 10; i++) {
setColor(0xFFAA00);
}
}
if (result >= 120) {
for (int i = 0; i < 10; i++) {
setColor(0xFF0000);
}
}
if (result >= 45) {
for (int i = 0; i < 10; i++) {
setColor(0xFF4900);
}
}
blackAll();
//delay(1);
}
void setColor(int color) {
int r = (color & 0xff0000) >> 8;
int g = (color & 0xff00) << 8;
int b = color & 0xff;
int grb = g + r + b;
//从高到低
for (int k = 0; k < 24; k++) {
if (grb >> 23 & 0x1 == 1) {
//Serial.println(1);
SPI.transfer(CODE0);
} else {
SPI.transfer(CODE1);
//Serial.println(0);
}
grb <<= 1;
}
return;
}
void orange() {
for (int i = 0; i < LED_NUMBER; i++) {
//setColor(0xFFFF37);
setColor(0xFFDC35);
//setColor(0xFFFF6F);
//setColor(0xFFE153);
//setColor(0xFFFF93);
//setColor(0xFFE66F);
//setColor(0xFFFFAA);
//setColor(0xFFED97);
//setColor(0xFFFFB9);
//setColor(0xFFF0AC);
}
delay(100);
}
uint8_t stat = 0;
void loop() {
int buttonState = digitalRead(D2);
if (buttonState == LOW) {
delay(25);
if (buttonState == LOW) {
stat++;
stat = stat % 4;
delay(250);
}
}
switch (stat) {
case 0:
whiteAll();
break;
case 1:
orange();
break;
case 2:
voiceDance();
break;
case 3:
drawMarquee();
break;
default:
whiteAll();
}
}
07-09
3673
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-09
8347
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-27
1997
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)