0

Line Robot - Basic lessons - Reflectance sensor

MRMS reflectance sensors 9x, CAN, analog, I2C (mrm-ref-can) is a quite versatile sensor. Here, we will learn only the simplest form of measuring: digital values, meaning it either detects (for example) black stripe or not. Prerequisite for this functionality is calibration. The sensor must be calibrated.
  1. Run the program, position the robot near a black stripe on a white surface, and be ready to move it by hand.
  2. Enter command "cal".
  3. Immediately start moving the robot above the black stripe and over a white area, so that all the 9 transistors sense black and white. During the calibration, sensor's LED will be blinking rapidly. The end of calibration is indicated by a long LED-on. After that, normal 1-sec. blinking will continue.
  4. Stop moving the robot and issue a new command: "pri" (calibration print).
The last command will list 2 lines of numbers. The first line should be around 300 and less for all the 9 transistors and the second one around 700 and more. The difference between the 2 numbers in the same column must be at least 400. If so, the calibration was a success. The values will be stored in the sensor and will be retained even when the power is switched off. Only when the surface changes significantly, or the light does that, a new calibration might be desirable.
The calibrated sensor can be read easily. To do it using the terminal, enter "dgr" (digital read). Watch 1s representing a line below and 0s white surface. To read the sensor in Your program, use command
line(transistorNumber))
transistorNumber is a number between 0 and 8, for MRMS reflectance sensors 9x, CAN, analog, I2C (mrm-ref-can). The function returns "true" if the surface below is dark (like black). Having learned all that, we can make a program that detects the surface:
void RobotLine::loop() {
	if (line(0))
		print("Line\n\r");
}
How can we detect a white surface? line(0) is a logical expression, because it returns either "true" or "false" (like "2<3"). We also learned how logical not operator looks like: "!". Therefore,
!line(0) 
will detect a bright surface.

This was an example how to use the sensor in digital mode. It can also be used in analog mode in which every transistor returns a number in range between 0 (completely dark) and 1023 (very bright). The numbers in this range were reported by "pri" command. You can also use it in program, by calling function "brightness()":
void RobotLine::loop() {
	print("%i\n\r", brightness(0));
	delayMs(100);
}
"brightness(0)" is for transistor 0, but You can use 1, 2, etc., depending on how much transistors Your sensor features. Do not pay attention to the parts of this program You do not understand. You will learn that functionality later.

While reading either digital or analog values occurs momentary (from cache), switching between the 2 modes causes some cost in lost time, typically between 2 ms (to digital) and 15 ms (to analog). Therefore, it is advisable not to change the mode very often.

Task: print reflectance values

Your task is to print "White" when there is a white surface below sensor 8 (C++ starts counting from 0), so this is sensor 7 in C++ notation.