Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MEND
electrical
Commits
fd8c7a5f
Commit
fd8c7a5f
authored
Feb 16, 2021
by
Brandon Fan
💬
Browse files
add elevation change
parent
c56a3d2f
Changes
1
Hide whitespace changes
Inline
Side-by-side
main/elevation_change.ino
0 → 100644
View file @
fd8c7a5f
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long
duration
;
// variable for the duration of sound wave travel
int
distance
;
// variable for the distance measurement
int
prevDistance
;
void
setup
()
{
pinMode
(
trigPin
,
OUTPUT
);
// Sets the trigPin as an OUTPUT
pinMode
(
echoPin
,
INPUT
);
// Sets the echoPin as an INPUT
Serial
.
begin
(
9600
);
// // Serial Communication is starting with 9600 of baudrate speed
Serial
.
println
(
"Ultrasonic Sensor HC-SR04 Test"
);
// print some text in Serial Monitor
Serial
.
println
(
"with Arduino UNO R3"
);
prevDistance
=
0
;
digitalWrite
(
trigPin
,
LOW
);
delayMicroseconds
(
2
);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
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
;
// Speed of sound wave divided by 2 (go and back)
}
void
loop
()
{
// Clears the trigPin condition
digitalWrite
(
trigPin
,
LOW
);
delayMicroseconds
(
100
);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
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
;
// Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
if
(
distance
-
prevDistance
<
2000
&&
distance
-
prevDistance
>
-
2000
)
{
if
(
distance
-
prevDistance
>
40
)
{
Serial
.
print
(
"Major Change in Elevation Down: "
);
Serial
.
print
(
distance
-
prevDistance
);
Serial
.
println
(
"cm"
);
Serial
.
print
(
"New Elevation: "
);
Serial
.
print
(
distance
);
Serial
.
println
(
"cm"
);
Serial
.
println
();
}
else
if
(
distance
-
prevDistance
<
-
40
)
{
Serial
.
print
(
"Major Change in Elevation Up: "
);
Serial
.
print
(
distance
-
prevDistance
);
Serial
.
println
(
"cm"
);
Serial
.
print
(
"New Elevation: "
);
Serial
.
print
(
distance
);
Serial
.
println
(
"cm"
);
Serial
.
println
();
}
prevDistance
=
distance
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment