Back to Parent

// 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;

int buttonPin = D3;
int speakerPin = D2;

int count = 0;

void setup()
{

  // We'll want to subscribe to an event thats fairly unique


  // From the Particle Docs

  // A subscription works like a prefix filter.

  // If you subscribe to "foo", you will receive any event

  // whose name begins with "foo", including "foo", "fool",

  // "foobar", and "food/indian/sweet-curry-beans".


  // Basically this will match any event that starts with 'db2018/paired/'

  // This is a feature we'll useto figure out if our event comes from

  // this device or another (see publishMyEvent below)

  pinMode(speakerPin, OUTPUT );
  pinMode( buttonPin , INPUT_PULLUP);

  Particle.subscribe(  "diot/2019/paired/yoolqc7000" , handleSharedEvent );

}

void loop()
{
    // publish my event

    // you'll want some more complex stuff here

    int buttValue = digitalRead(buttonPin);
    if (buttValue == LOW) {
        
        publishMyEvent();
        // delay for a bit

        delay(100);
    }
}


void publishMyEvent()
{
  // 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.

  
  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/yoolqc7000" + 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, "90" );
      
        // check that it's been 10 secondds since our last publish

      
      String eventName1 = "diot/2019/paired/qcyool7000" + System.deviceID();
      Particle.publish( eventName1, "1000" );

      // And this will get shared out to all devices using this code


      // we just pubished so capture this.

      lastPublishedAt = millis();
      
      
  }
   count = count + 1;

}

// 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 (count == 3) {
        playPiezo1();
    } 
    if (count == 6) {
        playPiezo2();
    } 
    if (count == 9) {
        playPiezo3();
    } 
    if (count == 12) {
        playPiezo4();
    } 
    else {
        playBeep();
    }
    
    if(count >= 15) {
        count = 0;
    }
    // otherwise do your stuff to respond to

    // the paired device here


    //motorOn = true;


}

void playPiezo1() {

// create an array for the notes in the melody:

//C4,G3,G3,A3,G3,0,B3,C4


// TONES  ==========================================

// Start by defining the relationship between 

//       note, period, &  frequency. 

// #define  c     3830    // 261 Hz 

// #define  d     3400    // 294 Hz 

// #define  e     3038    // 329 Hz 

// #define  f     2864    // 349 Hz 

// #define  g     2550    // 392 Hz 

// #define  a     2272    // 440 Hz 

// #define  b     2028    // 493 Hz 

// #define  C     1912    // 523 Hz 


int melody[] = {415, 370, 330, 247};

// create an array for the duration of notes.

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {4, 4, 4, 2};


    // iterate over the notes of the melody:

    for (int thisNote = 0; thisNote < 4; thisNote++) {

      // to calculate the note duration, take one second

      // divided by the note type.

      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

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

      // to distinguish the notes, set a minimum time between them.

      // the note's duration + 30% seems to work well:

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:

      noTone(speakerPin);
    }
}

void playPiezo2() {

// create an array for the notes in the melody:

//C4,G3,G3,A3,G3,0,B3,C4


// TONES  ==========================================

// Start by defining the relationship between 

//       note, period, &  frequency. 

// #define  c     3830    // 261 Hz 

// #define  d     3400    // 294 Hz 

// #define  e     3038    // 329 Hz 

// #define  f     2864    // 349 Hz 

// #define  g     2550    // 392 Hz 

// #define  a     2272    // 440 Hz 

// #define  b     2028    // 493 Hz 

// #define  C     1912    // 523 Hz 


int melody[] = {330, 415, 370, 247, 330, 370, 415, 330};

// create an array for the duration of notes.

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {4, 4, 4, 2, 4, 4, 4, 2};


    // iterate over the notes of the melody:

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

      // to calculate the note duration, take one second

      // divided by the note type.

      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

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

      // to distinguish the notes, set a minimum time between them.

      // the note's duration + 30% seems to work well:

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:

      noTone(speakerPin);
    }
}

void playPiezo3() {


int melody[] = {415, 247, 370, 247, 247, 370, 415, 330, 415, 370, 330, 247};

// create an array for the duration of notes.

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2};


    // iterate over the notes of the melody:

    for (int thisNote = 0; thisNote < 12; thisNote++) {

      // to calculate the note duration, take one second

      // divided by the note type.

      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

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

      // to distinguish the notes, set a minimum time between them.

      // the note's duration + 30% seems to work well:

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:

      noTone(speakerPin);
    }
}
void playPiezo4() {


int melody[] = {330, 415, 370, 247, 330, 370, 415, 330 ,415, 247, 370, 247, 247, 370, 415, 330, 415, 370, 330, 247};

// create an array for the duration of notes.

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2};


    // iterate over the notes of the melody:

    for (int thisNote = 0; thisNote < 16; thisNote++) {

      // to calculate the note duration, take one second

      // divided by the note type.

      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

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

      // to distinguish the notes, set a minimum time between them.

      // the note's duration + 30% seems to work well:

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:

      noTone(speakerPin);
    }
}

void playBeep() {

// create an array for the notes in the melody:

//C4,G3,G3,A3,G3,0,B3,C4



int melody[] = {415};

// create an array for the duration of notes.

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {4};


    // iterate over the notes of the melody:

    for (int thisNote = 0; thisNote < 1; thisNote++) {

      // to calculate the note duration, take one second

      // divided by the note type.

      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

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

      // to distinguish the notes, set a minimum time between them.

      // the note's duration + 30% seems to work well:

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:

      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