// A program to display distance value when the push button is pressed
// Add line no. 7, 11 & 19
int echopin = 2;
int trigpin = 3;
int Time;
int Distance;
// At line no. 7 create a variable 'buttonpin' with a data type 'int' and store a value '4'.
int buttonpin = 4;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// At line no. 11 use 'pinMode()' function. Inside the function pass two parameters 'buttonpin' and 'INPUT_PULLUP' \
pinMode(buttonpin, INPUT_PULLUP);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
}
int lastState = HIGH;
void loop() {
// At line no. 19 use digitalRead() function, inside the function pass 'buttonpin' variable and store it in a variable 'value' with a datatype 'int'.
int value = digitalRead((buttonpin));
if (lastState != value) {
lastState = value;
if (value == HIGH) {
digitalWrite(trigpin, LOW);
digitalWrite(trigpin, HIGH);
digitalWrite(trigpin, LOW);
Time = pulseIn(echopin,HIGH);
Distance = Time * 0.0172;
Serial.println(Distance);
}
}
}