Week 9: Networking

I have used Arduinos to both host a Wifi and also to connect to a Wifi in my week 6 inputs assignments, where I use them to connect different controllers to an online game. However, because it is lame to double-submit assignment and I wanted to play around with cool sensors, I have come up with a particularly useless thing: a earthquake-time adaptable warning system (the useless is in the time-adaptable, not the earthquake warning system). The idea was that if a sensor detects vibration, it checks the local time. If it is early in the morning, maybe it needs to be louder, so it plays one sound, if it is later, the noise levels have changed, so it plays a different sound. I used a knock sensor and a Piezo buzzer. Unfortunately, the knock sensor was not as sensitive as I had hoped (to be fair, I had not tested it under earthquake condition), so one had to truly drop it a couple of times before it made a sound. I also made a website where one could watch which song was going to be on, depending on the time.

#include <TimeLib.h>

#define BUZZER D6
#define KNOCK  D10

// set once before upload
#define START_HOUR 10
#define START_MIN  44

#define REST 0

// notes
#define C4 262
#define D4 294
#define E4 330
#define G4 392
#define A4 440
#define B4 494
#define C5 523
#define D5 587
#define E5 659
#define Fs5 740
#define G5 784
#define Gs5 831
#define A5 880
#define B5 988
#define Ds5 622

// songs
int nggyu_notes[] = {A4, B4, D5, B4, Fs5, Fs5, E5, D5};
int nggyu_len[]   = {8, 8, 4, 8, 4, 8, 4, 2};
int nggyu_bpm = 113;

int baby_notes[] = {C5, C5, C5, E5, G5, REST, G5, G5, G5};
int baby_len[]   = {8, 8, 8, 8, 4, 8, 8, 8, 2};
int baby_bpm = 115;

int nyan_notes[] = {Fs5, Gs5, B5, Fs5, Gs5, Ds5, Fs5, B5};
int nyan_len[]   = {8, 8, 8, 8, 8, 8, 8, 4};
int nyan_bpm = 160;

#define LEN(x) (sizeof(x) / sizeof(x[0]))

void playNote(int freq, int len, int bpm) {
  int ms = (60000 / bpm) * 4 / len;

  if (freq == REST) {
    delay(ms);
    return;
  }

  tone(BUZZER, freq, ms * 0.9);
  delay(ms);
  noTone(BUZZER);
}

void playSong(int *notes, int *dur, int count, int bpm) {
  for (int i = 0; i < count; i++) {
    playNote(notes[i], dur[i], bpm);
  }
}

void setup() {
  pinMode(BUZZER, OUTPUT);
  pinMode(KNOCK, INPUT);

  Serial.begin(9600);

  setTime(START_HOUR, START_MIN, 0, 17, 5, 2026);
}

bool prevKnock = LOW;

void loop() {
  bool knock = digitalRead(KNOCK);

  if (knock && !prevKnock) {
    int h = hour();

    Serial.print("Knock at ");
    Serial.print(h);
    Serial.print(":");
    if (minute() < 10) Serial.print("0");
    Serial.print(minute());
    Serial.print(" → ");

    if (h < 12) {
      Serial.println("Never Gonna Give You Up");
      playSong(nggyu_notes, nggyu_len, LEN(nggyu_notes), nggyu_bpm);
    }
    else if (h < 15) {
      Serial.println("Baby Shark");
      playSong(baby_notes, baby_len, LEN(baby_notes), baby_bpm);
    }
    else {
      Serial.println("Nyan Cat");
      playSong(nyan_notes, nyan_len, LEN(nyan_notes), nyan_bpm);
    }

    delay(600); // simple debounce after playback
  }

  prevKnock = knock;
  delay(10);
}