Question: I need help with a tiny Arduino project, everything is done I just need a little help. Hardware: Arduino, LCD, button, breadboard, PIR motion sensor

I need help with a tiny Arduino project, everything is done I just need a little help.

Hardware: Arduino, LCD, button, breadboard, PIR motion sensor & Powertail

Here is what my code supposed to do:

If PIR motion sensor does not detect a motion for 20 seconds, the powertail is supposed to turn off whatever device is connected to it

The LCD is supposed to show the countdown time

If the button on the breadboard is pressed, with every press, it adds 5 seconds to the time

So lets say countdown has reached 9 seconds and you press the button 5 times, it should add 25 seconds.

Please fix this for me because the code is glitchy

-------------------------------------------------------------------

#include //VARS //the time we give the sensor to calibrate (10-60 secs according to the datasheet)

int calibrationTime = 30;

//the time when the sensor outputs a low impulse long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 5000;

boolean lockLow = true; boolean takeLowTime;

int pirPin = 8; //the digital pin connected to the PIR sensor's output int buttonPin = 7; int pstPin = 13; int tcount = 100; //attempt at making a counter int buttonState = 0; int delayTime = 50; int sec = 0; int totalTimePrint = 0; int totalcount =100; LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

///////////////////////////// //SETUP void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(pstPin, OUTPUT); // for the output pin for the switch tail pinMode(buttonPin, INPUT); lcd.begin(16, 2); //give the sensor some time to calibrate Serial.print("calibrating sensor "); lcd.print("calibrating for 30s"); for(int i = 0; i < calibrationTime; i++){ //30 seconds of calibration time Serial.print("."); lcd.print("."); delay(1000); } lcd.clear(); Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); lcd.display(); // lcd.print("Hello World!");

}

//////////////////////////// //LOOP void loop(){ Serial.println("TOP OF LOOP"); lcd.display(); buttonState = digitalRead(buttonPin); //this is the input button for changing time if (buttonState == HIGH) { if(tcount<400){ tcount = tcount +100; totalcount = totalcount+100; }else { tcount = 100; totalcount = 100; } } else if(buttonState == LOW){ tcount = tcount; totalcount = totalcount; }

//this is the PIR Sensor if(digitalRead(pirPin) == HIGH){ //if motion is detected digitalWrite(pstPin, LOW); if(lockLow){ lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); } takeLowTime = true; tcount = totalcount; delay(delayTime); }

if(digitalRead(pirPin) == LOW){ //no motion digitalWrite(pstPin, LOW); if(takeLowTime){ lowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; //make sure this is only done at the start of a LOW phase } if(!lockLow && millis() - lowIn > pause){ lockLow = true; // Serial.print("motion ended at "); //output // Serial.print((millis() - pause)/1000); //Serial.println(" sec"); // delay(50); } delay(delayTime); tcount--; if (tcount == 0){ digitalWrite(pstPin, HIGH); // turn the Powertail off //if low doesnt turn it off change it to high exit(0); }

}

totalTimePrint = tcount /20; Serial.println(); lcd.print("Time Remaining:"); lcd.print(totalTimePrint); lcd.print("sec "); if(tcount%3 == 0){ lcd.clear();} }

------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!