// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 30
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel lightstrip = Adafruit_NeoPixel( PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE );
//Designing for IoT 11/15/2019
//Group: Friend Finder
//Function: Turn on leds and solenoids when buttons are pressed.
int solPin = D0;
int times = 1;
int d1 = 100;
int d2 = 1000;
int buttonPin = D4;
int ledPin = D3;
int OnTime = 0;
bool buttonDown=false;//as a trigger
bool onOrNot=false;//if the person is available
int LastClick=0;
bool shouldDoIt = false;
// 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 = 1000;
void setup()
{
lightstrip.begin();
lightstrip.show();
Serial.begin( 9600 );
Particle.variable( "times", ×, INT );
Particle.variable( "d1", &d1, INT );
Particle.variable( "d2", &d2, INT );
Particle.function( "setTimes", setTimes );
Particle.function( "setD1", setDelayOne );
Particle.function( "setD2", setDelayTwo );
Particle.function( "test", testSolenoid );
pinMode(solPin, OUTPUT);
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
pinMode( ledPin , OUTPUT ); // sets pin as output
OnTime = times*(d1+d2);
Particle.subscribe( "diot/2019/paired/friendfinder/On" , handleSharedEvent );
Particle.subscribe( "diot/2019/paired/friendfinder/Off" , handleSharedEvent2 );
}
void loop()
{
// find out if the button is pushed
// or not by reading from it.
int buttonState = digitalRead( buttonPin );
// delay( 100 );
if( buttonState == LOW )
{
// turn the LED On
digitalWrite( ledPin, HIGH);
if(buttonDown==false){//as long as the button is pushed, the code inside this 'if' will run once
testSolenoid("1");
onOrNot=!onOrNot;
LastClick=millis();
uint32_t c = lightstrip.Color(255 , 0, 0 );
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, c );
lightstrip.show();
// delay(100);
}
buttonDown=true;
}
}else{
// otherwise
// turn the LED Off
digitalWrite( ledPin, LOW);
buttonDown=false;
}
if(onOrNot){
digitalWrite( ledPin, HIGH);
publishMyEvent_On();//tell the other board that the friend using this board is available
doIt( times, d1, d2 );
}else{
digitalWrite( ledPin, LOW);
digitalWrite(solPin, LOW);
publishMyEvent_Off();//tell the other board that the friend using this board is available
uint32_t off = lightstrip.Color( 0, 0, 0 );
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, off );
lightstrip.show();
// delay( 100);
}
}
}
void doIt( int times, int d1, int d2 ){
/*
for( int i = 0; i < times; i++ )
{
digitalWrite(solPin, HIGH);
delay( d1 ) ;
digitalWrite(solPin, LOW);
delay( d2 );
}
*/
int now=millis();
if(now==LastClick+d1){
digitalWrite(solPin, HIGH);
}else if(now==LastClick+d1+d2){
digitalWrite(solPin, LOW);
LastClick=now;
}
}
int testSolenoid( String command ){
shouldDoIt = true;
return 1;
}
int setTimes( String command ){
Serial.println( "setTimes: " + command );
int value = command.toInt();
if( value < 0 ) return -1;
if( value > 20 ) return -1;
times = constrain( value, 0, 20 );
return 1;
}
int setDelayOne( String command ){
Serial.println( "setDelayOne: " + command );
int value = command.toInt();
if( value < 10 ) return -1;
if( value > 20000 ) return -1;
d1 = constrain( value, 0, 20000 );
return 1;
}
int setDelayTwo( String command ){
Serial.println( "setDelayTwo: " + command );
int value = command.toInt();
if( value < 10 ) return -1;
if( value > 20000 ) return -1;
d2 = constrain( value, 0, 20000 );
return 1;
}
void publishMyEvent_On()
{
// 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 = "diot/2019/paired/friendfinder/On" + 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();
}
}
void publishMyEvent_Off()
{
if( lastPublishedAt + publishAfter < millis() )
{
String eventName = "diot/2019/paired/friendfinder/Off" + System.deviceID();
Particle.publish( eventName, "data goes here" );
lastPublishedAt = millis();
}
}
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;
}else{
if(onOrNot){//when this board is told by another board that the friend is availbale and this board is also available, turn on the lightstrip in purple
uint32_t c = lightstrip.Color( 255, 0, 255 );
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, c );
lightstrip.show();
// delay(100);
}
}
}
// otherwise do your stuff to respond to
// the paired device here
//motorOn = true;
}
void handleSharedEvent2(const char *event, const char *data)
{
String eventName = String( event ); // convert to a string object
String deviceID = System.deviceID();
if( eventName.indexOf( deviceID ) != -1 ){
return;
}else{
if(onOrNot){//when this board is told by another board that the friend is unavailbale and this board is still available, turn the lightstrip to default(red or blue)
uint32_t c = lightstrip.Color( 255, 0, 0 );
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, c );
lightstrip.show();
// delay(100);
}
}
}
}
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. .