#include <Keypad.h>
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
const byte ROWS = 4; // Four rows
const byte COLUMNS = 3; // Three columns
char keys[ROWS][COLUMNS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_ROWS[ROWS] = { 4, 13, 12, 11 };
byte pin_COLUMNS[COLUMNS] = { 10, 8, 7 };
char digit1;
char digit2;
char digit3;
char digit4;
char enter;
int counter = 1;
Keypad keypad = Keypad(makeKeymap(keys), pin_ROWS , pin_COLUMNS , ROWS , COLUMNS );
void setup()
{
Serial.begin(9600);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(9, OUTPUT);
}
void loop()
{
int x = 0.01723 * readUltrasonicDistance(3, 2);
char key = keypad.getKey();
while ( x < 30) {
tone(9, 30);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay (100);
if (key) {
if (counter == 1) {digit1 = key; }
if (counter == 2) {digit2 = key;}
if (counter == 3) {digit3 = key;}
if (counter == 4) {digit4 = key;}
if (counter == 5) {enter = key;}
counter ++;
Serial.println(key);}
if (counter == 6) {
switch (enter) {
case'#':
if (digit1 == '2' && digit2 == '0' && digit3 == '2' && digit4 == '1') {
noTone(9);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
Serial.println("right pass");
break;
default:
Serial.print("wrong pass");
}
}
digit1 = NULL;
digit2 = NULL;
digit3 = NULL;
digit4 = NULL;
enter = NULL;
counter = 1;
}
break;
}
noTone(9);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
I am facing a problem with this code, the if condition for the password is performing well and the serial monitor types " right pass" but the other actions did not work as supposed to be (noTone(9); digitalWrite(5, LOW);digitalWrite(6, LOW); )
what could possibly cause such a thing ?
this is like an alarm system, where read the distance via ultrasonic sensor and when it is less than 30 cm, two LEDs and a buzzer go on and you should enter the right pass to stop the alarm.
Aucun commentaire:
Enregistrer un commentaire