// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D6
#define PIXEL_COUNT 9
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //Lamp
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int direction = 0 ; // direction state, +1, 0 or -1
// 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;
String check = "step0";
int distance = 0;
bool gameState = false;
// DO NOT CHANGE THIS
String sharedEventName = "diot/2019/smartstudio/";
String flagEventName = "diot/2019/smartstudio/flag";
//set up button
int buttonPin = D3;
bool buttonState = HIGH;
int brightness = 20;
void setup()
{
Particle.variable("distance", distance);
Particle.subscribe( "diot/2019/smartstudio/flag", handleSharedEvent );
Particle.subscribe( "diot/2019/smartstudio/lamp", handleSharedEvent ); // Lamp
Particle.variable("checking", check);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
myservo.attach(D2); // attach the servo on the D2 pin to the servo object
myservo.write(25); // test the servo by moving it to 25°
}
void loop()
{
int volts_1 = analogRead(A0);
//int calculated = (6762/(reading-9))-4;
int volts = (3.3*(volts_1-900))/2000; // value from sensor * (5/1024)
int distance = 320/(1+10*volts);
// int distance = 13*pow(volts, -1); // worked out from datasheet graph
uint16_t i;
uint32_t c = strip.Color(219, 101, 255);
uint32_t w = strip.Color(255, 255, 255);
uint32_t d = strip.Color(0,0,0);
uint32_t r = strip.Color(255,0,0);
buttonState = digitalRead( buttonPin );
if(distance<15){
publishEndEvent();
gameState = false;
}
//turn off light, LAMP EVENT
if (buttonState == LOW){
publishFlagEvent();
gameState = false;
for (i=0; i< strip.numPixels() ; i++ ){
strip.setPixelColor(i,d);
}
strip.show();
delay(1000);
}
Particle.publish("volts", String(volts));
Particle.publish("distance", String(distance));
if(gameState){
myservo.write(0); // move servo to 0° - ding!
delay(500); // wait 100 ms
myservo.write(45);
delay(500); // wait 100 ms
for (i=0; i< strip.numPixels() ; i++ ){
strip.setPixelColor(i,c);
}
strip.show();
delay(1000);
}
}
void publishEndEvent()
{
// 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() )
{
String eventName = sharedEventName + "end/" + System.deviceID();
// then we share it out
Particle.publish( eventName, "Congrats!" );
lastPublishedAt = millis();
}
}
void publishFlagEvent() //Flag
{
// 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() )
{
String eventName = sharedEventName + "flag/" + System.deviceID();
// then we share it out
Particle.publish( eventName, "Move to next event" );
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;
}
if(eventName.startsWith("diot/2019/smartstudio/flag")){
gameState = true;
}
// otherwise do your stuff to respond to
// the paired device here
//motorOn = true;
}
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. .