Becky Ellis
Published

Neopixel Motion Activated Night Light

Neopixels make a moving rainbow when triggered by a distance less than 1 meter in front of an Ultrasonic sensor.

BeginnerFull instructions provided30 minutes10,980
Neopixel Motion Activated Night Light

Things used in this project

Hardware components

Resistor 1k ohm
Resistor 1k ohm
×1
Ultrasonic Sensor
×1
Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
NeoPixel strip
NeoPixel strip
×1

Story

Read more

Schematics

Good luck figuring this schematic out. Sorry.

Code

Code that works!

Arduino
#include <Adafruit_NeoPixel.h>
#define LEDPIN 12 // connect the Data from the strip to this pin on the Arduino
#define NUMBER_PIEXELS 43// the number of pixels in your LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER_PIEXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
int delayTime = 200;
int wait = 500;
int i = 0;


void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
strip.begin();

}
void loop()
{

digitalWrite(TrigPin, LOW);       
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //The echo time is converted into cm
cm = (int(cm * 100.0)) / 100.0;     //Keep two decimal places
Serial.print("Distance\t=\t");
Serial.print(cm);
Serial.print("cm");
Serial.println();
    
 if (cm <100){

for (int i=0; i<strip.numPixels()-1; i++) {
   // starting at i, draw the 7 color rainbow}
   // a seven segment rainbow with red on the highest pixel 
void rainbow7(uint16_t i, uint16_t wait) ;
    int np = strip.numPixels();  // we use the modulo function with this
    strip.setPixelColor(i     % np, 0, 0, 0); // off
    strip.setPixelColor((i+1) % np, 25, 0, 25); // violet
    strip.setPixelColor((i+2) % np, 255, 0, 255); // indigo
    strip.setPixelColor((i+3) % np, 0, 0, 150); // blue
    strip.setPixelColor((i+4) % np, 0, 150, 0); // green
    strip.setPixelColor((i+5) % np, 255, 255, 0); // yellow
    strip.setPixelColor((i+6) % np, 110, 70, 0); // orange
    strip.setPixelColor((i+7) % np, 150, 0, 0); // red
    strip.show();
    delay(wait); 
    strip.clear();
    strip.show();}}
  else{
  strip.clear();
  strip.show(); 
  }

}

Credits

Becky Ellis
0 projects • 10 followers
I'm a science teacher and electronics fanatic. :)Moderator for Hackster.io
Contact

Comments

Please log in or sign up to comment.