想用按键中断产生一个随机数点亮WS2812灯环
请教大家一个Arduino问题,想用按键中断产生一个随机数点亮WS2812灯环,但每次都同时点亮多个是哪里问题。#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel (NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
int delayval = 1000;
volatile int state = LOW;
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show();
attachInterrupt(1, stateChange, CHANGE);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0; i<16; i++)
{
for(int a=0; a<16; a++)
{
if(a == i) strip.setPixelColor(a, 5, 15, 0);
else strip.setPixelColor(a,0);
strip.show();
}
delay(1000);
}
}
void stateChange()
{
int r = random(15);
strip.setPixelColor(r, 5, 25, 0);
strip.show();
Serial.println(r);
delay(5000);
}
因为你设置的是双边沿触发中断,相当于你按下会产生中断点亮一个灯,放开的时候也会产生中断,点亮一个灯。建议你将中断设置为,上升沿或下降沿触发,另外中断函数里面最好不要使用延时函数,里面内容尽量简短 挺好的想法
页:
[1]