Advanced Kit 4: Reflectance Array

Information
Step 1

Your next task will be to add line-following to your robot. To do this you will receive a reflectance array shown below

Reflectance sensor

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

Step 2

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

Ref schematic

  • Plug the three wires labeled 1-3 into digital pins of your choosing
  • Plug the GND wire into your ground rail.
  • Plug Vcc into a digital pin. In your start-up function, set the pinMode to OUTPUT, and then set the pin HIGH. This will power your reflectance array without overloading it with current
  • Planning
    Step 3
    Car with ref array

    Attach the sensor to the front of your robot. Get it so that the sensor will be as close to the floor as possible. (Without scraping against it of course).

    Step 4
    Let's start Coding

    Begin coding your new file. You can use some of your old code if it will help

    Make sure your new file:

    1. Attaches your servos

    2. Powers your reflectance array through a digital pin as in step 2

    3. Begins the serial monitor

    4. For now, has a blank loop() function
    Information
    Step 5

    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

    Reflectance info graphic

    1. We start by setting a timer variable to 0, which we will use later

    2. Each sensor has an emitter and a receiver. Here we are looking at one of the three sensors. My left sensor is attached to pin 8, so when we turn it HIGH it begins to emit Infrared (IR) light. This light bounces off the ground and is collected by the receiver

    3. When the receiver collects the light waves, it stores the energy in a capacitor. A capacitor is like a small battery and it takes time to charge, so we wait about 20 milliseconds or so to give it time.

    4. What we are interested in is how much light the capacitor stored in that time. So first we turn pin 8 from OUTPUT to INPUT mode, so now instead of OUTPUTing to and thereby filling the capacitor, it drains the energy and takes it as INPUT

    5. As long as pin 8 is receiving INPUT it will not read LOW. So here we have a loop that iterates the timer as we wait for the capacitor to drain. Once it is done draining into pin 8, there will no longer be any INPUT and thus it will read LOW and the loop will end.

    6. Lastly, we now have an integer named "timer" which tells us how long it took to drain the capacitor, so we have the computer return the value.
    Step 6

    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.

    Function image

    Upload your code and open the Serial Monitor and make sure your sensor is returning values.

    Step 7
    Calibrate the sensors

    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.

    Calibration infographic

    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.

    Example:

    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.

    Step 8

    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.

    Array chart

    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....
    Challenge
    Step 9
    CHALLENGE:

    For your final step, the challenge is to complete all of the code and logic your line follower will need to solve this maze. Show an instructor when your robot successfully follows the line on the tape path in class

    Car with ref array
    Advanced Kit 4: Reflectance Array Info

    Account

    MVCode Clubs

    Created By

    Newprofile jamie

    Access Level

    premium

    For Teachers and Schools

    Teach coding to your students with MVCode Teach

    MVCode offers an integrated product designed to teach coding to students.

    Learn more about MVCode Teach