Як зробити просту сигналізацію на Arduino?

У статті хочу описати не новинку, а необхідний пристрій, який допоможе повідомити про проникнення до приміщення.
Для проекту нам знадобиться:
1. KYIV UNO аналог Arduino UNO REV3.
2. Провід тата-мама.
3. Датчик руху PIR HC-SR501
6. Акумуляторний тримач для 2х акумуляторів + дроти для живлення плати.
7. Два акумулятори тип 18650.
8. USB провід для програмування KYIV UNO.
Насамперед збираємо схему і після перевірки правильності підключення сідаємо за написання прошивки для нашого проекту.
Приклад схеми:
У схемі присутні датчик руху, світлодіод і динамік. Для програмування необхідні бібліотеки: "Arduino.h", "LED.h", "PIR.h", "SpeakerStereo3W.h" їх необхідно знайти, завантажити та інсталювати.
Тестовий код для перевірки підключення:
// Include Libraries
#include "Arduino.h"
#include "LED.h"
#include "PIR.h"
#include "SpeakerStereo3W.h"
// Pin Definitions
#define LEDR_PIN_VIN 5
#define PIR_PIN_SIG 2
#define STEREOSPEAKER_PIN_POS 3
// Global variables and defines
unsigned int StereoSpeakerHoorayLength = 6; // amount of notes in melody
unsigned int StereoSpeakerHoorayMelody[] = {NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5}; // list of notes. List length must match HoorayLength!
unsigned int StereoSpeakerHoorayNoteDurations[] = {8, 8, 8, 4, 8, 4 }; // note durations; 4 = quarter note, 8 = eighth note, etc. List length must match HoorayLength!
// object initialization
LED ledR(LEDR_PIN_VIN);
PIR pir(PIR_PIN_SIG);
SpeakerStereo3W StereoSpeaker(STEREOSPEAKER_PIN_POS);
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");
menuOption = menu();
}
// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
if(menuOption == '1') {
// LED - Basic Red 5mm - Test Code
// The LED will turn on and fade till it is off
for(int i=255 ; i> 0 ; i -= 5)
{
ledR.dim(i); // 1. Dim Led
delay(15); // 2. waits 5 milliseconds (0.5 sec). Change the value in the brackets (500) for a longer or shorter delay in milliseconds.
}
ledR.off(); // 3. turns off
}
else if(menuOption == '2') {
// Infrared PIR Motion Sensor Module - Test Code
bool pirVal = pir.read();
Serial.print(F("Val: ")); Serial.println(pirVal);
}
else if(menuOption == '3') {
// 3W Stereo Speaker - Test Code
// The Speaker will play the Hooray tune
StereoSpeaker.playMelody(StereoSpeakerHoorayLength, StereoSpeakerHoorayMelody, StereoSpeakerHoorayNoteDurations);
delay(500);
}
if (millis() - time0 > timeout)
{
menuOption = menu();
}
}
// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{
Serial.println(F("\nWhich component would you like to test?"));
Serial.println(F("(1) LED - Basic Red 5mm"));
Serial.println(F("(2) Infrared PIR Motion Sensor Module"));
Serial.println(F("(3) 3W Stereo Speaker"));
Serial.println(F("(menu) send anything else or press on board reset button\n"));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == '1')
Serial.println(F("Now Testing LED - Basic Red 5mm"));
else if(c == '2')
Serial.println(F("Now Testing Infrared PIR Motion Sensor Module"));
else if(c == '3')
Serial.println(F("Now Testing 3W Stereo Speaker"));
else
{
Serial.println(F("illegal input!"));
return 0;
}
time0 = millis();
return c;
}
}
}
// ====================================================================
Для роботи схеми у режимі сигналізації необхідно написати свій код та перевірити його роботу. Вдалих експериментів!
В цій категорії немає товарів.