Back to Parent

#define button D3
#define piezo D2


int speakerPin = D2; 
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
int noteDurations[] = {4,8,8,4,4,4,4,4 };


long lastPublishedAt = 0;

int publishAfter = 10000;

void setup()
{

  Particle.subscribe(  "GRP5_SZL" , handleSharedEvent );
  pinMode(button, INPUT_PULLUP);
  pinMode(piezo, OUTPUT);

}

void loop()
{
    if (digitalRead(button)==LOW) {
        publishMyEvent();
    }
    
    delay(100);
}


void publishMyEvent()
{

  if( lastPublishedAt + publishAfter < millis() )
  {
      
      String eventName = "GRP5_SZL" + System.deviceID();

      Particle.publish( eventName, "data goes here" );

      lastPublishedAt = millis();
  }

}

void handleSharedEvent(const char *event, const char *data)
{
    
    String eventName = String( event ); 

    String deviceID = System.deviceID();

    if( eventName.indexOf( deviceID ) != -1 ){

      playNotes();
    }

}

void playNotes()
{
    
    for (int thisNote = 0; thisNote < 8; thisNote++) {

   
      int noteDuration = 1000/noteDurations[thisNote];
      tone(speakerPin, melody[thisNote],noteDuration);

     
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
    
      noTone(speakerPin);
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0