edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Alex Glow
Published

NERD ALERT! // Smart Doorbell with Arduino

We needed a doorbell for our office's freight elevator.

BeginnerFull instructions provided1 hour9,868
NERD ALERT! // Smart Doorbell with Arduino

Things used in this project

Story

Read more

Code

NERD ALERT doorbell code

C/C++
Cobbled together from a couple of Seeed Grove kit examples. Extremely annoying.
/*
  Cobbled together from Grove_Buzzer and Grove_RGB_Backlit_LCD examples
  in the Seeed Grove Kit repo:
  https://github.com/Seeed-Studio/Sketchbook_Starter_Kit_V2.0

  See the full project at https://www.hackster.io/glowascii/nerd-alert-e31fc2
*/

#include <Wire.h>
#include "rgb_lcd.h"

rgb_lcd lcd;
const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

int speakerPin = 3;          // Grove Buzzer connect to D3
int length = 2;             // the number of notes
char notes[] = "ca"; // a space represents a rest
int beats[] = { 4, 4 };
int tempo = 50;

const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

void playTone(int tone, int duration) {
    for (long i = 0; i < duration * 1000L; i += tone * 2) {
        digitalWrite(speakerPin, HIGH);
        delayMicroseconds(tone);
        digitalWrite(speakerPin, LOW);
        delayMicroseconds(tone);
    }
}

void playNote(char note, int duration) {
    char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
    int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

    // play the tone corresponding to the note name
    for (int i = 0; i < 8; i++) {
        if (names[i] == note) {
            playTone(tones[i], duration);
        }
    }
}

void setup() 
{
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin, INPUT_PULLUP);
    
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    lcd.setRGB(0, 0, 0);

    pinMode(speakerPin, OUTPUT);
}

void loop() 
{
    buttonState = digitalRead(buttonPin);

    if (buttonState == LOW) {
      lcd.setRGB(colorR, colorG, colorB);
      lcd.print("NERD ALERT! ");
      for (int i = 0; i < length; i++) {
        if (notes[i] == ' ')
          {
            delay(beats[i] * tempo); // rest
          }
        else
          {
            playNote(notes[i], beats[i] * tempo);
          }
        // pause between notes
        delay(tempo / 2);
        }
    } else {
      lcd.setRGB(0, 0, 0);
      lcd.clear();
    }
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

Credits

Alex Glow
154 projects • 1645 followers
Hackster Pro Partner; the Hackster team's resident Hardware Nerd for 10 years. Robots, music, EEG, wearables, languages; FIRST Robotics kid.

Comments