ROLLBOTMICRO
30
/*********Read data of the sensors***********/
void
RollbotReadSensor
::
Read_Data
()
{
data
[
0
]
=
analogRead
(
A0
);
data
[
1
]
=
analogRead
(
A1
);
data
[
2
]
=
analogRead
(
A2
);
data
[
3
]
=
analogRead
(
A3
);
data
[
4
]
=
analogRead
(
A7
);
OLED_Flag
[
0
]
=
map
(
data
[
0
],
50
,
500
,
3
,
6
);
OLED_Flag
[
1
]
=
map
(
data
[
1
],
50
,
500
,
3
,
6
);
OLED_Flag
[
2
]
=
map
(
data
[
2
],
50
,
500
,
3
,
6
);
OLED_Flag
[
3
]
=
map
(
data
[
3
],
50
,
500
,
3
,
6
);
OLED_Flag
[
4
]
=
map
(
data
[
4
],
50
,
500
,
3
,
6
);
}
Compare the collected AD value with the given value and generate a result which indicates
the position of the robot to the black line, as shown below.
int
RollbotReadSensor
::
Read_BlackFlag
()
{
while
(
1
)
{
Read_Data
();
if
((
data
[
0
]
<
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
-
4
;
else
if
((
data
[
0
]
<
threshold
)
&&
(
data
[
1
]
<
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
-
3
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
<
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
-
2
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
<
threshold
)
&&
(
data
[
2
]
<
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
-
1
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
<
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
0
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
<
threshold
)
&&
(
data
[
3
]
<
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
1
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
<
threshold
)
&&
(
data
[
4
]
>
threshold
))
return
2
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
<
threshold
)
&&
(
data
[
4
]
<
threshold
))
return
3
;
else
if
((
data
[
0
]
>
threshold
)
&&
(
data
[
1
]
>
threshold
)
&&
(
data
[
2
]
>
threshold
)
&&
(
data
[
3
]
>
threshold
)
&&
(
data
[
4
]
<
threshold
))
return
4
;
SunFounder