Receiving Pillow Code
//This code is for the "Receiving Pillow"
// This #include statement was automatically added by the Particle IDE.
#include <adafruit-drv2605-photon.h>
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D3
#define PIXEL_COUNT 14
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int vibrator=D2;
int button= D3;
int buttonState = true;
#include <Wire.h>
//#include "Adafruit_DRV2605.h"
Adafruit_DRV2605 drv;
uint8_t effect = 7;
uint32_t wait = 10;
// This value will store the last time we published an event
// This value will store the last time we published an event
long lastPublishedAt = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 10000;
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
Particle.subscribe( "hugIt/imReady" , handleSharedEvent1 );
Particle.subscribe("hugIt/imScared", handleSharedEvent2);
pinMode( vibrator, OUTPUT);
Serial.begin(9600);
drv.begin();
drv.selectLibrary(1);
// I2C trigger by sending 'go' command
// default, internal trigger when sending GO command
drv.setMode(DRV2605_MODE_INTTRIG);
}
void loop()
{
uint16_t i;
uint32_t c = strip.Color(219, 101, 255);
uint32_t c_off = strip.Color(0, 0, 0);
// publish my event
// you'll want some more complex stuff here
// buttonState = digitalRead( buttonPin );
// if(buttonState == LOW) {
// publishMyEvent();
// }
}
void publishMyEvent()
{
// Remember that a device can publish at rate of about 1 event/sec,
// with bursts of up to 4 allowed in 1 second.
// Back to back burst of 4 messages will take 4 seconds to recover.
// So we want to limit the amount of publish events that happen.
// check that it's been 10 secondds since our last publish
if( lastPublishedAt + publishAfter < millis() )
{
// Remember our subscribe is matching "db2018/paired/"
// We'll append the device id to get more specific
// about where the event came from
// System.deviceID() provides an easy way to extract the device
// ID of your device. It returns a String object of the device ID,
// which is used to identify your device.
String eventName1 = "hugIt/imReady" + System.deviceID();
String eventName2 = "hugIt/imScared" + System.deviceID();
Particle.publish( eventName1, "I'm SCARED" );
Particle.publish( eventName2, "Ehhhhh" );
//Publish event to all devices
lastPublishedAt = millis();
}
}
// Our event handlde requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
void handleSharedEvent1(const char *event, const char *data)
{
uint16_t i;
uint32_t c = strip.Color(219, 101, 255);
uint32_t c_off = strip.Color(0, 0, 0);
// Now we're getting ALL events published using "db2018/paired/"
// This includes events from this device.
// So we need to ignore any events that we sent.
// Let's check the event name
String eventName = String( event ); // convert to a string object
String deviceID = System.deviceID();
// device id = 0123456789abcdef
// event = "diot/2019/paired/0123456789abcdef"
if( eventName.indexOf( deviceID ) != -1 ){
// if we get anything other than -1
// the event came from this device.
// so stop doing stuff
return;
}
// otherwise do your stuff to respond to
// the paired device here
for (i=0; i< strip.numPixels() ; i++ ){
strip.setPixelColor(i,c);
delay(500);
}
strip.show();
delay(5000);
for (i=0; i< strip.numPixels() ; i++ ){
strip.setPixelColor(i,c_off);
}
strip.show();
delay(100);
}
void handleSharedEvent2(const char *event, const char *data)
{
// Now we're getting ALL events published using "db2018/paired/"
// This includes events from this device.
// So we need to ignore any events that we sent.
// Let's check the event name
String eventName = String( event ); // convert to a string object
// This gives us access to a bunch of built in methods
// Like indexOf()
// Locates a character or String within another String.
// By default, searches from the beginning of the String,
// but can also start from a given index,
// allowing for the locating of all instances of the character or String.
// It Returns: The index of val within the String, or -1 if not found.
// We can use this to check if our event name contains the
// id of this device
String deviceID = System.deviceID();
// device id = 0123456789abcdef
// event = "diot/2019/paired/0123456789abcdef"
if( eventName.indexOf( deviceID ) != -1 ){
// if we get anything other than -1
// the event came from this device.
// so stop doing stuff
return;
}
digitalWrite( vibrator, HIGH );
delay(100);
Serial.print("Effect #"); Serial.println(effect);
// set the effect to play
drv.setWaveform(0, effect); // play effect
drv.setWaveform(1, 0); // end waveform
// play the effect!
drv.go();
for( int i = 0; i < 3; i++ )
{
digitalWrite(vibrator, HIGH);
delay( 1000 ) ;
}
digitalWrite( vibrator, LOW );
// delay(wait * 60 * 1000);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .