I'm currently making a home security system on tinkerCAD that, when it detects motion, tracks how far away the motion is, displays that information to the lcd, and then plays a tone on a buzzer and lights up an led. All of this works, except that the system needs to be able to switch from inches to cm, vice versa when the push button is pressed. Despite that, whenever I press the push button, it doesn't seem to work at all, and keeps displaying cm. I've spent a long time trying to find out what's wrong, and it's likely I've missed out on something simple, but I haven't come up with a solution.
Code (and the build of the system in case) below:
/*
The circuit:
* Push Button pin to digital pin 13
* LED pin to digital pin 12
* Buzzer pin to digital pin 11
* PIR Sensor Signal pin to digital pin 10
* UDS Trigger pin to digital pin 9
* UDS Echo pin to digital pin 8
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#include <LiquidCrystal.h>
// Initializing variables.
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int TrigPin = 9;
const int EchoPin = 8;
long duration = 0; // duration of echo pulse is between 296 and 59200 uS
int ledPin = 12;
const int inputPin = 10; // choose the input pin (for PIR sensor)
int pirState = 0; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int buzzerPin = 11;
int buttonPin = 13;
int buttonState = 0;
void setup() {
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(buttonPin, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
// Helper functions.
long durationToCentimeters(long microseconds){
return microseconds/58;
}
float durationToInches(float microseconds){
return microseconds/148;
}
void loop() {
pirState = digitalRead(inputPin);
if(pirState == HIGH){
digitalWrite(TrigPin, LOW); // output short LOW pulse beforehand to ensure a clean HIGH pulse
delay (2);
digitalWrite(TrigPin, HIGH); // the PING))) is triggered by a HIGH pulse of 10 microseconds
delay (10);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH); // The EchoPin reads the HIGH pulse signal from the PING)))
buttonState = digitalRead(buttonPin); // Reads when the button is pushed.
if(buttonState == HIGH){
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print("MOTION DETECTED!");
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(durationToInches(duration));
lcd.print(" Inches");
digitalWrite(ledPin, HIGH);
tone(11,523,1000);
}else{
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print("MOTION DETECTED!");
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(durationToCentimeters(duration));
lcd.print(" cm");
digitalWrite(ledPin, HIGH);
tone(11,523,1000);
}
}else{
noTone(11);
digitalWrite(ledPin, LOW);
lcd.clear();
}
}




