const int buttonPin = 5; // the number of the pushbutton pin
const int motorPin = 6; // the number of the LED pin
long lastPublishedAt = 0;
int publishAfter = 10000;
bool gameState = false;
// variables will change:
// variable for reading the pushbutton status
int buttonState = HIGH;
void setup() {
// initialize the LED pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
Particle.subscribe( "diot/2019/smartstudio/start" , handleSharedEvent );
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
publishLampEvent();
digitalWrite(motorPin, LOW);
gameState = false;
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (gameState) {
digitalWrite(motorPin, HIGH);
delay(50);
} else {
// turn LED off:
digitalWrite(motorPin, LOW);
}
}
void publishLampEvent() // Publish function for the ending event
{
// check that it's been 10 secondds since our last publish
if( lastPublishedAt + publishAfter < millis() )
{
String eventName = "diot/2019/smartstudio/lamp" + System.deviceID();
// 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 handleSharedEvent(const char *event, const char *data){ // Only need one handle event function for both events
// Let's check the event name
String eventName = String( event ); // convert to a string object
// 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/start")){
gameState = 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. .