使用パーツ
- LED
- LCD RGB Backlight
- Buzzer
- RIP Motion Sensor
- Speaker
#include "rgb_lcd.h"
#define PIR_MOTION_SENSOR 2 // Pin D2
#define LED 4 // Pin D4
#define BUZZER 5 // Pin D5
#define SPEAKER 6 // Pin D6
rgb_lcd lcd;
int counter = 0; // 32bit Max 2147483647
int lastValue = LOW;
char scount[17];
float gLastMtime = 0;
float gLastSound = 0;
int flagAlert = 0;
void kLcdoutDec(int i)
{
sprintf(scount, "%14d", i);
lcd.setCursor(0, 1);
lcd.print(scount);
}
void beep() {
int i;
for (i = 0; i < 1; i++)
{
digitalWrite(BUZZER, HIGH);
delay(4);
digitalWrite(BUZZER, LOW);
delay(1);
}
}
void sound()
{
int idx = 0;
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 400; i++)
{
digitalWrite(SPEAKER, LOW);
delayMicroseconds(500 - i);
digitalWrite(SPEAKER, HIGH);
delayMicroseconds(500 - i);
}
delay(200);
}
digitalWrite(SPEAKER, HIGH);
}
void LedOn()
{
digitalWrite(LED, HIGH);
}
void LedOff()
{
digitalWrite(LED, LOW);
}
void setup()
{
pinMode(PIR_MOTION_SENSOR, INPUT);
pinMode(LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(SPEAKER, OUTPUT);
digitalWrite(SPEAKER, HIGH);
lcd.begin(16, 2);
lcd.clear();
lcd.print("Work supervisor ");
lcd.setRGB(0, 128, 0);
beep();
// delay(1000);
// sound();
}
void loop()
{
unsigned long mtime = millis();
int sensorValue = digitalRead(PIR_MOTION_SENSOR);
if (sensorValue == HIGH) {
LedOn();
counter = 0;
flagAlert = 0;
lcd.setRGB(0, 128, 0);
if (lastValue != sensorValue) {
int nondet = (int)((mtime - gLastMtime) / 1000);
if (nondet > 10) {
kLcdoutDec(nondet);
beep();
}
gLastMtime = mtime;
}
}
else {
LedOff();
if(mtime - gLastMtime > 10000) {
flagAlert = 1;
lcd.setRGB(128, 0, 0);
}
if(flagAlert == 1 && ((mtime - gLastSound) > 2300)) {
gLastSound = mtime;
sound();
}
}
lastValue = sensorValue;
if (++counter >= 1000000000)
counter = 0;
}