Please check the line 112, where I put this message : //I WOULD LIKE TO PUT HERE A FUNCTION WHICH, IF AFTER 30 SECONDS NO ONE HAS PRESSED ON *, THE PROGRAM RESTART . My program is , if a person or something is detected between 50cm of the ultrasonic sensor, the LCD asks for the password and on the keypad you enter *, and then the password. Everything works fine, but after spending hours trying to find how to do what I mentioned earlier, nothing I tried worked…
/*
Arduino Servo controlled by ultrasonic sensor and keypad and displays infos on lcd
January 4th 2020
PiTech™
*/
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LowPower.h>
#define I2C_ADDR 0x3F // LCD i2c Adress and pins
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(0x3F,16,2);
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
int code = 1590; //The code I used, you can change it
int tot,i1,i2,i3,i4;
//To restart the code if you ever miss the password
char c1,c2,c3,c4;
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char keypressed = myKeypad.getKey();
Servo myservo; // create servo object to control a servo
const int trigPin = 12;
const int echoPin = 13;
unsigned long time;
void setup() {
myservo.attach(11); // attaches the servo on pin 11 to the servo object
}
void loop()
{
// the distance result in centimeters:
long duration, cm;
myservo.write(8);
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(4);
//The smallest unit of time that the Uno and Mega chips can normally clock is 4 microseconds.
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// the condition for the distance
if (cm > 1 && cm < 50)
{
lcd.init();
lcd.backlight();
lcd.home ();
lcd.print("Loading");
delay (250);
lcd.clear();
lcd.print("Loading.");
delay (250);
lcd.clear();
lcd.print("Loading..");
delay (250);
lcd.clear();
lcd.print("Loading...");
delay (250);
lcd.clear();
lcd.print("Press * to enter");
lcd.setCursor(0,1);
lcd.print("code");
keypressed = myKeypad.waitForKey();
//I WOULD LIKE TO PUT HERE A FUNCTION WHICH, IF AFTER 30 SECONDS NO ONE HAS PRESSED ON *, THE PROGRAM RESTART
}
//The getKey fucntion keeps the program runing, as long you didn't press "*" the whole thing bellow wouldn't be triggered
if (keypressed == '*') // and you can use the rest of you're code simply
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code :"); //when the "*" key is pressed you can enter the passcode
keypressed = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above
if (keypressed != NO_KEY)
{
c1 = keypressed;
lcd.setCursor(0, 1);
lcd.print("*");
}
keypressed = myKeypad.waitForKey();
if (keypressed != NO_KEY)
{
c2 = keypressed;
lcd.setCursor(1, 1);
lcd.print("*");
}
keypressed = myKeypad.waitForKey();
if (keypressed != NO_KEY)
{
c3 = keypressed;
lcd.setCursor(2, 1);
lcd.print("*");
}
keypressed = myKeypad.waitForKey();
if (keypressed != NO_KEY)
{
c4 = keypressed;
lcd.setCursor(3, 1);
lcd.print("*");
}
i1=(c1-48)*1000; //the keys pressed are stored into chars
i2=(c2-48)*100;
i3=(c3-48)*10;
i4=c4-48;
tot=i1+i2+i3+i4;
if (tot == code) //if the code is correct you trigger whatever you want here it just print a message on the screen
{
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome, PiTech");
myservo.write(113);
delay (6000);
lcd.clear();
lcd.noBacklight();
myservo.write(8);
}
else //if the code is wrong you get this
{
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Loading");
delay (250);
lcd.clear();
lcd.print("Loading.");
delay (250);
lcd.clear();
lcd.print("Loading..");
delay (250);
lcd.clear();
lcd.print("Loading...");
delay (250);
lcd.clear();
lcd.print("Wrong password !");
delay(3000);
lcd.clear();
lcd.print("Please, try");
lcd.setCursor(0, 1);
lcd.print("again");
delay (2000);
}
}
else {
myservo.write(8); // sets the servo position according to the scaled value
delay(100);
}
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(29);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2; // If this doesn't give a correct answer, you may need to use parentheses to establish the order of operation.
}