int LEFT_EYE = 3; /* analog port number of left light sensor */ int RIGHT_EYE = 4; /* analog port number of right light sensor */ void main() { while (1) { printf("L=%d, R=%d\n", analog(LEFT_EYE), analog(RIGHT_EYE)); sleep(0.1); } }
int MAX_LIGHT = 10; /* maximum brightness level of environment */ int MIN_LIGHT = 200; /* minimum brightness level of environment */ int normalize(int light) { int output = 100 - ((light - MAX_LIGHT) * 100 / (MIN_LIGHT - MAX_LIGHT)); /* keep output within range 0 to 100 */ if (output < 0) output = 0; if (output > 100) output = 100; return output; }This function maps sensor readings less than or equal to MAX_LIGHT (corresponding to high brightness levels) to motor speed 100, and readings greater than or equal to MIN_LIGHT (corresponding to low brightness levels) to motor speed 0. Modify your program from Part 2 so that it reports normalized light sensor readings, and then test out the sensors. You should experiment with different values of MAX_LIGHT and MIN_LIGHT, in order to find the most appropriate values for your environment.
int LEFT_MOTOR = 1; /* port number of left motor */ int RIGHT_MOTOR = 3; /* port number of right motor */ void main() { while (1) { motor(LEFT_MOTOR, normalize(analog(RIGHT_EYE))); motor(RIGHT_MOTOR, normalize(analog(LEFT_EYE))); } }Simply interchanging RIGHT_EYE and LEFT_EYE results in light-avoiding behavior. Experiment with both kinds of behavior under a variety of lighting conditions. Can you rewrite the control program to accentuate differences between the two light sensors?
void turn_right(float seconds) { fd(LEFT_MOTOR); bk(RIGHT_MOTOR); sleep(seconds); off(LEFT_MOTOR); off(RIGHT_MOTOR); }Calling turn_right(0.25) will execute a quarter-second turn to the right. Define similar functions for turning to the left and going backwards, and then use these in your touch sensor code.
float random_time(float min, float max)which should return a random floating-point number in the range min to max. For example, random_time(0.25, 1.5) would return a number between 0.25 and 1.5. You can then use this as the argument to turn_right or turn_left. (Hint: subtracting min from max gives the size of the range of desired values. Use random(100) to generate a random "percentage" of this range.) How does randomized turning affect your robot's ability to avoid obstacles while seeking light sources? How long can your robot "survive" in its environment without getting stuck?