Years ago(when I was a kid) in China, when you walk into most convenience stores and small shops, an accessory by the door will welcome you by saying “welcome”, and it also serves to notify the store owners that they have customers. I remember when I was a kid, I found it really annoying, because it makes the sound not only when you walk in, but also when you walk out. And if you stand by the door, it will keep ‘welcoming’ you. It’s probably because the accessory sensor only detects for distance, and it’s not smart enough to distinguish when not to make the sound.
Nowadays, I don’t hear it as often, and I’m not sure if it’s because the sensing accessory it too annoying, or simply because people associate this sound with ‘low-end’ stores. But I thought if this sensing accessory could become smarter, maybe it’s not a bad idea to bring it back?
So I added 2 features:
-
- only makes the sound when someone walks in, not out;
- don’t make the sound if the store owner is already by the door or near the door
I initially wanted to use 2 ultrasonic distance sensors(1 to sense the person walking through the door, and another to sense the store owner being nearby the door), but I only had 1 distance sensor, so I used a button to indicate the event of “the owner is nearby the door”.
Input: Button, Photo-resistor, ultrasonic distance sensors
Output: buzzer speaker
I wasn’t able to get the speaker to play “Welcome” in Chinese, but I tried to mimic the tones of the way “Welcome” sounds with these sensing accessories(shoutout to my musically talented friend Michelle).
I used a photoresistor to detect whether the person is going in or out, but this scheme is far from reliable. A real motion sensor can do a much better job than what I have.
#include<Wire.h> // PIN INIT const int SPEAKERPIN = 3; const int TRIGPIN = 4; const int ECHOPIN = 5; const int BUTTONPIN = 7; const int PHOTOPIN = A0; // GLOBAL VARS int distanceThreshold = 4; int prevPhotoVal = 0; int get_distance(){ unsigned long duration; int distance; digitalWrite(TRIGPIN, HIGH); delay(1); digitalWrite(TRIGPIN, LOW); duration = pulseIn(ECHOPIN, HIGH, 20000); distance = duration / 57; // Divide by round-trip microseconds per cm to get cm return distance; } void setup() { Serial.begin(9600); pinMode(SPEAKERPIN, OUTPUT); pinMode(ECHOPIN, INPUT); pinMode(TRIGPIN, OUTPUT); pinMode(PHOTOPIN, INPUT); pinMode(BUTTONPIN, INPUT_PULLUP); } void loop() { int buttonUp = digitalRead(BUTTONPIN); int currPhotoVal = analogRead(PHOTOPIN); int distance = get_distance(); if (distance < distanceThreshold && buttonUp) { if(currPhotoVal > prevPhotoVal) { tone(SPEAKERPIN , 185, 200); delay(200); tone(SPEAKERPIN , 146.83, 200); delay(200); tone(SPEAKERPIN , 185, 200); delay(200); tone(SPEAKERPIN , 146.83, 400); delay(400); } } prevPhotoVal = currPhotoVal; }