49713 Designing for the Internet of Things
· 18 members
A hands-on introductory course exploring the Internet of Things and connected product experiences.
Create a feeling of togetherness when living apart through the experience of watching a scary movie
We modelled this device after the interaction of squeezing someone’s arm during the scary part of a movie for reassurance.
The device uses pressure sensors and haptics to help couples feel closer to each other by communicating the emotions they are experiencing while watching a horror movie.
The person who is feeling scared will grip the arms of their hugging pillow, triggering the pressure sensors. The haptics on the other person’s pillow will vibrate lightly to let them know their friend or lover is scared.
//This code is for the "husband pillow"
//which senses the user's readiness for a movie
//and whether they are scared
// 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;
//setup pins
int bendAnalogPin = A0; // FSR is connected to analog 0
int lFsrAnalogPin = A2;//left fsr sensor
int rFsrAnalogPin = A4;//left fsr sensor
int bendReading; // the analog reading from the bend sensor
int lFsrReading; // the analog reading from the left FSR sensor
int rFsrReading; // the analog reading from the left FSR sensor
void setup(void)
{
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(bendAnalogPin , INPUT);
pinMode(lFsrAnalogPin , INPUT);
pinMode(rFsrAnalogPin , INPUT);
Particle.subscribe( "hugIt/imReady" , handleSharedEvent );
Particle.subscribe( "hugIt/imScared" , handleSharedEvent );
}
void loop(void)
{
int bendReading = analogRead(bendAnalogPin);
Serial.print("Bend Analog reading = ");
Serial.println(bendReading);
int lFsrReading = analogRead(lFsrAnalogPin);
Serial.print("lFsr Analog reading = ");
Serial.println(lFsrReading);
int rFsrReading = analogRead(rFsrAnalogPin);
Serial.print("rFsr Analog reading = ");
Serial.println(rFsrReading);
if(bendReading > 10)
{
publishImReady();//sends "ready" signal
delay(100);// delay for a bit
}
if(lFsrReading > 300)
{
publishImScared();//sends signal that you are scared
delay(100);
}
if(rFsrReading > 300)
{
publishImScared();//sends signal that you are scared
delay(100);
}
}
void publishImReady()
{
// 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 eventName = "hugIt/imReady" + System.deviceID();
// now we have something like "diot/2019/paired/0123456789abcdef"
// and that corresponds to this devices info
// then we share it out
Particle.publish( eventName, "I'm READY" );
// And this will get shared out to all devices using this code
// we just pubished so capture this.
lastPublishedAt = millis();
}
}
void publishImScared()
{
// 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 eventName = "hugIt/imScared" + System.deviceID();
// now we have something like "diot/2019/paired/0123456789abcdef"
// and that corresponds to this devices info
// then we share it out
Particle.publish( eventName, "I'm SCARED" );
// And this will get shared out to all devices using this code
// we just pubished so capture this.
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 handleSharedEvent(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;
}
}
Click to Expand
//This code is for the Solenoid
// 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;
int buttonPin = D3;
int buttonstate=0;
int solPin = D2;
//int buttonpin = D4;
//int z= 0;
//int read_button=0;
bool shouldActivate = false;
bool shouldDoIt = false;
//int duration = 60*1000;
//long start=0;
void setup()
{
// We'll want to subscribe to an event thats fairly unique
// From the Particle Docs
// A subscription works like a prefix filter.
// If you subscribe to "foo", you will receive any event
// whose name begins with "foo", including "foo", "fool",
// "foobar", and "food/indian/sweet-curry-beans".
// Basically this will match any event that starts with 'db2018/paired/'
// This is a feature we'll useto figure out if our event comes from
// this device or another (see publishMyEvent below)
pinMode(buttonPin, INPUT_PULLUP );
Particle.subscribe( "hugIt/imScared" , handleSharedEvent );
pinMode(solPin, OUTPUT);
// start = millis();
}
void loop()
{
// publish my event
// you'll want some more complex stuff here
buttonstate= digitalRead(buttonPin);
if(buttonPin == 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 eventName = "hugit/imscared" + System.deviceID();
// now we have something like "diot/2019/paired/0123456789abcdef"
// and that corresponds to this devices info
// then we share it out
Particle.publish( eventName, "data goes here" );
// And this will get shared out to all devices using this code
// we just pubished so capture this.
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 handleSharedEvent(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;
}
doSolenoid();
}
// otherwise do your stuff to respond to
// the paired device here
void doSolenoid()
{
for( int i = 0; i < 5; i++ )
{
digitalWrite(solPin, HIGH);
delay( 100 ) ;
digitalWrite(solPin, LOW);
delay( 100 );
}
}
//working code for solenoid
// Light up Neopixel
// 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 D2
#define PIXEL_COUNT 14
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int buttonPin = D3;
bool buttonState = true;
// 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" , handleSharedEvent );
}
void loop()
{
// publish my event
// you'll want some more complex stuff here
buttonState = digitalRead( buttonPin );
if(buttonState == LOW) {
publishMyEvent();
}
// delay for a bit
delay(100);
}
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 eventName = "hugIt/imReady" + System.deviceID();
// now we have something like "diot/2019/paired/0123456789abcdef"
// and that corresponds to this devices info
// then we share it out
Particle.publish( eventName, "I'm SCARED" );
// And this will get shared out to all devices using this code
// we just pubished so capture this.
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 handleSharedEvent(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);
}
strip.show();
delay(5000);
for (i=0; i< strip.numPixels() ; i++ ){
strip.setPixelColor(i,c_off);
}
strip.show();
delay(100);
}
Click to Expand
A hands-on introductory course exploring the Internet of Things and connected product experiences.
Create a feeling of togetherness when living apart through the experience of watching a scary movie
February 2nd, 2019