棘手问题,我用uno开发板连接超声波传感器控制WS2812灯带,原本应该是代码上传到开发板,插上电源,我触发超声波传感器灯带才亮,离开传感器感应范围就熄灭,但是现在的效果是插上电源不用传感器灯带就亮了,我把传感器都拔掉了也不影响它继续亮,是不是代码缺少什么控制传感器的语句啊求求朋友们帮帮忙看一看
#include<Adafruit_NeoPixel.h>
#ifdef __AVR__
#include<avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 120
Adafruit_NeoPixel pixels(NUMPIXELS,PIN,NEO_GRB+NEO_KHZ800);
#define DELAYVAI 500
int TrigPin = 8;
int EchoPin = 9;
int val;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
pinMode(PIN,OUTPUT);
#if defined(_AVR_ATtiny85_)&&(F-CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
pixels.begin();
}
void loop()
{
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(EchoPin,HIGH);
delayMicroseconds(10);
int cm = pulseIn(EchoPin,HIGH)/20.0;
cm = (int(cm*100.0))/100.0;
Serial.println(cm);
Serial.println("cm");
delay(1);
if(58>=cm)
{
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i,pixels.Color(random (48,238),random (113,255),random (62,241)));
pixels.show();
delay(2);
}
}
else{
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i,pixels.Color(0,0,0));
pixels.show();
delay(2);
}
}
}
|