Updated the sketch for MySensors V2 and am now using the Adafruit Neopixel library
/**
*******************************
*
* DESCRIPTION
* A node to indicate correct parking position to allow closing of garage door.
* uses a pre built IR beam "door minder" to detect car position
*MySensors V2 and using Adafruit neopixel library and bounce2 library
* Connect N/O contacts of Infrared device between
* pin 3 and GND.
*
* Connect a N/O reed switch between GND and pin 2
*
* Connect RGB Led strip data terminal to pin 4
*
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
#include "MySensors.h"
#include <SPI.h>
#include <Bounce2.h>
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 18 // How many leds are in the strip? change this to the number of LEDs in your strip
#define PIN 4 // Data pin that led data will be written out over
#define CHILD_ID 0 // door switch MySensors id number
#define IR_PIN 3 // Arduino Digital I/O pin for infrared device
#define DOOR_PIN 2 // pin garage door switch is connected to
int marker = 0; // used to decide if LEDs are allowed to turn white
int irValue ; // holder for ir state
int doorValue ; // holder for garage door state
int oldDoorValue=-1; //set to -1 to ensure current door switch status is sent at startup
unsigned long startMillis = 0; // holder for the time when garage door first opens
unsigned long millisNow = 0; // holder for the current time
const long activeTime = 120000; // Time the sensor will stay active after garage door is opened in milliseconds. change this to suit your situation
Bounce debouncerA = Bounce(); // Instantiate Bounce object 1.... iR switch
Bounce debouncerB = Bounce(); // Instantiate Bounce object 2.... Garage Door switch
MyMessage msg(CHILD_ID,V_TRIPPED);
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
/* ----Setup the buttons as inputs and activate their internal pull-up resistor---- */
pinMode(IR_PIN,INPUT_PULLUP); //set pin as input and activate internal pull-up resistor
pinMode(DOOR_PIN,INPUT_PULLUP); //set pin as input and activate internal pull-up resistor
/* ---After setting up the buttons, setup debouncers---- */
debouncerA.attach(IR_PIN);
debouncerA.interval(5);
debouncerB.attach(DOOR_PIN);
debouncerB.interval(5);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Park Sensor", "2.0");
// Register all sensors to gateway (they will be created as child devices)
present(CHILD_ID, S_DOOR); // Register binary input sensor to gw
}
void loop() {
updateSwitches(); // call function to check switches status
millisNow = millis(); // get the current time
if (millisNow - startMillis < activeTime){ // check to see if timeout has been reached
if (doorValue == HIGH ) { // garage door is open
if (irValue == LOW ) { // car is in ir beam
ledChange(strip.Color(255, 0, 0)); // car is blocking ir beam,turn LEDs red
}
else{
ledChange(strip.Color(0, 255, 0)); // car is out of ir beam, turn LEDs green
}
}
else{
if (marker == 1){
ledChange(strip.Color(127, 127, 127)); // door down but timer not finished. turn leds white for entry mode
}
}
}
else { // Timeout has been reached. Turn off LEDs
if (marker == 1){ // check marker to only turn off if needed
ledChange(strip.Color(0, 0, 0)); // turn off leds (black)
marker = 0;
}
}
}
/* --------------start of functions-------------- */
/* --Update the switches status, send door state change if required -- */
void updateSwitches (){
debouncerA.update(); // IR switch
debouncerB.update(); // Door switch
irValue = debouncerA.read(); // get the state of the IR switch
doorValue = debouncerB.read(); // get the state of the Door switch
if (doorValue != oldDoorValue) { // Check if digital input has changed and send in new value if it has
send(msg.set(doorValue==HIGH ? 1 : 0)); // Send in the new value
if (doorValue == HIGH){ // door is open
startMillis = millis(); // store start time of door opening for timeout check
marker = 1;
}
oldDoorValue = doorValue;
}
}
/* ----------function to change LED color------------*/
void ledChange(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
}