Your next task will be to add line-following to your robot. To do this you will receive a reflectance array shown below
We will go over how this works in detail a bit later. For now all you need to know is that each of the black squares can detect the "darkness" of the color placed under them. We will be using this to identify the black line of the path
Do not plug Vcc into the 5V rail.
Keep your robot the way it is, but save your code so far and create a new code file. At the end, if there is time, you will be combining this new code with your old code. For now though your robot will only follow lines
Begin coding your new file. You can use some of your old code if it will help
Make sure your new file:
Let's look at how the sensor array works
Start by creating a new function under and outside of your void loop(). Declare it as shown below as: int left_check()
The following diagram will show you the code for the function step by step, follow along with the descriptions as you code. The description for each step will be under the info-graphic
Now let's go back to your loop function, which is thus far empty, and we can call the left_check() function we just created. We'll store the returned timer value in a variable called "left" and display it to the Serial Monitor.
Upload your code and open the Serial Monitor and make sure your sensor is returning values.
If you've completed the step above, go ahead an duplicate your left_check() function twice, so that you now have three integer type functions to read from the left sensor, the middle sensor, and the right sensor.
Remember you'll need to specify which digital pin you used for each sensor
Once you have done that, modify your loop() so that you are reading from each of the sensors and displaying them to the serial monitor. The way I chose to do it is shown below.
Once all of your sensors are working, it's time to calibrate them. That is to say, we need to see what range of values each sensor returns in response to cardboard and to tape.
The image above is showing a small piece of cardboard with a strip of tape on it which you can find in the classroom on the materials table. By moving this square across each sensor and reading the output on the serial monitor, you can start to fill out this chart
The chart I've shown is just an example. The numbers shown are made-up. You'll need to make your own chart. For each sensor, write down in the corresponding slot an approximate range of values each sensor returns for the given material.
Say for my left sensor, the numbers I see on the serial monitor range between 100 and 300. I'll write that in the first box. My middle sensor, however, returns a range between 150 and 450. And so on.
With just the information I've shown so far, we could say that if a sensor is returning a number greater than 500, it sees black. Work out this chart for yourself and go to the next step.
Okay, so now we can tell whether a sensor is seeing black or not. What do we do with it?
Below is a chart I made of all the possible cases we can have. The black circle indicates that that sensor has detected black under it. So for example, in the first case, every sensor sees black, so the robot moves forward.
I've given you the chart, you'll have to make your if/else statements to control. But I'll help you get started
Notice the most important sensor is the middle sensor. In my chart, anytime the middle sensor sees black, the robot goes forward. It is only when the middle sensor is no longer above the black line that the robot must turn.
Also notice you do not have to account for the case where the middle sensor detects cardboard yet both of the side sensors detect tape, as this doesn't make sense and will never occur.
Your code could look something like this:
if (//MIDDLE SEES BLACK) go_forward(); else if (//LEFT SEES WHITE) turn_right(); else //etc....