|
我想用按钮来切换led灯条的显示模式,程序码写完测试时,按下按钮只有第一项有显示,另外的都无法显示,用Serial.println()查看数值,按键按第2次时,数值会变到-255去,再按按键变-254再按变-253........,请问有大神知道原因吗?可以幫我解答一下, 附上我的程序码
板子是Arduino nano v3 的
#include <FastLED.h>
#define LED_TYPE WS2812B
#define NUM_LEDS 12
#define COLOR_ORDER GRB
#define LED_PIN 2
#define button 3
#define brightness 5
CRGB myleds[NUM_LEDS];
bool buttonUp = true;
int Time = 0;
void setup()
{
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(myleds, NUM_LEDS);
FastLED.setBrightness(brightness);
FastLED.show();
FastLED.clear();
}
void loop()
{
bool newState = digitalRead(button);
if (newState == LOW && buttonUp == true)
{
delay(50);
// Serial.print("按鈕按下後");
// Serial.println(newState);
newState = digitalRead(button);
if (newState == LOW)
{
Time++;
Serial.println(Time);
if (Time > 4)
{
Time = 0;
}
}
showPattern(Time);
}
buttonUp = newState;
}
void showPattern(int n)
{
if (n == 0)
{
for (int i = 0; i <= NUM_LEDS; i++)
{
myleds[i] = CRGB(0, 0, 0);
FastLED.show();
delay(200);
}
}
else if (n == 1)
{
led_1();
}
else if (n == 2)
{
led_2();
}
else if (n == 3)
{
led_3();
}
}
void led_1()
{
for (int i = 0; i <= NUM_LEDS; i++)
{
myleds[i] = CRGB(255, i * 10, 0);
FastLED.show();
delay(200);
}
for (int i = NUM_LEDS; i >= 0; i--)
{
myleds[i] = CRGB(0, 255, i * 10);
FastLED.show();
delay(200);
}
}
void led_2()
{
for (int i = 0; i <= NUM_LEDS; i++)
{
fill_rainbow(myleds, i, 0, 23);
FastLED.show();
delay(10);
FastLED.clear();
}
}
void led_3()
{ // blue
FastLED.setBrightness(10);
for (int i = 0; i < NUM_LEDS / 2; i++)
{
myleds[i] = CRGB(0, 0, 255);
myleds[12 - i] = CRGB(0, 0, 255);
FastLED.show();
delay(30);
}
//delay(1000);
for (int i = 0; i < NUM_LEDS / 2; i++)
{
myleds[i] = CRGB(0, 0, 0);
myleds[12 - i] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
}
}
|
|