#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(2, 3, 4, 5, 11, 12);// initialize the library with the numbers of the interface pins
int pirPin = 7; //Variable that will receive the digital signal from the sensor
int LEDgreen = 6; //LED addressed to the digital pin 6
int speaker = 8; //speaker addressed to the digital pin 7
int buttonPin = 9; // the pin that the pushbutton is attached to
void setup(){
pinMode(buttonPin, INPUT);// Initialize the button pin as an input:
pinMode(pirPin, INPUT); // declares variable pirPin as an input
pinMode(LEDgreen, OUTPUT); // declares variable LEDgreen as an output
pinMode(speaker, OUTPUT); // declares variable speaker as an output
lcd.begin(16, 2);
// turn on the cursor:
lcd.cursor();
Serial.begin(9600);
}
void loop(){
int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
if(buttonState == LOW){
if(digitalRead(pirPin) == LOW){
/*The LED Blinks once with the command digitalWrite
the LED keeps turned on after blink during the motion cycle*/
digitalWrite(LEDgreen, HIGH);
delay(1000);
digitalWrite(LEDgreen, LOW);
delay(500);
digitalWrite(LEDgreen, HIGH);
/*speaker plays frequency, the number 7 is the digital pin,
the second number is the frequency played and the third number is the sound duration
we repeated this command with different frequency and varying the time in order to create a
pleasant sound */
tone(4,261,300);
delay(200);
tone(4,329,300);
delay(200);
tone(4,392,600);
delay(200);
tone(4,329,300);
delay(200);
tone(4,392,300);
delay(200);
tone(4,493,600);
delay(200);
tone(4,392,300);
delay(200);
tone(4,493,300);
delay(200);
tone(4,294,600);
delay(200);
lcd.setCursor(0,0);
lcd.print(" Motion "); // Print a message to the LCD.
lcd.setCursor(0,1);
lcd.print(" Detected ");
} else if(digitalRead(pirPin) == HIGH) {
digitalWrite(LEDgreen, LOW); // Turn off the LED in the case where no motion is detected
lcd.setCursor(0,0);
lcd.print("No Motion ");
lcd.setCursor(0,1);
lcd.print(" Detected ");
}
} else if(buttonState == HIGH) {
if(digitalRead(pirPin) == LOW) {
digitalWrite(LEDgreen, HIGH);
delay(1000);
digitalWrite(LEDgreen, LOW);
delay(500);
digitalWrite(LEDgreen, HIGH);
tone(4,261,300);
delay(200);
tone(4,329,300);
delay(200);
tone(4,392,600);
delay(200);
tone(4,329,300);
delay(200);
tone(4,392,300);
delay(200);
tone(4,493,600);
delay(200);
tone(4,392,300);
lcd.setCursor(0,0);
lcd.print(" Motion ");
lcd.setCursor(0,1);
lcd.print(" Detected & door ");
} else if(digitalRead(pirPin) == HIGH) {
digitalWrite(LEDgreen, LOW);
// Turn off the LED in the case where no motion is detected
tone(4,329,300);
delay(200);
tone(4,392,300);
delay(200);
tone(4,493,600);
delay(200);
tone(4,392,300);
lcd.setCursor(0,0);
lcd.print("No Motion ");
lcd.setCursor(0,1);
lcd.print(" Detected but door ");
}
}
delay(2000); // wait for 1 seconds before compare again
}
-
3I think you forgot to ask a question there...Majenko– Majenko2016-01-06 20:58:35 +00:00Commented Jan 6, 2016 at 20:58
1 Answer
This is a summary of your basic loop logic:
void loop() {
int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
if (buttonState == LOW) {
if (digitalRead(pirPin) == LOW) {
// buttonState is LOW
// pirPin is recently read as LOW
// ... Motion Detected ...
}
else if (digitalRead(pirPin) == HIGH) {
// buttonState is LOW
// pirPin is recently read as HIGH
// ... No Motion Detected ...
}
}
else if (buttonState == HIGH) {
if (digitalRead(pirPin) == LOW) {
// buttonState is HIGH
// pirPin is recently read as LOW
// ... Motion Detected & Door ...
}
else if (digitalRead(pirPin) == HIGH) {
// buttonState is HIGH
// pirPin is recently read as HIGH
// ... No Motion Detected but Door ...
}
}
delay(2000); // wait for 1 seconds before compare again
}
However, this is bad code:
if (digitalRead(pirPin) == LOW) {
// ... Motion Detected ...
}
else if (digitalRead(pirPin) == HIGH) {
// ... No Motion Detected ...
}
because each time the digitalRead(..) function is called, it can give a different value. So even though your intention is clear, this code won't work the way you expect. Consider what happens if the first digitalRead(pirPin) is high, so digitalRead(pirPin)==LOW is false, but then the second digitalRead(pirPin) is low, so digitalRead(pirPin)==HIGH is also false. Then neither block of code runs. Instead, use:
if (digitalRead(pirPin) == LOW) {
// ... Motion Detected ...
}
else {
// ... No Motion Detected ...
}
Or since you already sampled it at the start of the loop with int pirVal = digitalRead(pirPin);, simplify the logic as:
void loop() {
int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
if (buttonState == LOW) {
if (pirVal == LOW) {
// buttonState is LOW
// pirPin is LOW
// ... Motion Detected ...
}
else {
// buttonState is LOW
// pirPin is HIGH
// ... No Motion Detected ...
}
}
else {
// buttonState is HIGH
if (pirVal == LOW) {
// buttonState is HIGH
// pirPin is LOW
// ... Motion Detected & Door ...
}
else {
// buttonState is HIGH
// pirPin is HIGH
// ... No Motion Detected but Door ...
}
}
delay(2000); // wait for 1 seconds before compare again
}
By the way, if you're using the Arduino IDE, you can easily clean up the indentation to make these control structures easier to see, by using Tools | Auto Format.
-
hi thanks MarkU the problem is that my lcd does not work maybe it is the setup or the loop i do not knowlobna amin– lobna amin2016-01-07 10:21:36 +00:00Commented Jan 7, 2016 at 10:21
-
2In future, it would be useful to write a trivial program to confirm that your hardware works before moving on to driving it with new software that might itself not work. A simple "Hello, World!" program to write on the display would tell you very quickly whether you have a hardware or a software error.JRobert– JRobert2016-01-07 16:14:22 +00:00Commented Jan 7, 2016 at 16:14
-
1The standard "hello world" example for LCD display is in the Arduino IDE under File | Examples | LiquidCrystal | HelloWorld. Note that your example code uses a different pinout,
LiquidCrystal lcd(2, 3, 4, 5, 11, 12)instead of the normal configurationLiquidCrystal lcd(12, 11, 5, 4, 3, 2). Is it possible that your LCD connections are not what you intended?MarkU– MarkU2016-01-08 01:01:09 +00:00Commented Jan 8, 2016 at 1:01 -
hi thanks a lot MarkU it is a software error it was the configuration LiquidCrystal lcd it is working now :)lobna amin– lobna amin2016-01-08 08:37:55 +00:00Commented Jan 8, 2016 at 8:37
-
The code
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);defines which arduino pins are connected to the LCD hardware -- RS=12, Enable=11, D4=5, D5=4, D6=3, D7=2; with R/W grounded. Connect your LCD exactly this way and run the HelloWorld example, to verify that your LCD hardware is working. Once you get that working, then fix your code to use the same configurationlcd(12, 11, 5, 4, 3, 2)instead of what you had before,lcd(2, 3, 4, 5, 11, 12). You've got the same pins but in a different order, and I assume your LCD is connected with the normal connections.MarkU– MarkU2016-01-08 08:49:41 +00:00Commented Jan 8, 2016 at 8:49