David VarblaneMatthew Zotsman
Published

IoT Light Strip

Learn how to set up a light-sensitive LED Strip using only 2 Particle Argon Kits and an Individually Programmable LED Strip!

IntermediateFull instructions provided7 hours1,132
IoT Light Strip

Things used in this project

Hardware components

Particle Argon Kit
×2
Individually Addressable LED Strip
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

Light Sensor Schematic

LED Strip + Argon Schematic

Code

Light Sensor Code

C/C++
Code for the Argon with the integrated light sensor. Data is read from the photoresistor and an event is published both for when light is detected and when it is not
//LED light connected to A5 and ground 
int led1 = A5; 

//LED on the Argon 
int led2 = D7; 

//Photoresistor connected to A0 pin that detects light 
int photoresistor = A0;
int analogValue;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(photoresistor, INPUT);

}

//Loop constructed to read analog value of the photoresistor and digital 
//write to the LEDs in the circuit. Particle.publish is used twice to publish 
//an event both when light is detected and when it is not. 
void loop() {
    analogValue = analogRead(photoresistor);
    
//if statement below can be changed to adjust how sensitive the photoresistor is to light
    if (analogValue > 15) {
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        Particle.publish("LED-OFF", "0", PUBLIC);
        delay(7000);
    } else {
        digitalWrite(led1, HIGH);
        digitalWrite(led2, HIGH);
        Particle.publish("LEDON", "1", PUBLIC);
        delay(7000);
    }
}

LED Strip Controller

C/C++
Code used to run the individually addressable LED strip. Published events are read to indicate when the strip turns on. Different patterns are defined and can be used.
#include "Particle.h"
#include "neopixel.h" 

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 300
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

//Defines the color chase pattern 
void colorChase(uint32_t c, uint8_t wait) {
    int i;
    
    for (i=0; i<strip.numPixels()+10; i++) strip.setPixelColor(i, 0); 
    
    for (i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        strip.setPixelColor(i-10, 0);
        delay(wait);
    }
    
    strip.show();
}


//Defines the color all pattern(makes whole strip one color)
void colorAll(uint32_t c, uint8_t wait) {
  uint16_t i;

  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(wait);
}

//Defines the color wipe pattern 
void colorWipe(uint32_t c, uint8_t wait) {
    int i;
    
    for (i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);    
    
    for(uint16_t i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait);
  }
}

void setup() {
    
//Subscribes to the two published events from the Argon with the light sensor
    Particle.subscribe("LEDON", myHandler, MY_DEVICES);
    Particle.subscribe("LED-OFF", Handler, MY_DEVICES);
    strip.begin();
    strip.show();
}


//Handler function for when light IS NOT detected
void myHandler(const char *event, const char *data) {
    
    Serial.print(event);
    Serial.print(", data: ");
    
    if (data = "1") {
        colorWipe(strip.Color(255, 255, 0), 5);
        delay(1000);
    }

 }

//Handler function for when light IS detected 
void Handler(const char *event, const char *data) {
    
    Serial.print(event);
    Serial.print(", data: ");
    
    if (data = "0") {
        colorChase(strip.Color(0, 0, 0), 10);
        delay(1000);
    }

 }

//Can be used for continuous loop without being connected to the light sensor
//void loop() {
    
    //colorWipe(strip.Color(255, 255, 255), 5);
    
    //colorChase(strip.Color(255, 0, 255), 5);
 
    //colorChase(strip.Color(0, 255, 255), 5);
     
    //colorChase(strip.Color(255, 255, 0), 5);
     
    //colorWipe(strip.Color(0, 255, 255), 5);

    
//}  

Credits

David Varblane

David Varblane

1 project • 0 followers
Matthew Zotsman

Matthew Zotsman

1 project • 0 followers

Comments