diff --git a/lessons/lesson60.cpp b/lessons/lesson60.cpp new file mode 100644 index 0000000..eeaef15 --- /dev/null +++ b/lessons/lesson60.cpp @@ -0,0 +1,82 @@ +// https://www.youtube.com/watch?v=A6kEroT-g2o&list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP&index=60 + + +#include +#include + +const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12; +LiquidCrystal lcd(rs, en, d4, d5, d6, d7); + + +int trigPin = 2; +int echoPin = 3; +int pingTravleTime; +float distance; +const int pulsTime = 40; // uS +const float pingTimeToDistance = 0.017; // uS to cm, travletime[s] * 340 m/s / 2 + +int buttonPin = A0; +int buttomState; + +int setTime = 500; +int dt = 1000; + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + Serial.println("Measurring Distance"); + + delay(setTime); + + lcd.begin(16, 2); + lcd.print("Hello, World!"); + + pinMode(trigPin, OUTPUT); + pinMode(echoPin, INPUT); + pinMode(buttonPin, INPUT); + digitalWrite(buttonPin, HIGH); +} + +void loop() { + // put your main code here, to run repeatedly + buttomState = digitalRead(buttonPin); + while (buttomState == HIGH) + { + lcd.setCursor(0, 0); + lcd.print("Place the Target"); + lcd.setCursor(0, 1); + lcd.print("Press the Button"); + buttomState = digitalRead(buttonPin); + } + { + digitalWrite(trigPin, LOW); + delayMicroseconds(pulsTime); + digitalWrite(trigPin, HIGH); + delayMicroseconds(pulsTime); + digitalWrite(trigPin, LOW); + pingTravleTime = pulseIn(echoPin, HIGH); + delay(25); + + distance = pingTravleTime * pingTimeToDistance; // Travel time to distance in cm + + Serial.print("Ping Time and Distance:"); + Serial.print("Ping Time: "); + Serial.print(pingTravleTime); + Serial.print(" Distance: "); + Serial.print(distance); + Serial.println(" cm"); + + // Display on LCD + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Ping Time: "); + lcd.print(pingTravleTime); + lcd.print("us"); + lcd.setCursor(0, 1); + lcd.print("Distance: "); + lcd.print(distance); + lcd.print("cm"); + } + + +}