實作影片
電路圖
#include "pitches.h"
#include <SimpleTimer.h>
SimpleTimer ABC;
#include <Adafruit_NeoPixel.h>
#define PIN D4
#define sp D2
#define NUMPIXELS 12
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "qYS-UhRWkSSMqbG7fLje5yDyiKtmvMyo";
char ssid[] = "LJH";
char pass[] = "1234567890";
int LedMode = 0;
int red = 50, green = 50, blue = 50;
int melody_TwoTiger[] = {
_C6, _D6, _E6, _C6, _C6, _D6, _E6, _C6, _E6, _F6, _G6, _E6, _F6, _G6, _G6, _A6, _G6, _F6, _E6, _C6, _G6, _A6, _G6, _F6, _E6, _C6, _D6, _G5, _C6, 0, _D6, _G5, _C6, 0
};
byte noteDurations_TwoTiger[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1
};
void PlayTwoTiger() {
if (LedMode == 0)PlayMusicV1(melody_TwoTiger,
noteDurations_TwoTiger,
sizeof(noteDurations_TwoTiger),
1);
}
int melody_Star[] = {
_C6, _C6, _G6, _G6, _A6, _A6, _G6, 0, _F6, _F6, _E6, _E6, _D6, _D6, _C6, 0, _G6, _G6, _F6, _F6, _E6, _E6, _D6, 0, _G6, _G6, _F6, _F6, _E6, _E6, _D6, 0, _C6, _C6, _G6, _G6, _A6, _A6, _G6, 0, _F6, _F6, _E6, _E6, _D6, _D6, _C6, 0
};
byte noteDurations_Star[] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
void PlayStar() {
if (LedMode == 1)PlayMusicV1(melody_Star,
noteDurations_Star,
sizeof(noteDurations_Star),
1);
}
int melody_MyDonky[] = {
_C6, _C6, _C6, _E6, _G6, _G6, _G6, _G6, _A6, _A6, _A6, _C7, _G6, 0, _F6, _F6, _A6, _E6, _E6, _E6, _E6, _D6, _D6, _D6, _D6, _G6, _G6, _C6, _C6, _C6, _E6, _G6, _G6, _G6 , _G6, _A6, _A6, _A6, _C7, _G6, 0, _F6, _F6, _F6, _A6, _E6, _E6, _E6, _E6, _E6, _E6, _D6, _D6, _D6, _E6, _C6, 0
};
byte noteDurations_MyDonky[] = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 4
};
void PlayMyDonky() {
if (LedMode == 2)PlayMusicV1(melody_MyDonky,
noteDurations_MyDonky,
sizeof(noteDurations_MyDonky),
1);
}
int melody_MyBaBy[] = {
_C6, _C6, _D6, _E6, _E6, _D6, _C6, _D6, _C6, 0, _E6, _E6, _F6, _G6, _G6, _G6, _F6, _E6, _F6, _E6, 0, _G6, 0, _C6, 0, _G6, 0, _C6, 0, _E6, 0, _D6, 0, _C6 , 0, 0, 0
};
byte noteDurations_MyBaBy[] = {
2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
void PlayMyBaBy() {
if (LedMode == 3)PlayMusicV1(melody_MyBaBy,
noteDurations_MyBaBy,
sizeof(noteDurations_MyBaBy),
1);
}
void LeftRotate() {
int static count = 0;
count = count + 1;
if (count > 12)count = 0;
Serial.print("LeftRotate=");
Serial.println(count);
AllLedColor(0, 0, 0);
LedFromToColor(count, 1, red, green, blue);
}
void RightRotate() {
int static count = 12;
count = count - 1;
if (count < 1)count = 12;
Serial.print("RightRotate=");
Serial.println(count);
AllLedColor(0, 0, 0);
LedFromToColor(count, 1, red, green, blue);
}
void Pili() {
int static count = 0;
count = count + 1;
if (count > 12)count = 0;
Serial.print("Pili=");
Serial.println(count);
AllLedColor(0, 0, 0);
LedFromToColor(count, 1, red, green, blue);
LedFromToColor(NUMPIXELS - 2 - count, 1, red, green, blue);
}
void Custom() {
int static count = 12;
count = count - 1;
if (count < 1)count = 12;
Serial.print("Custom=");
Serial.println(count);
AllLedColor(0, 0, 0);
LedFromToColor(count, 12, red, green, blue);
}
void AlllightON() {
int static count = 12;
Serial.print("AlllightON=");
Serial.println(count);
AllLedColor(255, 255, 255);
LedFromToColor(count, 12, 255, 255, 255);
}
void AlllightOFF() {
int static count = 12;
Serial.print("AlllightOFF=");
Serial.println(count);
AllLedColor(0, 0, 0);
LedFromToColor(count, 12, 0, 0, 0);
}
void WS2812Control()
{
switch (LedMode)
{ case 0: LeftRotate(); break;
case 1: RightRotate(); break;
case 2: Pili(); break;
case 3: Custom(); break;
case 4: AlllightON(); break;
case 5: AlllightOFF(); break;
}
}
void setup()
{
strip.begin();
Serial.begin(9600);
ABC.setInterval(100, WS2812Control);
Blynk.begin(auth, ssid, pass);
ABC.setInterval(200, PlayTwoTiger);
ABC.setInterval(150, PlayStar);
ABC.setInterval(50, PlayMyDonky);
ABC.setInterval(150, PlayMyBaBy);
}
void loop()
{
Blynk.run();
ABC.run();
}
BLYNK_WRITE(V1) {
int value = param.asInt();
Serial.printf("LEFT V1=%d\n", value);
LedMode = 0;
}
BLYNK_WRITE(V2) {
int value = param.asInt();
Serial.printf("RIGHT V2=%d\n", value);
LedMode = 1;
}
BLYNK_WRITE(V3) {
int value = param.asInt();
Serial.printf("PILI V3=%d\n", value);
LedMode = 2;
}
BLYNK_WRITE(V4) {
int value = param.asInt();
Serial.printf("CUSTOM V4=%d\n", value);
LedMode = 3;
}
BLYNK_WRITE(V5) {
int value = param.asInt();
Serial.printf("RED V5=%d\n", value);
red = value;
}
BLYNK_WRITE(V6) {
int value = param.asInt();
Serial.printf("GREEN V6=%d\n", value);
green = value;
}
BLYNK_WRITE(V7) {
int value = param.asInt();
Serial.printf("BLUE V7=%d\n", value);
blue = value;
}
BLYNK_WRITE(V8) {
int value = param.asInt();
Serial.printf("BLUE V8=%d\n", value);
LedMode = 4 ;
}
BLYNK_WRITE(V9) {
int value = param.asInt();
Serial.printf("BLUE V9=%d\n", value);
LedMode = 5 ;
}
void AllLedColor(byte red, byte green, byte blue) {
LedFromToColor(0, NUMPIXELS, red, green, blue);
}
void LedFromToColor(byte from, byte len, byte red, byte green, byte blue) {
for (int i = from; i < from + len; i++) {
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
}
void PlayMusicV1(int melody[], byte duration[], int melodyLen, float freMul) {
int static count = 0;
Serial.print("melodyLen count=");
Serial.print(count);
tone(sp, melody[count]*freMul);
byte static NextCount = 0;
if (NextCount < duration[count]) {
NextCount++;
}
else {
count = count + 1;
NextCount = 0;
}
if (count > melodyLen) count = 0;
}