Advertisement
ZulRocky

Modul 7 PIR SENSOR

Feb 25th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <LiquidCrystal_SR_LCD3.h>
  2. const int PIN_LCD_STROBE = 2;
  3. const int PIN_LCD_DATA = 3;
  4. const int PIN_LCD_CLOCK = 4;
  5. LiquidCrystal_SR_LCD3 lcd(PIN_LCD_DATA, PIN_LCD_CLOCK, PIN_LCD_STROBE);
  6.  
  7. int ledPin = A5; // choose the pin for the LED
  8. int inputPin = 12; // choose the input pin (for PIR sensor)
  9. int pirState = LOW; // we start, assuming no motion detected
  10. int val = 0; // variable for reading the pin status
  11. int button=7, push=0, ok=0, on=0, menu=0;
  12. int buzzer=A0;
  13.  
  14. unsigned long turnoff;
  15.  
  16. void setup() {
  17. pinMode(ledPin, OUTPUT); // declare LED as output
  18. pinMode(inputPin, INPUT); // declare sensor as input
  19. pinMode(button, INPUT);
  20. pinMode(buzzer, OUTPUT);
  21. lcd.begin(16,2);
  22. lcd.home();
  23.  
  24. Serial.begin(9600);
  25. }
  26.  
  27. void loop(){
  28. Serial.print(" BUTTON : ");
  29. Serial.println(digitalRead(button));
  30. while(digitalRead(button)==LOW){
  31. if(push==0 && ok==0){
  32. push=1;
  33. }else if(push==1 && ok==1){
  34. push=2;
  35. }else if(push==2 && ok==2){
  36. push=3;
  37. }else if(push==3 && ok==3){
  38. push=1;
  39. }else if(push==4 && ok==4){
  40. push=1;
  41. }
  42. }
  43. val = digitalRead(inputPin); // read input value
  44.  
  45. if(push==0){
  46. lcd.clear();
  47. ok=0;
  48. }else if(push==1 || push==2 || push==3){
  49. ok=1;
  50. if(push==2){
  51. ok=2;
  52. }else if(push==3){
  53. ok=3;
  54. }
  55. lcd.clear();
  56. lcd.setCursor(0,0);
  57. if(push==1){
  58. lcd.print("Menu 1");
  59. }else if(push==2){
  60. lcd.print("Menu 2");
  61. }else if(push==3){
  62. lcd.print("Menu 3");
  63. }
  64. if((turnoff+3000) < millis() && on==1){
  65. val=LOW;
  66. on=0;
  67. }
  68. if (val == HIGH) {
  69. lcd.setCursor(0,1);
  70. lcd.print(" Motion Detected! ");
  71. if (pirState == LOW && on==0) {
  72. // we have just turned on
  73. Serial.println("Motion detected!");
  74. // We only want to print on the output change, not state
  75. pirState = HIGH;
  76. if(on==0){
  77. turnoff = millis();
  78. }
  79. on=1;
  80. if(push==2){
  81. lcd.setCursor(8,0);
  82. lcd.print("buz:ON");
  83. analogWrite(buzzer, 255);
  84. }
  85. }
  86. if(push==3){
  87. for(int i=0; i<5; i++){
  88. digitalWrite(ledPin, HIGH);
  89. delay(200);
  90. digitalWrite(ledPin, LOW);
  91. delay(200);
  92. }
  93. }else{
  94. digitalWrite(ledPin, HIGH); // turn LED ON
  95. }
  96. } else {
  97. digitalWrite(ledPin, LOW); // turn LED OFF
  98. lcd.setCursor(0,1);
  99. lcd.print(" Motion ended! ");
  100. if (pirState == HIGH){
  101. // we have just turned of
  102. Serial.println("Motion ended!");
  103. // We only want to print on the output change, not state
  104.  
  105. pirState = LOW;
  106. lcd.setCursor(8,0);
  107. lcd.print("buz:OFF");
  108. analogWrite(buzzer, 0);
  109. }
  110. }
  111. }else if(push==4){
  112. ok=4;
  113. }
  114.  
  115. delay(200);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement