Back to Parent

long lastPublishedAt = 0;
int publishAfter = 10000;


int speakerPin = D3;
int buttonPin = D2;
int val;

int handled = 0;

int C = 262;
int D = 294;
int E = 330;
int F = 349;
int G = 392;
int A = 440;
int B = 494;
int highC = 540;

int melody[] = {C, C, D, C, F, E, C, C, D, C, G, F, C, C, highC, A, F, E, D,
                B, B, A, F, G, F};
int noteDurations[] = {16/3, 16, 4, 4, 4, 2, 16/3, 16, 4, 4, 4, 2, 16/3, 16, 4, 4, 4, 4, 4, 16/3, 16,
                        4, 4, 4, 2}; //  4 = 1/4 note, 8 = 1/8 note, etc.
int numNotes = arraySize(melody);

void setup() {
    pinMode(buttonPin, INPUT_PULLUP); 
    Particle.subscribe("diotZiyu", handleSharedEvent);
    Particle.subscribe("diotHongyu", handleSharedEvent);
    Particle.variable("Handled", handled);
    
}

void loop() {
    
    val = digitalRead(buttonPin);
    
    if (val == LOW){ // doorbell is pushed
        publishEvent();
        // playHappyBirthday();
        delay(2500);
    }
    
    delay(100);
}

void publishEvent() {
    if(lastPublishedAt + publishAfter < millis() ){
        String eventName = "diotYJ";
        Particle.publish(eventName, "data");
        
        lastPublishedAt = millis();
    }
}

void handleSharedEvent(const char *event, const char *data){
    String eventName = String(event);
    String deviceID = System.deviceID();
    handled = 1;
    if (eventName.indexOf("diotYJ") != -1) { // i.e. it's a self-published event
        return;        
    } else{
        playHappyBirthday();
    }
    
}
Click to Expand

Content Rating

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

0