Hi, So i have been working on an alarm system with arduino includes servo+ultrasonic sensor+lcd+keypad
the code is almost done but i can't make it work right so I am going to put the whole code
the problem is in the for loop if i remove it everything works great (except the servo) but when i add it
only the servo works, the lcd starts but i can't use the keypad. HELP.
#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Start, End;
int Trigger1=A2;
int Echo1=A1;
int Trigger2=A4;
int Echo2=A3;
int BUZZ=A5;
int distance1, distance2, pos;
long duration1,duration2;
int distance, Distanceinitial1, Distanceinitial2, i;
int Distancenow1, Distancenow2;
int lcdOff =0;
String password="8426";
String Passwordtemp;
boolean active = false;
boolean est_active;
boolean activeAlarm = false;
boolean alarmActive = false;
boolean Passwordcorrect;
boolean passChangeMode = false;
boolean passChange = false;
unsigned long start;
unsigned long Millis;
unsigned long periode = 28800000;
char key;
const byte ligne = 4;
const byte col = 3;
char keys[ligne][col] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte Pinligne[ligne] = {10, 9, 8, 1};
byte Pincol[col] = {13, 12, 11};
Keypad pad = Keypad( makeKeymap(keys), Pinligne, Pincol, ligne, col);
Servo servo;
void setup() {
servo.attach(A0);
pinMode(BUZZ,OUTPUT);
pinMode(Trigger1,OUTPUT);
pinMode(Echo1,INPUT);
pinMode(Trigger2,OUTPUT);
pinMode(Echo2,INPUT);
lcd.begin(16, 2);
start = millis();
};
void loop() {
if (activeAlarm) {
lcd.clear();
int countdown = 9;
while (countdown != 0) {
lcd.setCursor(0,0);
lcd.print(countdown);
countdown--;
tone(BUZZ, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Active!");
Distanceinitial1 = Distance1();
Distanceinitial2 = Distance2();
activeAlarm = false;
alarmActive = true;
}
if (alarmActive == true){
Millis = millis();
if (Millis - start >= periode)
{
Distancenow1 = Distance1() + 10;
Distancenow2 = Distance2() + 10;
if ( Distancenow1 < Distanceinitial1 || Distancenow2 < Distanceinitial2) {
tone(BUZZ, 1000);
lcd.clear();
Passwordvalide();}
}start = Millis;
}
if (!alarmActive) {
if (lcdOff == 0 ){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("1-Active");
lcd.setCursor(0,1);
lcd.print("2-Timer 3-change");
lcdOff = 1;
}
key = pad.getKey();
if (key =='1'){
tone(BUZZ, 1000, 200);
activeAlarm = true;
}
else if (key =='2'){
int x, y;
tone(BUZZ, 2000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Timer");
lcd.setCursor(0,1);
key = pad.getKey();
if (key != NO_KEY){
if (key == '0' || key == '1' || key == '2' || key == '3' ||
key == '4' || key == '5' || key == '6' || key == '7' ||
key == '8' || key == '9' ) {
y += key;
lcd.setCursor(x,1);
lcd.print("*");
x++;
}
}periode = y*3.6*1000000;
tone(BUZZ, 1000, 200);
activeAlarm = true;
}
else if (key =='3'){
lcd.clear();
int i=1;
tone(BUZZ, 2000, 100);
Passwordtemp = "";
lcd.setCursor(0,0);
lcd.print("Password actuel");
lcd.setCursor(0,1);
lcd.print(">");
passChangeMode = true;
passChange = true;
while(passChange) {
key = pad.getKey();
if (key != NO_KEY){
if (key == '0' || key == '1' || key == '2' || key == '3' ||
key == '4' || key == '5' || key == '6' || key == '7' ||
key == '8' || key == '9' ) {
Passwordtemp += key;
lcd.setCursor(i,1);
lcd.print("*");
i++;
tone(BUZZ, 2000, 100);
}
}
if (i > 5 || key == '#') {
Passwordtemp = "";
i=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password actuel");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( key == '*') {
i=1;
tone(BUZZ, 2000, 100);
if (password == Passwordtemp) {
Passwordtemp="";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nouveau Password");
lcd.setCursor(0,1);
lcd.print(">");
while(passChangeMode) {
key = pad.getKey();
if (key != NO_KEY){
if (key == '0' || key == '1' || key == '2' || key == '3' ||
key == '4' || key == '5' || key == '6' || key == '7' ||
key == '8' || key == '9' ) {
Passwordtemp += key;
lcd.setCursor(i,1);
lcd.print("*");
i++;
tone(BUZZ, 2000, 100);
}
}
if (i > 5 || key == '#') {
Passwordtemp = "";
i=1;
tone(BUZZ, 2000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nouveau Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( key == '*') {
i=1;
tone(BUZZ, 2000, 100);
password = Passwordtemp;
passChangeMode = false;
passChange = false;
lcdOff = 0;
}
}
}
}
}
}}
for(pos=0;pos<=180;pos++){
servo.write(pos);
delay(50);
digitalWrite(Trigger1, LOW);
delayMicroseconds(2);
digitalWrite(Trigger1, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger1, LOW);
duration1 = pulseIn(Echo1, HIGH);
digitalWrite(Trigger2, LOW);
delayMicroseconds(2);
digitalWrite(Trigger2, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger2, LOW);
duration2 = pulseIn(Echo2, HIGH);
distance1 = duration1*0.034/2;
distance2 = duration2*0.034/2;}
for(pos=180;pos>=0;pos--){
servo.write(pos);
delay(50);
digitalWrite(Trigger1, LOW);
delayMicroseconds(2);
digitalWrite(Trigger1, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger1, LOW);
duration1 = pulseIn(Echo1, HIGH);
digitalWrite(Trigger2, LOW);
delayMicroseconds(2);
digitalWrite(Trigger2, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger2, LOW);
duration2 = pulseIn(Echo2, HIGH);
distance1 = duration1*0.034/2;
distance2 = duration2*0.034/2;
}}
void Passwordvalide() {
int k=5;
Passwordtemp = "";
active = true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
while(active) {
key = pad.getKey();
if (key != NO_KEY){
if (key == '0' || key == '1' || key == '2' || key == '3' ||
key == '4' || key == '5' || key == '6' || key == '7' ||
key == '8' || key == '9' ) {
Passwordtemp += key;
lcd.setCursor(k,1);
lcd.print("*");
k++;
}
}
if (key == '#') {
Passwordtemp = "";
k=5;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
}
if ( key == '*') {
if ( Passwordtemp == password ) {
active = false;
alarmActive = false;
noTone(BUZZ);
lcdOff = 0;
}
else if (Passwordtemp != password) {
lcd.setCursor(0,1);
lcd.print("INVALIDE");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
}
}
}
}