我是这个平台和 Arduino 的新手。我尝试使用 arduino nano 为 RGB Leds 创建蓝牙控制器。我的代码运行良好,但我想让它“完美”(如果你明白我的意思)。
因此,我在 MIT-App-Inventor 上设计了一个 Android 应用程序,它允许我单击一个按钮并选择我想要的 rgb 效果(静态颜色、彩虹效果等)。
问题是当我有“循环”效果、彩虹色和混合色时,它们会停在一个点上,颜色会在 LED 灯带上冻结。
我尝试创建另一个变量,该变量会根据所选效果的类型而变化,int var = 1;
第 48 行。但是这样做,我无法再逃避循环了。
有任何想法吗 ?
PS:我正在使用 WS2812b 3 米 LED 灯条、一个 Arduino Nano 和一个 HC-05 蓝牙模块
这是整个代码:
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
SoftwareSerial BT(10, 11); // TX, RX of the Bluetooth Module
#define PIXEL_PIN 5 // Data Pin of Led strip
#define PIXEL_COUNT 160 // Number of LEDs in the strip
#define BRIGHTNESS 125 // use 96 for medium brightness
#define SPEED 5 // Speed of each Color Transition (in ms)
#define RAINBOW_SPEED 27 // Rainbow Transition speed
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
bool offOld = LOW;
bool WhiteOld = LOW;
bool RedOld = LOW;
bool GreenOld = LOW;
bool BlueOld = LOW;
bool YellowOld = LOW;
bool OrangeOld = LOW;
bool pinkOld = LOW;
bool purpleOld = LOW;
bool cyanOld = LOW;
bool RainbowOld = LOW;
bool rgbOld = LOW;
bool RandomOld = LOW;
int showType = 0;
void setup() {
BT.begin(9600);
BT.println("Connected to Arduino");
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show();
}
int var = 1;
char a;
void loop() {
bool off = LOW;
bool White = LOW;
bool Blue = LOW;
bool Red = LOW;
bool Green = LOW;
bool Yellow = LOW;
bool Orange = LOW;
bool pink = LOW;
bool purple = LOW;
bool cyan = LOW;
bool Rainbow = LOW;
bool rgb = LOW;
bool Random = LOW;
bool ende;
if (BT.available())
{
a= (char)BT.read();
if(a=='f')
{
off = HIGH;
var = 1;
BT.println("LEDs OFF");
}else{
off = LOW;
}
// ===========================================================================================
if(a=='w')
{
White = HIGH;
var = 1;
BT.println("LEDs turning White");
}else{
White = LOW;
}
// ===========================================================================================
if(a=='b')
{
Blue = HIGH;
var = 1;
BT.println("LEDs turning Blue");
}else{
Blue = LOW;
}
// ===========================================================================================
if(a=='r')
{
Red = HIGH;
var = 1;
BT.println("LEDs turning Red");
}else{
Red = LOW;
}
// ===========================================================================================
if(a=='g')
{
Green = HIGH;
var = 1;
BT.println("LEDs turning Green");
}else{
Green = LOW;
}
// ===========================================================================================
if(a=='y')
{
Yellow = HIGH;
var = 1;
BT.println("LEDs turning Yellow");
}else{
Yellow = LOW;
}
// ===========================================================================================
if(a=='o')
{
Orange = HIGH;
var = 1;
BT.println("LEDs turning Orange");
}else{
Orange = LOW;
}
// ===========================================================================================
if(a=='p')
{
pink = HIGH;
var = 1;
BT.println("LEDs turning Pink");
}else{
pink= LOW;
}
// ===========================================================================================
if(a=='v')
{
purple = HIGH;
var = 1;
BT.println("LEDs turning Purple");
}else{
purple = LOW;
}
// ===========================================================================================
if(a=='c')
{
cyan = HIGH;
var = 1;
BT.println("LEDs turning Cyan");
}else{
cyan = LOW;
}
// ===========================================================================================
if(a=='a')
{
Rainbow = HIGH;
var = 0;
BT.println("LEDs making Rainbow");
}else{
Rainbow = LOW;
}
// ===========================================================================================
if(a=='m')
{
rgb = HIGH;
var = 0;
BT.println("LEDs making Mixed Colors");
}else{
rgb = LOW;
}
// ===========================================================================================
if(a=='e')
{
Random = HIGH;
var = 0;
BT.println("LEDs turning random colors");
}else{
Random = LOW;
}
}
if (off == LOW && offOld == HIGH) {
delay(20);
if (off == LOW) {
do {
showType = 0 ; // Off animation Type 0
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (White == LOW && WhiteOld == HIGH) {
delay(20);
if (White == LOW) {
do {
showType = 1 ; // White animation Type 1
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (Red == LOW && RedOld == HIGH) {
delay(20);
if (Red == LOW) {
do {
showType = 2 ; // Red animation Type 2
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (Green == LOW && GreenOld == HIGH) {
delay(20);
if (Green == LOW) {
do {
showType = 3 ; // Green animation Type 3
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (Blue == LOW && BlueOld == HIGH) {
delay(20);
if (Blue == LOW) {
do {
showType = 4 ; // Blue animation Type 4
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (Yellow == LOW && YellowOld == HIGH) {
delay(20);
if (Yellow == LOW) {
do {
showType = 5 ; // Yellow animation Type 5
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (Orange == LOW && OrangeOld == HIGH) {
delay(20);
if (Orange == LOW) {
do {
showType = 8 ; // Orange animation Type 8
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (pink == LOW && pinkOld == HIGH) {
delay(20);
if (pink == LOW) {
do {
showType = 9 ; // Pink animation Type 9
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (purple == LOW && purpleOld == HIGH) {
delay(20);
if (purple == LOW) {
do {
showType = 10 ; // Purple animation Type 10
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (cyan == LOW && cyanOld == HIGH) {
delay(20);
if (cyan == LOW) {
do {
showType = 11 ; // Cyan animation Type 11
startShow(showType);
} while (a=0);
}
}
// ===========================================================================================
if (rgb == LOW && rgbOld == HIGH) {
delay(20);
if (rgb == LOW) {
showType = 6; // Mix animation Type 6
startShow(showType);
}
}
// ===========================================================================================
if (Rainbow == LOW && RainbowOld == HIGH) {
delay(20);
if (Rainbow == LOW) {
showType = 7; // Rainbow animation Type 7
startShow(showType);
}
}
// ===========================================================================================
if (Random == LOW && RandomOld == HIGH) {
delay(20);
if (Random == LOW) {
showType = 12; // Random colors Type 12
startShow(showType);
}
}
WhiteOld = White;
RedOld = Red;
BlueOld = Blue;
GreenOld = Green;
YellowOld = Yellow;
OrangeOld = Orange;
pinkOld = pink;
purpleOld = purple;
cyanOld = cyan;
offOld = off;
RainbowOld = Rainbow;
rgbOld = rgb;
RandomOld = Random;
}
void startShow(int i) {
switch(i){
case 0: colorWipe(strip.Color(0, 0, 0), SPEED); // Black/off
break;
case 1: strip.setBrightness(255); // Changes the Brightness to MAX
colorWipe(strip.Color(255, 255, 255), SPEED); // White
strip.setBrightness(BRIGHTNESS); // Reset the Brightness to Default value
break;
case 2: colorWipe(strip.Color(255, 0, 0), SPEED); // Red
break;
case 3: colorWipe(strip.Color(0, 255, 0), SPEED); // Green
break;
case 4: colorWipe(strip.Color(0, 0, 255), SPEED); // Blue
break;
case 5: colorWipe(strip.Color(255, 220, 0), SPEED); // Yellow
break;
case 8: colorWipe(strip.Color(255, 100, 0), SPEED); // Orange
break;
case 9: colorWipe(strip.Color(255, 0, 127), SPEED); // pink
break;
case 10: colorWipe(strip.Color(127, 0, 255), SPEED); // purple
break;
case 11: colorWipe(strip.Color(0, 255, 255), SPEED); // cyan
break;
case 6: do {
colorWipe(strip.Color(255, 220, 0), SPEED); // Yellow
colorWipe(strip.Color(255, 100, 0), SPEED); // Orange
colorWipe(strip.Color(255, 0, 0), SPEED); // Red
colorWipe(strip.Color(255, 0, 127), SPEED); // pink
colorWipe(strip.Color(127, 0, 255), SPEED); // purple
colorWipe(strip.Color(0, 0, 255), SPEED); // Blue
colorWipe(strip.Color(0, 255, 255), SPEED); // cyan
colorWipe(strip.Color(0, 255, 0), SPEED); // Green
theaterChase(strip.Color(255, 220, 0), SPEED); // Yellow
theaterChase(strip.Color(255, 100, 0), SPEED); // Orange
theaterChase(strip.Color(255, 0, 0), SPEED); // Red
theaterChase(strip.Color(255, 0, 127), SPEED); // pink
theaterChase(strip.Color(127, 0, 255), SPEED); // purple
theaterChase(strip.Color(0, 0, 255), SPEED); // Blue
theaterChase(strip.Color(0, 255, 255), SPEED); // cyan
theaterChase(strip.Color(0, 255, 0), SPEED); // Green
}while (var == 0);
case 7: do {
rainbowCycle(25);
}while (var == 0);
case 12: do {
colorWipe(strip.Color(random(0, 255),random(0, 255),random(0, 255)), SPEED);
}while (var == 0);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*2; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}