User Guide

173. // clear the slave’s LCD and display “Connect” and “OK” on two lines
174. // Put OK in the center to test x-y positioning
175. slave_clear();
176. slave_print(“Connect”);
177. slave_lcd_goto_xy(3,1);
178. slave_print(“OK”);
179. // play a tune
180. char tune[] = “\xB3 l16o6gab>c”;
181. tune[1] = sizeof(tune)-3;
182. serial_send_blocking(tune,sizeof(tune)-1);
183. // wait
184. wait_for_button(ALL_BUTTONS);
185. // reset calibration
186. slave_reset_calibration();
187. time_reset();
188. slave_auto_calibrate();
189. unsigned
char speed1 = 0, speed2 = 0;
190. // read sensors in a loop
191. while(1)
192. {
193. serial_send(“\x87”,1); // returns calibrated sensor values
194. // read 10 characters
195. if(serial_receive_blocking(buffer, 10, 100))
196. break;
197. // get the line position
198. serial_send(“\xB6”, 1);
199. int line_position[1];
200. if(serial_receive_blocking((char *)line_position, 2, 100))
201. break;
202. // get the battery voltage
203. serial_send(“\xB1”,1);
204. // read 2 bytes
205. int battery_millivolts[1];
206. if(serial_receive_blocking((char *)battery_millivolts, 2, 100))
207. break;
208. // display readings
209. display_levels((unsigned int*)buffer);
210. lcd_goto_xy(5,0);
211. line_position[0] /= 4; // to get it into the range of 0-1000
212. if(line_position[0] == 1000)
213. line_position[0] = 999; // to keep it to a maximum of 3 characters
214. print_long(line_position[0]);
215. print(“ “);
216. lcd_goto_xy(0,1);
217. print_long(battery_millivolts[0]);
218. print(“ mV “);
219.
delay_ms(10);
220. // if button A is pressed, increase motor1 speed
221. if(button_is_pressed(BUTTON_A) && speed1 < 127)
222. speed1 ++;
223. else if(speed1 > 1)
224. speed1 -= 2;
225. else if(speed1 > 0)
226. speed1 = 0;
227. // if button C is pressed, control motor2
228. if(button_is_pressed(BUTTON_C) && speed2 < 127)
229. speed2 ++;
230. else if(speed2 > 1)
231. speed2 -= 2;
232. else if(speed2 > 0)
233. speed2 = 0;
234. // if button B is pressed, do PID control
235. if(button_is_pressed(BUTTON_B))
236. slave_set_pid(40, 1, 20, 3, 2);
237. else
238. {
239. slave_stop_pid();
240. slave_set_motors(speed1, speed2);
241. }
242. }
243. }
244.
while(1);
245. }