Created: December 9th, 2021
Project: Happy Birthday Song Receiver / Sender
Objective: Create a connected system with three Particle Argon devices such that each device can send a signal to play 'Happy Birthday' on the other two devices.
Team: Youngjoo Lee, Ziyu Han, Hongyu Mao
Process
We began by writing a Happy Birthday song function to play on a Piezo.
Once we tested that this played the song properly on our individual devices, we used Particle's Publish/Subscribe feature to connect the devices. Each device publishes an event with a unique name when the button on that device is pressed. Each device is also subscribed to the other two devices' events so that when one device's button is pressed, the two remaining devices starts to play the song.
Issues:
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