// NeoPixel Ring simple sketch (c) 2013 Shae Erisson // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library #include #ifdef __AVR__ #include #endif // Which pin on the Arduino is connected to the NeoPixels? // On a Trinket or Gemma we suggest changing this to 1 #define PIN 2 // How many NeoPixels are attached to the Arduino? #define NUMPIXELS 20 // defines ultrasonic pins numbers const int trigPin = 3; const int echoPin = 4; // defines variables long duration; int distance; int ledValue; // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest // example for more information on possible values. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGBW + NEO_KHZ800); int delayval = 83; // delay for half a second void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication pixels.begin(); // This initializes the NeoPixel library. pinMode(2, OUTPUT); } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration*0.034/2; // map the sensor reading to a range for the LED ledValue = map(distance, 0, 150, 0, 255); if (ledValue < 0) { ledValue = 0; } digitalWrite(2, ledValue); // Prints the distance on the Serial Monitor to know its range Serial.print("Distance: "); Serial.println(distance); // Prints the LED value on the Serial Monitor to know its range Serial.print("LED Value: "); Serial.println(ledValue); for(int i=0;i