如何解决是否可以为某些功能创建一个循环,但在外部信息进入时停止它们?
我是这个平台和 Arduino 的新手。我尝试使用 arduino nano 为 RGB LED 创建蓝牙控制器。我的代码运行良好,但我想让它“完美”(如果你明白我的意思)。
所以,我在 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),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,SPEED); // Red
break;
case 3: colorWipe(strip.Color(0,SPEED); // Green
break;
case 4: colorWipe(strip.Color(0,SPEED); // Blue
break;
case 5: colorWipe(strip.Color(255,220,SPEED); // Yellow
break;
case 8: colorWipe(strip.Color(255,100,SPEED); // Orange
break;
case 9: colorWipe(strip.Color(255,127),SPEED); // pink
break;
case 10: colorWipe(strip.Color(127,SPEED); // purple
break;
case 11: colorWipe(strip.Color(0,SPEED); // cyan
break;
case 6: do {
colorWipe(strip.Color(255,SPEED); // Yellow
colorWipe(strip.Color(255,SPEED); // Orange
colorWipe(strip.Color(255,SPEED); // Red
colorWipe(strip.Color(255,SPEED); // pink
colorWipe(strip.Color(127,SPEED); // purple
colorWipe(strip.Color(0,SPEED); // Blue
colorWipe(strip.Color(0,SPEED); // cyan
colorWipe(strip.Color(0,SPEED); // Green
theaterChase(strip.Color(255,SPEED); // Yellow
theaterChase(strip.Color(255,SPEED); // Orange
theaterChase(strip.Color(255,SPEED); // Red
theaterChase(strip.Color(255,SPEED); // pink
theaterChase(strip.Color(127,SPEED); // purple
theaterChase(strip.Color(0,SPEED); // Blue
theaterChase(strip.Color(0,SPEED); // cyan
theaterChase(strip.Color(0,SPEED); // Green
}while (var == 0);
case 7: do {
rainbowCycle(25);
}while (var == 0);
case 12: do {
colorWipe(strip.Color(random(0,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,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);
}
解决方法
如果您是初学者,我建议您使用 MsTimer2
库,它会根据中断定期调用一个函数(回调函数)。因此,您可以在回调函数中定期读取和检查从 BT 模块接收到的字符。
只需参考此处的示例:https://playground.arduino.cc/Main/MsTimer2/
该示例显示通过定期调用 flash()
函数每 0.5 秒切换一次 LED,即使程序在无限 loop()
函数中运行。
编辑)幸运的是,由于它的简单性,您在使用 MsTimer2
时不需要了解什么是中断。但我建议您研究中断,因为它对于使用包括 Arduino 系列在内的微控制器至关重要,就像您的情况一样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。