Codes (Arduino+ Processing)
Arduino:
With Arduino, I collect the status of the Reed Switch, by detecting the different statuses (HIGH/LOW) of the Reed Switch and different combinations, it corresponds to 7 different statuses in total. (Original, Dream 1,2,3,4, the overall dream, await)
*The original code was learned from last-minute Engineers
#include <FastLED.h>
const int reedSwitch1Pin = 2;
const int reedSwitch2Pin = 3;
const int reedSwitch3Pin = 7;
const int reedSwitch4Pin = 5;
#define NUM_LEDS 60
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
int reedSwitch1State = 0;
int reedSwitch2State = 0;
int reedSwitch3State = 0;
int reedSwitch4State = 0;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
pinMode(reedSwitch1Pin, INPUT_PULLUP);
pinMode(reedSwitch2Pin, INPUT_PULLUP);
pinMode(reedSwitch3Pin, INPUT_PULLUP);
pinMode(reedSwitch4Pin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// 读取Reed开关状态
reedSwitch1State = digitalRead(reedSwitch1Pin);
reedSwitch2State = digitalRead(reedSwitch2Pin);
reedSwitch3State = digitalRead(reedSwitch3Pin);
reedSwitch4State = digitalRead(reedSwitch4Pin);
int status = 0;
// 根据Reed开关状态设置status值
if (reedSwitch1State == LOW && reedSwitch2State == LOW && reedSwitch3State == LOW && reedSwitch4State == LOW) {
status = 1; // 状态1 门互动
allRed();
}
if (reedSwitch1State == HIGH && reedSwitch2State == HIGH && reedSwitch3State == LOW && reedSwitch4State == LOW) {
status = 2; // 状态2 蝴蝶互动
rainbowEffect();
}
if (reedSwitch1State == LOW && reedSwitch2State == HIGH && reedSwitch3State == HIGH && reedSwitch4State == LOW) {
status = 3; // 状态3
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(20, 1150, 255);
}
FastLED.show();
}
if (reedSwitch1State == LOW && reedSwitch2State == HIGH && reedSwitch3State == LOW && reedSwitch4State == HIGH) {
status = 4; // 状态4
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255, 20, 255);
}
allOff();
}
if (reedSwitch1State == HIGH && reedSwitch2State == LOW && reedSwitch3State == HIGH && reedSwitch4State == HIGH) {
status = 5; // 状态5 quan kai
flashBlue();
}
if (reedSwitch1State == LOW && reedSwitch2State == HIGH && reedSwitch3State == LOW && reedSwitch4State == LOW) {
status = 6; // 状态6(新的状态)
Random(); // 新的效果:彩色的随机
}
Serial.println(status);
// Serial.print(reedSwitch1State);
// Serial.print(reedSwitch2State);
// Serial.print(reedSwitch3State);
// Serial.println(reedSwitch4State);
delay(150);
}
// LED全亮粉色
void allRed() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255, 20, 197);
}
FastLED.show();
}
// 彩虹渐变效果
void rainbowEffect() {
static uint8_t hue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue + (i * 10), 255, 255);
}
FastLED.show();
hue++;
delay(20);
}
// 闪烁蓝色和绿色
void flashGreen() {
//static bool isOn = false;
//CRGB color = isOn ? CRGB(0, 0, 255) : CRGB(0, 20, 50);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = (0, 0, 255);
}
FastLED.show();
//isOn = !isOn;
delay(500);
}
// 闪烁
void allOff() {
static bool isOn = false;
CRGB color = isOn ? CRGB(random(255), 20, random(255)) : CRGB(0, 0, 0);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
}
FastLED.show();
isOn = !isOn;
delay(50);
}
// 新的闪亮效果
void allBlue() {
static bool isOn = false;
CRGB color = isOn ? CRGB(0255, 20, 176) : CRGB(0, 0, 0);
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
isOn = !isOn;
delay(100);
}
void flashRed() {
static int offset = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ((i + offset) % 3 == 0) ? CRGB(random(255), random(255), random(255)) : CRGB::Black;
}
FastLED.show();
offset = (offset + 1) % 3;
delay(100);
}
void Random() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(random(255), random(255), random(255));
}
FastLED.show();
}
void flashBlue() {
static bool isOn = false;
CRGB color = isOn ? CRGB(0, 0, 255) : CRGB(0, 0, 0);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
}
FastLED.show();
isOn = !isOn;
delay(300);
}