Matha Goram
Published © GPL3+

Lighting Your Presence

This note is a basic introduction on the use of an interrupt service routine to work on actions autonomously upon detecting a state change.

BeginnerFull instructions provided30 minutes813
Lighting Your Presence

Things used in this project

Hardware components

ESP8266 NodeMCU board
×1
HC-SR501 Passive Infra-Red Sens
×1
WS2815 LED Strip 12V
×1
Alitove 12V 15A power supply
×1
Screw terminals for breadboard
×1
Elegoo Dupont wires
×1
Elegoo Breadboard, full
×1

Software apps and online services

Arduino IDE
Arduino IDE
FastLED library

Story

Read more

Custom parts and enclosures

Schematic

Generated by Fritzing

Schematics

Assembly diagram

Generated by Fritzing

Code

Code

C/C++
Requires Arduino IDE and FastLED library
/*
 * @file testWS2815_HC_SR501.ino
 * \brief Use PIR sensor to trigger LED strip illumination
 * @author https://github.com/FastLED/FastLED/blob/master/examples/ColorPalette/ColorPalette.ino
 * @version 0.1 2020-08-12 Initial DRAFT
 * @change  0.2 2020-12-19 added ISR handler
 * @descr
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses></https:>.
 * 
 * Copyright (c) 2020, Original developers; All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * Neither the name of the driver nor the names of its contributors may not be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS  AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 * 
 * Notes:
 *        ADC0    A0    D0  GPIO16  WAKE
 *        Reserved      D1  GPIO5   SCL
 *        Reserved      D2  GPIO4   SDA
 * SSD3   GPIO10  SD3   D3  GPIO0 FLASH
 * SDD2   GPIO9   SD2   D4  GPIO2 TXD1
 * SDD1   MOSI    SD1   3V3 3.3v
 * SDCMD  CS      CMD   GND GND
 * SDD0   MISO    SDO   D5  GPIO14  SCLK
 * SDCLK  SCLK    CLK   D6  GPIO12  MISO
 *        GND     GND   D7  GPIO13  MOSI  TXD2
 *        3.3V    3V3   D8  GPIO15  CS    TXD2
 *        EN      EN    RX  GPIO3   RXD0
 *        RST     RST   TX  GPIO1   TXD0
 *        GND     GND   GND GND
 *        Vin     Vin   3V3 3.3V
 * The shield must have the following preset assignments:
 * MAC address
 * static IP address
 * 
 * HC-SR501
 * Bottom view
 *            GND Data  +Vcc
 * H: repeat trigger
 * L: single trigger    
 *        Sensitivity TimeDelay
 * 
 * 1  +Vcc 5-12V
 * 2  Data
 * 3  GND
 * Trigger modes
 * L  Data goes high only once until time delay period expires
 * H  Data goes high as many times as object moves in detection range
 * 
 * This example avoids the use of Serial Monitor intentionally because
 * the goal is to operate the more advanced solutions without any interface to a desktop.
 * It may not be a textbook quality example for emulation purposes.
 * 
 * References:
 * Libraries
 * @see [FastLED](https://github.com/FastLED/FastLED/blob/master/examples/ColorPalette/ColorPalette.ino)
 * @see [SPI](https://www.arduino.cc/en/Reference/SPI)
 * @see [Ethernet](https://www.arduino.cc/en/Reference/Ethernet)
 * @see [PubSubClient](https://github.com/knolleary/pubsubclient)
 * @see [JSON](https://github.com/arduino-libraries/Arduino_JSON)
 */

#include <FastLED.h>

/*
    For LED strips like WS281x (5050 LED chipset), which have:
    - a data line
    - ground, and
    - power
    only DATA_PIN needs to be defined even if a fourth wire is available.
    For LED chipsets that are SPI based using four wires:
    - data
    - clock
    - ground
    - power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
    Clock pin only needed for SPI based chipsets when not using hardware SPI
*/

#define DATA_PIN  2   // NOT GPIO4, NOT 4, NOT D2 BUT simply 2!!!
#define CLOCK_PIN 13  // unused for WS281x
#define NUM_LEDS  300

#define pinHC_SR501 D3 // D1 on ESP8266 NodeMCU board

CRGB leds[NUM_LEDS];

volatile boolean motionDetected = false;   // flag for HC-SR501 state

/** Callback function for interrupt service routine to handle HC-SR501
 *  
 *  Check digital pin
 *  
 *  @param none
 *  No parameters for ISR function
 *  
 *  @return void
 */
void ICACHE_RAM_ATTR ISR_HC_SR501()
{
  motionDetected = true;
}

/** Callback function for interrupt service routine to handle HC-SR501
 *  
 *  Check digital pin
 *  
 *  @param none
 *  No parameters for ISR function
 *  
 *  @return void
 */

void fillBlank()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CRGB(0, 0, 0);
  }
  FastLED.show();
  delay(1000);
}

void setup()
{
  attachInterrupt(digitalPinToInterrupt(pinHC_SR501), ISR_HC_SR501, CHANGE); // LOW, CHANGE, FALLING
  delay(2000);                                              // for w/LEDs
  FastLED.addLeds<WS2813, DATA_PIN, GRB>(leds, NUM_LEDS);   // changed from RGB
  fillBlank();
}

void fillStack()
{
  int j = random(0, 6);
  for (int k = 0; k < j; k++)
  {
    leds[k] = CRGB(0, 0, 255);
  }
  for (int i = j; i < NUM_LEDS; i++)
  {
    leds[i]   = CRGB(255,   0,   0);
    leds[++i] = CRGB(  0, 255,   0);
    leds[++i] = CRGB(  0, 255,   0);
    leds[++i] = CRGB(  0,   0, 255);
    leds[++i] = CRGB(  0,   0, 255);
    leds[++i] = CRGB(  0,   0, 255);
    leds[++i] = CRGB(  0,   0,   0);
    FastLED.show();
    delay(100);
  }
  delay(1000);
}


void loop()
{
  if (motionDetected)
  {
    for (int i = 0; i < 5; i++)
    {
      fillStack();
      fillBlank();
    }
    motionDetected = false;
  }
}

/*
// Additional notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color.  You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative.  FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact 
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved 
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.
*/

Credits

Matha Goram

Matha Goram

27 projects • 22 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.

Comments