초음파센서와 네오픽셀을 연동하려 하는데요
초음파센서가 처음에값만 읽고 그 후에는 0cm로 읽히더라구요...
코드 어느 부분이 잘못된걸까요?ㅠㅠ
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
const int trigPin = 8;
const int echoPin = 7;
#define PIN 5
//CRGB leds[NUM_LEDS];
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(9600);
//FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// 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
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
long microsecondsToInches(long microseconds);
long microsecondsToCentimeters(long microseconds);
long duration, inches, cm;
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in");
Serial.print(cm);
Serial.print("cm");
Serial.println();
void colorWipe(uint32_t c, uint8_t wait);
if(cm>10)
{
colorWipe(strip.Color(0, 0, 0), 50);
}
else
{
colorWipe(strip.Color(255, 255, 255), 50);
}
}
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);
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per
// centimeter. The ping travels out and back, so to find
//the distance of the object we take half of the distance
//travelled.
return microseconds / 29 / 2;
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at
//1130 feet per second). This gives the distance travelled
//by the ping, outbound and return, so we divide by 2 to get the
//distance of the obstacle.
//See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
|