Ciao a tutti,
Tempo fa ho realizzato un progetto, un allarme con arduino.
l'arduino è all interno di una centralina a parete, da questa partono dei cavi che vanno a finire su una tastiera, su dei led, su un burrez, su dei sensori , e una sirena
il funzionamento è il seguente quando l'utente inserisce il codice sulla tastiera il led verde si accende e il buzzer fa un tipo di suono
altrimenti succede l'opposto si accende il led rosso e il buzzer fa un altro suono.
se si inserisce il codice corretto e si apre la porta l'allarme non suona altrimenti, se si apre la porta senza mettere il codice corretto l'allarme suona
tutto questo l'ho programmato, apparte lultimo passaggio,
a disposizione ho una sirena da 12v e sull arduino ho solo pin analogici, adesso mi chiedo come faccio programmare l'arduino in modo che quando il sensore è sbloccato l'allarme suoni ?? ![]()
adesso posto il codice cosi potete vedere quello che ho realizzato:
#include <Keypad.h>
#define LED 12
int sirena = 14; // LED on pin 5
int sensore = 15;
boolean running = false;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'D','C','B','A'},
{'#','9','6','3'},
{'0','8','5','2'},
{'*','7','4','1'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // create an instance of the keypad
// pin assignments
int speakerOut = 11;
int redLedpin = 12;
int greenLedpin = 10;
// variables
char* inputString = "0000"; // holds the user input
char password[5] = "123D"; // holds the default correct password that can later be changed
int curpos = 0; // cursor position
int locked = 1; // status of the system: if locked = 1, if unlocked = 0
void setup(){
pinMode(14,OUTPUT); //SIRENA
pinMode(15,INPUT); //SENSORE
digitalWrite(sensore, HIGH);
Serial.begin(9600);
pinMode(speakerOut, OUTPUT);
pinMode(redLedpin, OUTPUT);
pinMode(greenLedpin, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop(){
if (digitalRead(sensore) == LOW)
{ // switch is pressed - pullup keeps pin high normally
running = !running;
digitalWrite(sirena, running);
}
// RED LED if locked
if(locked ==1)
{
digitalWrite(redLedpin, HIGH);
digitalWrite(greenLedpin, LOW);
digitalWrite(LED, HIGH);
}
// GREEN LED if unlocked
if(locked ==0)
{
digitalWrite(redLedpin, LOW);
digitalWrite(greenLedpin, HIGH);
digitalWrite(LED, LOW);
}
// receive input from keypad
char key = keypad.getKey();
if (key != NO_KEY){ // only be bothered to do something if a key was pressed
Serial.println(key);
playKeyTone(); // play a beep to acknowledge that key pressed
switch(key) // look for the special keys to initiate an event
{
case '#':
parseString(); // try unlock routine
curpos = 0;
break;
case '*': // lock the system
locked = 1;
clearPassword(); // clear the password so that user needs to enter 4 digits again before unlocking
curpos = 0; // set cursor back to start of 4 digits
}
if(key != '*' && key != '#' && key != 'C')
{
inputString[curpos] = key; // if the key was none of the above three special ones, add the digit to the password string
curpos = curpos + 1; // move cursor to next position
}
if(curpos > 3) // if the cursor has reached the end of 4 digits, return it to the start
{
curpos = 0;
}
// Serial.println(inputString);
}
}
void playKeyTone(){ // beeping sound for key press
int elapsedtime = 0;
while (elapsedtime < 100)
{
digitalWrite(speakerOut,HIGH);
delayMicroseconds(500);
digitalWrite(speakerOut, LOW);
delayMicroseconds(500);
elapsedtime++;
}
}
void playWarningTone(){ // long beep for wrong password
int elapsedtime = 0;
while (elapsedtime < 200)
{
digitalWrite(speakerOut,HIGH);
delayMicroseconds(1000);
digitalWrite(speakerOut, LOW);
delayMicroseconds(1000);
elapsedtime++;
}
}
void playSuccessTone(){ // short beep for correct password
int elapsedtime = 0;
while (elapsedtime < 10)
{
digitalWrite(speakerOut,HIGH);
delayMicroseconds(2000);
digitalWrite(speakerOut, LOW);
delayMicroseconds(1000);
digitalWrite(speakerOut,HIGH);
delayMicroseconds(2000);
digitalWrite(speakerOut, LOW);
delayMicroseconds(3000);
elapsedtime++;
}
}
void parseString() // parse password to see if it matches
{
Serial.println("PARSING INPUT STRING");
Serial.println(inputString);
if(matchString(password, inputString) == 1) // did they match?
{
Serial.println("UNLOCKED");
locked = 0; // correct code entered, unlock system
playSuccessTone();
return;
}
else
playWarningTone(); // nope, wrong password
}
void changePassword()
{
Serial.println("PASSWORD CHANGE MODE");
if(locked == 0) // only get into this mode if unlocked! (otherwise anyone can change password without knowing correct one! that would be dumb!)
{
digitalWrite(redLedpin, HIGH); // turn on RED LED
digitalWrite(greenLedpin, HIGH); // turn on GREEN LED
char pkey = NO_KEY;
int c = 0;
while(c < 4)
{
pkey = keypad.getKey(); // get new password 4 digits
if(pkey != NO_KEY)
{
playKeyTone();
password[c] = pkey; // store it in the password string
Serial.println(pkey);
c = c + 1;
}
}
Serial.println("NEW PASSWORD:");
Serial.println(password);
}
}
int matchString(char* string1, char*string2) // function to match two strings, returns 1 if they match, 0 if they dont
{
for(int i=0; i < 5; i++)
{
if(string1[i] != string2[i])
{
return 0;
}
}
return 1;
}
void clearPassword() // clears the entered password after system is locked (so person can't just press # to unlock, needs to enter 4 digit code again!)
{
for(int a=0; a < 4; a++)
{
inputString[a] = '0';
}
}