Jenny_Skill Dev 5

Made by Yanling Zhang

Created: December 12th, 2021

0

I have created a project to play the song ‘Two Tigers'.

0
Skill Dev 5
zhang yanling - https://youtu.be/1zbtc9XH-xI
0
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, D, E, C, C, D, E, C, E, F, G, E, F, G};
int noteDurations[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1}; //  4 = 1/4 note, 8 = 1/8 note, etc.
int numNotes = arraySize(melody);

void setup() {
    pinMode(buttonPin, INPUT_PULLUP); 
    Particle.subscribe("diotCZ", 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 = "diotJenny";
        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("diotJenny") != -1) { // i.e. it's a self-published event
        return;        
    } else{
        playHappyBirthday();
    }
    
}

void playHappyBirthday(){
    for(int i = 0; i < 14; i ++){
        int noteDuration = 1000/noteDurations[i];
        tone(speakerPin, melody[i], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
        int pauseBetweenNotes = noteDuration * 1.10;
        delay(pauseBetweenNotes);
    // stop the tone playing:
        noTone(speakerPin);
        
    }
}
Click to Expand
x