The source code is simple enough; with the basic setup() and loop() functions below:
void setup(){ // put your setup code here, to run once: Serial.begin(9600); shield.begin(); shield.setBrightness(100); pinMode(pin_sensor, INPUT); pinMode(pin_buzzer, OUTPUT); //we define the "detectClaps" function in the sketch attachInterrupt(digitalPinToInterrupt(pin_sensor),detectClaps, RISING); setupRTC();}void loop(){ DateTime realTimeNow = rtc.now(); // ... //always display colour on the matrix, corresponding to time setColourFromTime(realTimeNow); if (hasClapped){ //we have detected two claps, print in "hh:mm" format shield.print(realTimeNow.toString("hh:mm")); }}
The setColourFromTime() function is simply a collection of if-statements to block off portions of the day:
if(hours < morningTime){ // between 0 -> 6am // change from red to whitish blue to wake up}else if (hours > morningTime && hours < afternoonTime){ // 6am -> 4pm // show mainly blue, some white, etc}// etc
Then while we're doing this, the detect claps function is a simple interrupt that will check for two claps in a reasonable time frame. We "debounce" the claps so that any ringing effects aren't misunderstood as a second clap.
//fired any time the audio module detects noisevoid detectClaps(){ unsigned long timeNow = millis(); if (firstClapTime == 0) { firstClapTime = timeNow; return; } unsigned long duration = timeNow - firstClapTime; if (duration > clap_debounce_ms && duration < clap_delay_ms) { //two claps genuine; set clapped flag firstClapTime = 0; hasClapped = true; } else if (duration > clap_delay_ms) { //timeout.. took too long firstClapTime = 0; }}
We have also included a #define TIMELAPSE comment that you can comment out to test what it will look like over the course of a whole day. If you uncomment this define, it will rapidly move through the time in the day and output the time in Serial.