// 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 );
int solPin = D0;
int buttonPin = D4;
int ledPin = D3;
int d1 = 100; //delay time before the solenoid goes up
int d2 = 1500;//delay time before the solenoid goes down
bool buttonDown=false;//works as a valve to make sure some codes only run for once when the button is pushed
bool refreshtimeOrNot=false;//works as another valve to make sure some codes only run for once when the button is pushed
int LastClick=0;//the last time that the solenoid clicked
bool onOrNot=false;//a variable checking if the other friend is available
bool evenOrNot=true;//a variable checking if the person using this board is available
bool buttonState;//if the button is being pushed
int now=0;//a variable showing the time that the doIt function has been running
long lastPublishedAt = 0;// This value will store the last time we published an event
int publishAfter = 1000;// this is the time delay before we should publish a new event from this device
void setup()
{
lightstrip.begin();
lightstrip.show();
Serial.begin( 9600 );
Particle.variable( "d1", &d1, INT );
Particle.variable( "d2", &d2, INT );
Particle.variable( "even", evenOrNot);
Particle.variable( "onOrNot", onOrNot);
Particle.variable( "buttonState", buttonState);
Particle.variable( "Last", LastClick);
Particle.variable( "now", now);
Particle.function( "setD1", setDelayOne );
Particle.function( "setD2", setDelayTwo );
pinMode(solPin, OUTPUT);
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
pinMode( ledPin , OUTPUT ); // sets pin as output
Particle.subscribe( "diot/2019/paired/friendfinder/On" , handleSharedEvent );
Particle.subscribe( "diot/2019/paired/friendfinder/Off" , handleSharedEvent2 );
}
void loop()
{
buttonState = digitalRead( buttonPin ); // find out if the button is pushed or not by reading from it.
if( buttonState == false )//when the button is pushed down
{
if(buttonDown==false){//as long as the button is pushed, the code inside this 'if' will run once
if(evenOrNot){
publishMyEvent_On();//tell the other board that the friend using this board is available
}else{
publishMyEvent_Off();//tell the other board that the friend using this board is unavailable
}
buttonDown=true;//the valve that makes sure the codes above only run once each time
}
}else{//when the button is not pushed down
buttonDown=false;//reset the valve for the if part above
}
if(onOrNot){//when the other friend is available
if(!evenOrNot)
{//when the person using this board is also available
d2=300;//speed up the solenoid's clicking to strengthen the signal
uint32_t c = lightstrip.Color( 255, 0, 255 );// turn the color of the lightstrip to purple
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, c );
lightstrip.show();
}
}
else
{//when the person using this board is not available
d2=1500;//slow down the solenoid's clicking to make it ambient
uint32_t c = lightstrip.Color(255, 0, 0);//turn the color of the lightstrip back to red
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, c );
lightstrip.show();
}
}
if(refreshtimeOrNot==false)
{//if we just switch to this part, run the following code for once
LastClick=millis();//initialize the timer of the doIt function
refreshtimeOrNot=true;//make sure the codes here only run once each time
}
doIt( d1, d2 );//turn on the solenoid
}
else
{
refreshtimeOrNot=false;//reset the valve for the if part above
LastClick=millis();
digitalWrite(solPin, LOW);//reset the position of the solenoid
uint32_t off = lightstrip.Color( 0, 0, 0 );//kill the lightstrip
for( int i = 0 ; i < lightstrip.numPixels(); i++ ){
lightstrip.setPixelColor( i, off );
lightstrip.show();
}
}
}
void doIt(int d1, int d2 ){//keep the solenoid clicking with intervals d1 and d2
now=millis();//keep the timer running
if((now>LastClick+d1)&&(now<LastClick+d1+d2)){//wait for d1 before the solenoid goes up
digitalWrite(solPin, HIGH);
}else if(now>LastClick+d1+d2){//wait for d2 before the solenoid goes down
digitalWrite(solPin, LOW);
LastClick=now;
}
}
int setDelayOne( String command ){//make sure the d1 value is sitting within a proper range
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 ){//make sure the d2 value is sitting within a proper range
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()//publish event when telling the other friend the person using this board is available
{
// 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 seconds 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" );
evenOrNot=! evenOrNot;
digitalWrite( ledPin, HIGH);
// And this will get shared out to all devices using this code
// we just published so capture this.
lastPublishedAt = millis();
}
}
void publishMyEvent_Off()//publish event when telling the other friend the person using this board is not available
{
if( lastPublishedAt + publishAfter < millis() )
{
String eventName = "diot/2019/paired/friendfinder/Off" + System.deviceID();
Particle.publish( eventName, "data goes there" );
evenOrNot=! evenOrNot;
digitalWrite( ledPin, LOW);
lastPublishedAt = millis();
}
}
void handleSharedEvent(const char *event, const char *data)//when receiving function that the the other friend is available, execute the following codes
{
// 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{
onOrNot=true;
}
// otherwise do your stuff to respond to
// the paired device here
//motorOn = true;
}
void handleSharedEvent2(const char *event, const char *data)//when receiving function that the the other friend is not available, execute the following codes
{
String eventName = String( event ); // convert to a string object
String deviceID = System.deviceID();
if( eventName.indexOf( deviceID ) != -1 ){
return;
}else{
onOrNot=false;
if(!evenOrNot){//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( 0, 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. .