1+ #include < Shieldbot . h>
2+
3+ #define BLACK LOW
4+ #define WHITE HIGH
5+
6+ #define LEFT 1 ;
7+ #define RIGHT 0 ;
8+
9+ enum State_type {
10+ Initialise ,
11+ Advance ,
12+ Advance_Turn_left ,
13+ Advance_Turn_right ,
14+ Rotate_to_time ,
15+ backup
16+ };
17+
18+ Shieldbot shieldbot = Shieldbot ();
19+
20+ int S1 ,S2 ,S3 ,S4 ,S5 ; // values to store state of sensors
21+ int pre_S1,pre_S2,pre_S3,pre_S4,pre_S5;
22+ int inc_minutes;
23+ int counter;
24+ int tick;
25+
26+ int last_side;
27+
28+
29+ State_type State = Initialise ;
30+
31+ void setup (){
32+ Serial . begin(9600 );
33+ shieldbot. setMaxSpeed(255 );// 255 is max
34+
35+ Serial . print(" Shield bot says send me a command" );
36+ Serial . println();
37+ Serial . print(" r = right, l = left, f = fwd, b = back" );
38+ Serial . println();
39+ tick = 0 ;
40+ counter = 0 ;
41+ }
42+
43+ void loop (){
44+
45+ pre_S1 = S1 ;
46+ pre_S2 = S2 ;
47+ pre_S3 = S3 ;
48+ pre_S4 = S4 ;
49+ pre_S5 = S5 ;
50+
51+ S1 = shieldbot. readS1();
52+ S2 = shieldbot. readS2();
53+ S3 = shieldbot. readS3();
54+ S4 = shieldbot. readS4();
55+ S5 = shieldbot. readS5();
56+
57+ // get time
58+
59+
60+ counter++ ;
61+ if (counter == 60 )
62+ {
63+ inc_minutes = 1 ;
64+ counter = 0 ;
65+ }
66+ // Print the status of each sensor to the Serial console
67+ /* Serial.print("S5: ");
68+ Serial.print(S5);
69+ Serial.print(" S4: ");
70+ Serial.print(S4);
71+ Serial.print(" S3: ");
72+ Serial.print(S3);
73+ Serial.print(" S2: ");
74+ Serial.print(S2);
75+ Serial.print(" S1: ");
76+ Serial.print(S1); */
77+
78+ // drive fwd with staying centered
79+ switch (State )
80+ {
81+ case Initialise :
82+ // rotate until hit center then fwd until 12
83+ State = Advance ;
84+ break ;
85+ case Advance : // drive fwd with staying centered
86+
87+ tick = 0 ;
88+ if (is_centered() == true )
89+ {
90+ if (last_side == 0 )
91+ {
92+ if ((S5 == BLACK ) & (pre_S5 == WHITE ))
93+ {
94+ State = Rotate_to_time ;
95+ last_side = 1 ;
96+ }
97+ else
98+ goforward();
99+
100+ }
101+ else // left
102+ {
103+ if (S1 == BLACK & pre_S1 == WHITE )
104+ {
105+ State = Rotate_to_time ;
106+ last_side = 0 ;
107+ }
108+ else
109+ goforward();
110+
111+ }
112+
113+
114+ }
115+ else
116+ {
117+ if (S2 == WHITE )
118+ State = Advance_Turn_left ;
119+ else if (S4 == WHITE )
120+ State = Advance_Turn_right ;
121+ }
122+ break ;
123+
124+ case Advance_Turn_left :
125+ goleft();
126+ if (is_centered()== true )
127+ State = Advance ;
128+
129+ break ;
130+
131+ case Advance_Turn_right :
132+ goright();
133+ if (is_centered()== true )
134+ State = Advance ;
135+ break ;
136+
137+ case Rotate_to_time :
138+
139+ Serial . print(" rotatae to time state" );
140+
141+
142+ if (inc_minutes == 1 )
143+ {
144+ if ((S2 == BLACK & pre_S2 == WHITE ))
145+
146+ // found next mark
147+ inc_minutes = 0 ;
148+ tick++ ;
149+
150+ Serial . print(" tick: " );
151+ Serial . print(tick);
152+
153+ if (is_centered() & tick > 10 )
154+ {
155+ State = Advance ;
156+ }
157+ else
158+ goright_big();
159+ }
160+
161+ break ;
162+
163+ default :
164+ break ;
165+ // if nothing else matches, do the default
166+ // default is optional
167+ }
168+
169+
170+
171+ delay (50 );
172+
173+ botstop();
174+
175+ Serial . println();
176+ }
177+
178+
179+ boolean is_centered()
180+ {
181+
182+ if (S3 == BLACK & S4 == BLACK & S2 == BLACK )
183+ return true ;
184+ else
185+ return false ;
186+
187+ }
188+
189+
190+
191+ void goright (){
192+ shieldbot. drive(32 ,0 );
193+ Serial . print(" Right turn" );
194+ }
195+
196+ void goright_big (){
197+ shieldbot. drive(20 ,- 20 );
198+ Serial . print(" Right turn" );
199+ }
200+
201+ void goleft (){
202+ shieldbot. drive(0 ,32 );
203+ Serial . print(" Left turn" );
204+ }
205+
206+ void goforward()
207+ {
208+ shieldbot. drive(32 ,32 );
209+ Serial . print(" go fwd" );
210+ }
211+
212+ void botstop()
213+ {
214+ shieldbot. drive(0 ,0 );
215+ }
0 commit comments