User Guide

101. print_long(bat);
02. print(“mV”);
1
103. lcd_goto_xy(0,1);
104. print(“Press B”);
105. delay_ms(100);
106. }
107. // Always wait for the butt
ur
on to be released so that 3pi doesn’t
hand is away from it.
eadings and keeps
maximum values encountered. The
alues as a bar graph.
essed(BUTTON_B))
easurement.
nsors,IR_EMITTERS_ON);
;
efore we start driving.
s
ined somewhere.
();
rovide
” argument to read_line() here, even though we
imum
000)
108. // start moving until yo
109. wait_for_button_release(BUTTON_B);
110. delay_ms(1000);
rating the 111. // Auto-calibration: turn right and left while calib
112. // sensors.
er<80;counter++) 113. for(counter=0;count
114. {
115. if(counter < 20 || counter >= 60)
116. set_motors(40,-40);
117. else
118. set_motors(-40,40);
t of sensor r119. // This function records a se
120. // track of the minimum and
121. // IR_EMITTERS_ON argument means that the IR LEDs will be
122. // turned on during the reading, which is usually what you
123. // want.
124. calibrate_line_sensors(IR_EMITTERS_ON);
125. // Since our counter runs to 80, the total delay will be
600 ms. 126. // 80*20 = 1
127. delay_ms(20);
128. }
129. set_motors(0,0);
130. // Display calibrated v
131. while(!button_is_pr
132. {
133. // Read the sensor values and get the position m
134.
unsigned int position = read_line(se
135. // Display the position measurement, which will go from 0
136. // (when the leftmost sensor is over the line) to 4000 (when
137. // the rightmost sensor is over the line) on the 3pi, along
138. // with a bar graph of the sensor readings. This allows you
139. // to make sure the robot is ready to go.
140. clear();
141. print_long(position);
142. lcd_goto_xy(0,1);
ings(sensors); 143. display_read
144. delay_ms(100);
145. }
146. wait_for_button_release(BUTTON_B)
147. clear();
148. print(“Go!”);
149. // Play music and wait for it to finish b
go); 150. play_from_program_space(
151. while(is_playing());
152. }
the code starts. All C program153. // This is the main function, where
ion def154. // must have a main() funct
155. int main()
156. {
r values 157. unsigned int sensors[5]; // an array to hold senso
3pi 158. // set up the
itialize159. in
160. // This is the “main loop” - it will run forever.
161. while(1)
162. {
163. // Get the position of the line. Note that we must p
164. // the “sensors
165. // are not interested in the individual sensor readings.
166. unsigned int position = read_line(sensors,IR_EMITTERS_ON);
167
. if(position < 1000)
168.
{
169. // We are far to the right of the line: turn left.
and the left motor to zero, 170. // Set the right motor to 100
171. // to do a sharp turn to the left. Note that the max
172. // value of either motor speed is 255, so we are driving
173. // it at just about 40% of the max.
174. set_motors(0,100);
175. // Just for fun, indicate the direction we are turning on
176. // the LEDs.
177. left_led(1);
178. right_led(0);
179. }
180. else if(position < 3