forked from kaserashi/Electronic-letterbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.ino
30 lines (30 loc) · 840 Bytes
/
arduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<SoftwareSerial.h>
SoftwareSerial ArduinoUno(3,2);
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
ArduinoUno.begin(9600);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
ArduinoUno.print(distance);
ArduinoUno.println("\n");
Serial.println(distance);
delay(1000);
}