An interactive lamp that changes speed based on distance of objects in front of it. This uses the Arduino UNO a 60 LED Adafruit NeoPixel strip and an Ultrasonic Ping Sensor.
This also introduces the concept of functions in programming. The circuit is fairly straight forward as Om has tried to illustrate in this diagram! 🙂
Refer to Adafruit’s Neopixel Überguide and the Arduino Ping Sensor examples for detailed how to’s. (Note the ping sensor we used has a two separate pins for input and output rather than just one shown in the Arduino example.).
Here is the code.
#include <Adafruit_NeoPixel.h> Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 2); int delaytime=30; const int pingPin = 7; const int echoPin = 8; void setup() { strip.begin(); strip.show(); pinMode(pingPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 255, 0, 0); strip.show(); delay(delaytime);} delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 255, 255, 0); strip.show(); delay(delaytime);} delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 0, 255, 255); strip.show(); delay(delaytime);} delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 0, 255, 0); strip.show(); delay(delaytime);} delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 0, 0, 255); strip.show(); delay(delaytime);} delaytime=pingyity(); for(int i=0; i<60; i++){ strip.setPixelColor(i, 255, 0, 255); strip.show(); delay(delaytime); } } int pingyity() { //Send ping signal out long duration, inches; digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); //read ping sensor signal input duration = pulseIn(echoPin, HIGH); // convert the time into a distance inches = duration / 74 / 2; //set light delay time based on sensor if(inches<=30) return(inches); else return(30); }