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
a995aaa5
Commit
a995aaa5
authored
Jan 26, 2021
by
sonaraju
Browse files
Add old code
parent
2c41761e
Changes
1
Hide whitespace changes
Inline
Side-by-side
main.cpp
0 → 100644
View file @
a995aaa5
const
int
trigPin1
=
13
;
const
int
echoPin1
=
12
;
const
int
trigPin2
=
9
;
const
int
echoPin2
=
8
;
const
int
trigPin3
=
5
;
const
int
echoPin3
=
4
;
const
int
led1
=
11
;
const
int
led2
=
10
;
const
int
led3
=
7
;
long
duration
;
int
distance1
;
int
distance2
;
int
distance3
;
//no LEDs in this kit, just use readout in terminal for now
void
setup
()
{
// put your setup code here, to run once:
//Set trig pins as Output and echo pins as Input
pinMode
(
trigPin1
,
OUTPUT
);
pinMode
(
echoPin1
,
INPUT
);
pinMode
(
trigPin2
,
OUTPUT
);
pinMode
(
echoPin2
,
INPUT
);
pinMode
(
trigPin3
,
OUTPUT
);
pinMode
(
echoPin3
,
INPUT
);
pinMode
(
led1
,
OUTPUT
);
pinMode
(
led2
,
OUTPUT
);
pinMode
(
led3
,
OUTPUT
);
Serial
.
begin
(
9600
);
}
void
loop
()
{
// put your main code here, to run repeatedly:
usSensor
(
trigPin1
,
echoPin1
,
1
);
usSensor
(
trigPin2
,
echoPin2
,
2
);
usSensor
(
trigPin3
,
echoPin3
,
3
);
if
(
distance1
<
30
&&
distance2
<
30
)
{
Serial
.
println
(
"Large obstacle detected"
);
}
}
void
usSensor
(
int
trigPin
,
int
echoPin
,
int
sensorNum
)
{
int
ledPin
=
0
;
digitalWrite
(
trigPin
,
LOW
);
delay
(
2
);
digitalWrite
(
trigPin
,
HIGH
);
delay
(
100
);
digitalWrite
(
trigPin
,
LOW
);
duration
=
pulseIn
(
echoPin
,
HIGH
);
int
distance
=
duration
*
(
0.034
/
2
);
if
(
sensorNum
==
1
)
{
distance1
=
distance
;
ledPin
=
led1
;
}
else
if
(
sensorNum
==
2
)
{
distance2
=
distance
;
ledPin
=
led2
;
}
else
{
distance3
=
distance
;
ledPin
=
led3
;
}
Serial
.
print
(
"Sensor "
);
Serial
.
print
(
sensorNum
);
Serial
.
print
(
": "
);
Serial
.
println
(
distance
);
if
(
distance
<
30
)
{
digitalWrite
(
ledPin
,
HIGH
);
delay
(
distance
*
15
);
digitalWrite
(
ledPin
,
LOW
);
//delay(distance * 15);
Serial
.
print
(
"Obstacle detected: Sensor "
);
Serial
.
println
(
sensorNum
);
//delay(100);
}
else
{
digitalWrite
(
ledPin
,
LOW
);
// turn the LED off by making the voltage LOW
delay
(
300
);
}
}
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