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
61a6b3ab
Commit
61a6b3ab
authored
Jan 26, 2021
by
Brandon Fan
Browse files
add main with motor
parent
f99b3352
Changes
1
Hide whitespace changes
Inline
Side-by-side
main/main_with_motor.ino
0 → 100644
View file @
61a6b3ab
const
int
trigPin
=
12
;
const
int
echoPin
=
11
;
const
int
trigPin2
=
8
;
const
int
echoPin2
=
9
;
const
int
motor
=
3
;
const
int
button
=
13
;
const
int
potentiometer
=
A0
;
long
duration2
;
long
duration
;
int
distance
;
int
distance2
;
int
volume
;
Int
volumeScaled
;
void
setup
()
{
//US1
pinMode
(
trigPin
,
OUTPUT
);
// Sets the trigPin as an Output
pinMode
(
echoPin
,
INPUT
);
// Sets the echoPin as an Input
//US2
pinMode
(
trigPin2
,
OUTPUT
);
// Sets the trigPin as an Output
pinMode
(
echoPin2
,
INPUT
);
// Sets the echoPin as an Input
pinMode
(
potentiometer
,
INPUT
);
pinMode
(
button
,
INPUT_PULLUP
);
pinMode
(
motor
,
OUTPUT
);
Serial
.
begin
(
9600
);
// Starts the serial communication
}
void
loop
()
{
if
(
digitalRead
(
button
)
==
HIGH
)
{
// Clears the trigPin
digitalWrite
(
trigPin2
,
LOW
);
digitalWrite
(
trigPin
,
LOW
);
delayMicroseconds
(
2
);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite
(
trigPin2
,
HIGH
);
digitalWrite
(
trigPin
,
HIGH
);
delayMicroseconds
(
10
);
digitalWrite
(
trigPin2
,
LOW
);
digitalWrite
(
trigPin
,
LOW
);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2
=
pulseIn
(
echoPin2
,
HIGH
);
duration
=
pulseIn
(
echoPin
,
HIGH
);
//Potentiometer
volume
=
analogRead
(
potentiometer
);
volumeScaled
=
volume
*
0.25
;
// Calculating the distance
distance
=
duration
*
0.034
/
2
;
distance2
=
duration2
*
0.034
/
2
;
if
(
distance2
>
30
)
{
Serial
.
println
(
"No output"
);
}
111
if
(
distance2
<
30
)
{
if
(
distance
<
75
)
{
Serial
.
println
(
distance
);
analogWrite
(
motor
,
volumeScaled
);
delay
(
distance
*
15
);
}
else
{
}
}
else
{
digitalWrite
(
motor
,
LOW
);
}
}
if
(
digitalRead
(
button
)
==
LOW
)
{
digitalWrite
(
motor
,
LOW
);
}
}
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