我有一条Neopixels,我正在床上使用它。最近,我决定添加一个声音传感器,这样我就可以拍一次来切换我正在使用的光动画。现在我遇到麻烦让我的代码工作。我可以打开灯,但我再也不能拍了拍它们。我怀疑可能是问题在于我只是在我用来运行灯光动画的while循环结束时关闭灯光动画的条件,但是,我不知道我能做什么解决这个问题。有人可以帮我吗?
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
int soundSensor = 2;
int LED = 13;
void setup()
{
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode (soundSensor, INPUT);
pinMode (LED, OUTPUT);
}
void loop()
{
int toggle = 0;
int statusSensor = digitalRead (soundSensor);
if (statusSensor == 1 && toggle == 0)
{
toggle= 1;
digitalWrite(LED, HIGH);
//statusSensor = 0;
statusSensor = digitalRead (soundSensor);
}
//NEOPIXEL CODE:
while (toggle == 1)
{
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
digitalWrite(LED,HIGH);
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(255,255,255), 50); //White
colorWipe(strip.Color(255,255,255), 50);
colorWipe(strip.Color(0, 0, 255), 50); // Blue
rainbow(20);
rainbowCycle(20);
if(statusSensor == 1){
toggle = 0;
}
}
if (statusSensor == 1 && toggle == 1)
{
toggle = 0;
digitalWrite(LED, LOW);
}
}
//NEOPIXEL Functions:
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 rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; 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);
}
}
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);
答案 0 :(得分:0)
当您将切换设置为0时,不会将任何颜色写入新像素条带。所以它们只会处于它们所处的状态。当你将切换设置为零时,只需将它们全部设置为“黑色”(即关闭)即可解决这个问题:
if(statusSensor == 1) {
toggle = 0;
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(strip.Color(0,0,0));
}
strip.show();
}
如果您只是希望它们立即转出,您在这里不需要任何延迟,但也许您可以让它们逐渐消失?