Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Last active February 23, 2016 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmpwizard/5a086323d6d25a754700 to your computer and use it in GitHub Desktop.
Save fmpwizard/5a086323d6d25a754700 to your computer and use it in GitHub Desktop.
LED Strip lights for the garage door
// Sample RFM69 receiver/gateway sketch, with ACK and optional encryption, and Automatic Transmission Control
// Passes through any wireless received messages to the serial port & responds to ACKs
// It also looks for an onboard FLASH chip, if present
// RFM69 library and sample code by Felix Rusu - http://LowPowerLab.com/contact
// Copyright Felix Rusu (2015)
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>//get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h> //comes with Arduino IDE (www.arduino.cc)
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#ifdef __AVR__
#include <avr/power.h>
#endif
//*********************************************************************************************
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NODEID 1 //unique for each node on same network
#define NETWORKID 100 //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
//*********************************************************************************************
#define SERIAL_BAUD 115200
#define LED 9 // Moteinos have LEDs on D9
#define FLASH_SS 8 // and FLASH SS on D8
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL)
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
bool on = false;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(SERIAL_BAUD);
delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
radio.setHighPower(); //only for RFM69HW!
#endif
radio.encrypt(ENCRYPTKEY);
radio.promiscuous(promiscuousMode);
char buff[50];
sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
if (flash.initialize()){
Serial.print("SPI Flash Init OK. Unique MAC = [");
flash.readUniqueId();
for (byte i=0;i<8;i++){
Serial.print(flash.UNIQUEID[i], HEX);
if (i!=8) Serial.print(':');
}
Serial.println(']');
} else {
Serial.println("SPI Flash MEM not found (is chip soldered?)...");
}
#ifdef ENABLE_ATC
Serial.println("RFM69_ATC Enabled (Auto Transmission Control)");
#endif
}
byte ackCount=0;
void loop() {
if (radio.receiveDone()){
char msg = 'Z';
msg = (char)radio.DATA[0];
if (msg == 'A' && !on) {
digitalWrite(LED, HIGH);
on = true;
} else if (msg == 'Z' && on) {
digitalWrite(LED, LOW);
on = false;
}
if (radio.ACKRequested()){
byte theNodeID = radio.SENDERID;
radio.sendACK();
}
}
}
// Sample RFM69 sender/node sketch, with ACK and optional encryption, and Automatic Transmission Control
// Sends periodic messages of increasing length to gateway (id=1)
// It also looks for an onboard FLASH chip, if present
// RFM69 library and sample code by Felix Rusu - http://LowPowerLab.com/contact
// Copyright Felix Rusu (2015)
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>//get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#include <Adafruit_NeoPixel.h>
#include "Arduino.h"
#ifdef __AVR__
#include <avr/power.h>
#endif
//*********************************************************************************************
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NODEID 2 //must be unique for each node on same network (range up to 254, 255 is used for broadcast)
#define NETWORKID 100 //the same on all nodes that talk to each other (range up to 255)
#define GATEWAYID 1
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
//*********************************************************************************************
#ifdef __AVR_ATmega1284P__
#define LED 15 // Moteino MEGAs have LEDs on D15
#define FLASH_SS 23 // and FLASH SS on D23
#else
#define LED 9 // Moteinos have LEDs on D9
#define FLASH_SS 8 // and FLASH SS on D8
#endif
#define SERIAL_BAUD 115200
int TRANSMITPERIOD = 150; //transmit a packet to gateway so often (in ms)
char payloadOn[] = "A";
char payloadOff[] = "Z";
char buff[20];
byte sendSize=0;
boolean requestACK = false;
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL)
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
//======================================
int indicatorLED = 10;
int echoPin = 3; // Echo Pin
int trigPin = 4; // Trigger Pin
int maximumRange = 20; // Maximum range needed
int minimumRange = 5; // Minimum range needed
int cnt = 0;
long duration, distance; // Duration used to calculate distance
boolean on = false;// 3 scans within range triggers the lights
//======================================
// 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 indicator = Adafruit_NeoPixel(1, indicatorLED, NEO_RGB + NEO_KHZ800);
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
indicator.begin();
indicator.setPixelColor(0, indicator.Color(255, 0, 0));
indicator.show();
//=============================
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//=============================
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
radio.setHighPower(); //uncomment only for RFM69HW!
#endif
radio.encrypt(ENCRYPTKEY);
//radio.setFrequency(919000000); //set frequency to some custom frequency
//Auto Transmission Control - dials down transmit power to save battery (-100 is the noise floor, -90 is still pretty good)
//For indoor nodes that are pretty static and at pretty stable temperatures (like a MotionMote) -90dBm is quite safe
//For more variable nodes that can expect to move or experience larger temp drifts a lower margin like -70 to -80 would probably be better
//Always test your ATC mote in the edge cases in your own environment to ensure ATC will perform as you expect
#ifdef ENABLE_ATC
radio.enableAutoPower(-70);
#endif
char buff[50];
if (flash.initialize()){
Serial.print("SPI Flash Init OK ... UniqueID (MAC): ");
flash.readUniqueId();
} else {
Serial.println("SPI Flash MEM not found (is chip soldered?)...");
}
#ifdef ENABLE_ATC
Serial.println("RFM69_ATC Enabled (Auto Transmission Control)\n");
#endif
}
long lastPeriod = 0;
bool ret = false;
void loop() {
ret = ultrasonicSensor();
if (ret && !on){
on = true;
indicator.setPixelColor(0, indicator.Color(0, 255, 0));
indicator.show();
sendSignal();
delay(1000);
} else if (ret && on) {
on = false;
indicator.setPixelColor(0, indicator.Color(255, 0, 0));
indicator.show();
sendSignal();
delay(1000);
}
}
void sendSignal(){
//check for any received packets
if (radio.receiveDone()) {
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
for (byte i = 0; i < radio.DATALEN; i++){
Serial.print((char)radio.DATA[i]);
}
Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
if (radio.ACKRequested()) {
radio.sendACK();
Serial.print(" - ACK sent");
}
Blink(LED,3);
Serial.println();
}
int currPeriod = millis()/TRANSMITPERIOD;
if (currPeriod != lastPeriod) {
lastPeriod=currPeriod;
//send FLASH id
if (on) {
if (radio.sendWithRetry(GATEWAYID, payloadOn, 1)) {
Serial.print(" ok!");
Serial.println((char)payloadOn[0]);
} else {
Serial.print(" nothing...");
}
} else {
if (radio.sendWithRetry(GATEWAYID, payloadOff, 1)) {
Serial.print(" ok!");
Serial.println((char)payloadOff[0]);
} else {
Serial.print(" nothing...");
}
}
}
}
void Blink(byte PIN, int DELAY_MS) {
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}
//=============================
bool ultrasonicSensor() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
//distance = duration/58.2;
distance = (duration/2) / 29.1;
if (distance >= maximumRange || distance <= minimumRange){
//Serial.println("out of range ");
//Serial.println(distance);
//sent = false;
cnt = 0;
return false;
} else if (distance >= minimumRange && distance <= maximumRange && cnt > 3){
Serial.println("triggered!");
cnt = 0;
return true;
} else if ((distance <= maximumRange && distance >= minimumRange)){
//catch current color
uint32_t currColor = indicator.getPixelColor(0);
indicator.setPixelColor(0, indicator.Color(0, 0, 255));
indicator.show();
delay(100);
indicator.setPixelColor(0, currColor);
indicator.show();
cnt++;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment