Make:it Robotics Starter Kit - Understanding Bitwise Math




Introduction: Make:it Robotics Starter Kit - Understanding Bitwise Math

About: Software Developer, like to work with electronics, embedded systems, robots etc.

This blog post is a continuation on my tinkering with the Make:it Robotics Kit. Hopefully I can pass on some of the information that I have learned so it will make it easier for others to get up to speed on working with the Robotics Kit.

After building the line following robot, the manual had a link to download the software for the robot. I tried to navigate to the link and it was not available:

This is a common problem when manufactures are writing documents, links seem to change and revisions often miss each other. Not a problem Radio Shack was good enough to provide a link to the download files on their web site:

I will not post the entire source code here as you can download it and follow along. The source code contains library and include files for the Main Robotics Kit, Project 1 and Project 2 Kits and the Sensor Kit. There are individual Arduino Sketch files for each robot provided in the kits.

All of the sketches depend on the a MakeItRobotics.cpp file and a MakeItRobotics.h file. If you need assistance understanding what these files are, google for .cpp file and .h include file. If you need further help drop me an email on this site. Since I built the line following robot this blog post will discuss the line following code, which is named linefollow.ino.

This file is a text file that the Arduino IDE reads or any development environment can read. The linefollow.ino file is documented and should provide a basic understanding of what is going on with the program. But I did find a typo that could cause some confusion. Also I will try to provide some additional information for those folks that are rather new to robotics, Arduino and programming in general.

For more info check out my blog:

Step 1: Arduino Sketch

The linefollow.ino is pretty much a standard Arduino Sketch file, at the beginning of the file is a main comments section, a declaration/include section, a setup() function and a loop() function.

The declaration/include section includes header files, which include definitions and resources that the sketch will used during program execution.

Global objects and variables can also be declared and initialized at this location as well. For example in the linefollow.ino sketch the line_following object is created (or in programming language, this is called instantiated)

Everything in the setup() function is run once when the Arduino first powers up. The setup() function is where you place start up code that initialize things that your loop program needs for program execution. For Example the Arduino needs to talk to the robot motor driver/sensor board using the serial port.

So the setup() function calls the Serial.begin(10420) method to setup the serial port so the Arduino can take with the motor driver board. The loop() function is where all of the action in the program takes place. the loop() function loops or repeats over and over without stopping until the power is removed from the Arduino. This is why the function is called loop().

Step 2: Parsing ReadOptical() Method

As I said there is are a few typos in the comments of the code in linefollow.ino.Every loop the program calls the following function:sensor_in=line_following.read_optical();

The comments for this read_optical() method reads the following:


0×000 optical1 black

0x0ff optical1 white

0×100 optical1 white

0x1ff optical1 black

0x2XX not ready; don’t use this value

Actually is should be the following


0×000 optical1 black

0xff optical1 white

0×100 optical2 white

0x1ff optical2 black

0x2XX not ready; don’t use this value(either sensor can return this value)

Actually 0x0ff should be 0xff

Refer to blog post Software Part 3

You will see in the below paragraphs the importance of these typos.(has to do with reading every other sensor) On the line following robot there are two optical sensors, every time the loop() function executes the linefollow.ino sketch calls a method to see if one of the sensors is seeing light or dark.

On a micro-controller, such as the Arduino, one second is a very long time. The loop() function can execute many thousand times a second. Just how long each loop takes depends on how many program instructions take place. Some instructions take longer to execute than other instructions.

Instruction (what functions and methods are made up of) execution time is measured in CPU clock cycles. The Arduino by default operates at 16 MHz or 16 million clock cycles a second. The linefollow.ino sketch performs some pretty simple logic.

After setting up some variables, the loop() calls a function to see if one sensor is light or dark, Depending on the answer some fancy logic is executed to reduce the answer to a value of 0, 1, 2 or 3. These values are then used to tell the motors to either go forward, turn left or right.

Every 15 milliseconds the loop program is told to switch and read the other sensor. So you have a program that loops reading first one sensor, converts the answer to 0, 1, 2 or 3 ,tell the motors what to do, and then read the other sensor, convert the answer to 0, 1, 2 or 3 and then tell the motors what to do. In many cases, depending on the values of the action1 and action2 variables, the instructions to control the motors are skipped. Refer to a later blog post.

These operations are done again at thousands of times a second. The rest of this blog and later blogs are going to teach you what is happening with the fancy logic that reads the sensor and converts the answer to 0, 1, 2, or 3. The following code segment is the fancy code that converts the sensor readings to a value of0, 1, 2 or 3. This code involves some program instructions that can be quite confusing for newmicro-controller programmers.

Please refer another blog entry that I wrote called “Tutorial – Bit Banging and Boolean Math without the Math” This tutorial goes hand in hand with understanding the following code segment:

if((sensor_in & 0xf00) == 0)

sensorValue1 = sensor_in & 0xff;

else if((sensor_in & 0xf00) >> 8 == 1)

sensorValue2 = sensor_in & 0xff;

if (sensorValue1 == 0x00)action1 = action1 & 0xfe;

if (sensorValue1 == 0xFF)action1 = action1 | 0x01;

if (sensorValue2 == 0x00)action1 = action1 | 0x02;

if (sensorValue2 == 0xFF)action1 = action1 & 0xfd;

This segment of code uses three special operators (instructions that operate on given values) The code uses the following binary operators: & Bitwise AND operator, | Bitwise OR operatorand the >> Bitwise Right Shift operator.

(Read the tutorial)

Step 3: Python to the Rescue

To make this easier so you do not have to convert this by hand I wrote a Python program that does all of the math for you and shows you what is taking place at the binary level, so all you have to do is look at the results.

Install Python on your computer, it is free. Read the tutorial, Everything is explained on how to get started with Python. I have done all of the work for you.

In the next tutorial we will capture some sensor data from the robot and analyze the data with our Python program



    • Tiny Home Contest

      Tiny Home Contest
    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    11 Discussions

    what would be the best way to use motor 3 and 4 as if i made a 4 wheel drive? just starting out but just getting a little confused going between the 3 documents lol

    4 replies

    Hello David,

    The motor driver board that comes with the Make:it Robotics kit does have an additional two motor driver header connectors.

    There is a kit that Radio Shack sells that has extra motors and wheels,
    I do not have this kit but I would image that you can configure a 3 and four wheel configuration. Just as a note, it is much harder to robot steering using 4 wheels. differential steering (by turning on and off individual motors, like the two wheel version of the robot) does not work that well.
    Take a look at any RC car. They have front wheel steering by turning the wheels. You could more than likely pick up a broken RC car and use the steering components to modify and create a new robot using these components.

    You would also have to write some custom code to power the wheels and, using a RC servo write some code to turn the wheels using the servo.

    This would be a cool project to do.

    If you are not at that level of skill, keep on working with kits that Radio Shack sells and use these to increase your knowledge of programming the Arduino. Then you will have developed the skills needed to build and program your 4 wheel drive from wheel steering.



    yes the radio shack near me is closing so everything was 50%+ off so i bought up everything i can but now i want to use it all but dont know how lol. i have the 4 wheels and motors hooked up to the board givin but im un certain where to add the M3_action and M4_action in the coding. i did find the codes for the forward, reverse and steering. I also do have two smaller servos i picked up i plan on adding for steering atleast one but was hopeing to get the other two motors working i see where it refrences them in the cpp file

    I just built a car out of bordom (Didn't even have any RC abilities). I used 4 motors to drive the wheels and control them kinda like a bobcat. All four go fowards for foward movement, backwards for backward movements. To turn left, the left two wheels run backwards while the right two move forwards. This allows the bot to pivot rather than have to move forward to turn. Not sure if this is what you are looking for but just my suggestion. Also, I modified the walking bot code just to figure out the class that came with the starter kit.

    void loop()

    void GoFowards()
    Walkingrobot.m1_action(BW, DEFAULT_SPEED);
    Walkingrobot.m2_action(BW, DEFAULT_SPEED);
    Walkingrobot.m3_action(FW, DEFAULT_SPEED);
    Walkingrobot.m4_action(FW, DEFAULT_SPEED);

    void GoBackwards()
    Walkingrobot.m1_action(FW, DEFAULT_SPEED);
    Walkingrobot.m2_action(FW, DEFAULT_SPEED);
    Walkingrobot.m3_action(BW, DEFAULT_SPEED);
    Walkingrobot.m4_action(BW, DEFAULT_SPEED);

    void TurnLeft()
    Walkingrobot.m1_action(BW, DEFAULT_SPEED);
    Walkingrobot.m2_action(FW, DEFAULT_SPEED);
    Walkingrobot.m3_action(BW, DEFAULT_SPEED);
    Walkingrobot.m4_action(FW, DEFAULT_SPEED);

    void TurnRight()
    Walkingrobot.m1_action(FW, DEFAULT_SPEED);
    Walkingrobot.m2_action(BW, DEFAULT_SPEED);
    Walkingrobot.m3_action(FW, DEFAULT_SPEED);
    Walkingrobot.m4_action(BW, DEFAULT_SPEED);

    I think the original code to move the walking robot legs was like Walkingrobot.LeftLegMove Or something. Because of the way the class is set up, you can bypass that and just go straight to m1_action - m4_action.

    Take note that to move the motor forwards, you pass FW and then you pass a speed (I think it is an int). Depending on how you wire your motors depends on how the FW and BW turn the motors. So you may have to switch the variables (Lot easier than rewiring it) to make your wheels spin the way you want.

    Hi David,

    Yes you are on the right track. Looking at the .cpp file and the .h file will give you ideas on the other methods available. Then you can write your own. Controlling an rc servo is pretty easy. Google for code examples. You could then write your wheel steering code, using a single rc servo.



    Hi Joe, thank you for your prompt reply. I agree with you about the encoders seems like it would be much more accurate. I'm just a beginner. I have finally gotten adept at installing libraries and I have modified a bunch of code to do what I want but I still don't quite understand the fundamentals. Usually the code is easily understandable where I can edit it to my purposes but the line follow sketch doesn't show how to control each motor individually. Can I just use Arduino code and assign a PWM pin to control each one? If so, which pins would be used on the motor breakout board? I tried to use the keywords from the keywords list to do something like:


    but it gives me errors so I'm not sure if those keywords are meant to be used that way.

    Anyways thanks for your time and help. Look forward to seeing your encoders idea in action!

    1 reply

    Hi swilus, There is no reason why you cannot use the Arduino functions directly, But that is what the go_forward() method is doing. Make sure you are referencing #include "MakeItRobotics.h" in your sketch project.

    Also make sure you have copied the MakeitRobotics folder to your library folder: C:\Program Files (x86)\Arduino\libraries

    Take a close look at the MakeitRobotics.cpp file, all of the methods are included in this file. If you take a close look at the motor methods you will see that they indeed reference the Arduino commands. Why re-invent something that is already written for you.

    In the MakeitRobotics.h file there is a class definition. In order to use these methods (not functions, there is a difference) you need to instantiate an instance of the class, then you can call it's methods. Look at the line "MakeItRobotics line_following;//declare object" in the linefollow.ino file. Instantiate means to create an object, in memory of the class.

    Look closely at how after the instantiation, all methods are then referenced by "line_following.". That is line_following followed by a period.

    If you cut up the linefollow.ino and remove everything except the code that calls the

    You should be able to turn on and off the motors. or just turn on or off one motor at a time.

    Put your robot up on blocks and do some testing to see how you can turn on and off the motors.

    Good luck, message me if you need any further assistance.



    Hello as of 9/14/14 neither of these links work. If it is a common problem I wish they would fix it. I purchased the Robotics Starter kit for my 12 year old son, he was very excited to get started on it. We got home in very fine print at the bottom of the box it says: *Requires Arduino Uno or other compatible board (Sold separately). Wish the print was larger or the sales person would have mentioned it.... We made a second trip to Radio Shack and purchased the Arduino Uno. He spent a couple hours assembling it and it looks great! He just needs the code so he can make it work.

    3 replies

    Hi Joe, Thanks for sharing! I am confused on how to alter the code to just control the motors directly with software and no sensors. For example I want to hook a piece of chalk to it and have it draw shapes on the ground. What would the basic code to do this look like or how do I find out?

    Thank you for your time and help if possible and thanks again for sharing.

    Kind regards!!!

    Hi, swilus, there are many ways to do this. I am not familiar with your background and what your skip level is with Arduino. But one easy way is to look at the linefollow.ino program and use the following commands to control the motors:




    The easy way would be use some sort of timer or counter to determine how long to leave the motors on and then use the above commands to turn on and off the motors. If you wanted to draw a larger and larger box you could use a variable and increment the variable to longer and longer times to increase the shape of the square.

    If you are a bit more advanced, check out the use of interrupts, interrupt vectors and timers to control how long the motors are turned on and off.

    The ultimate, something that I am getting ready to implement is to add wheel encoders and then you can count how many turns each wheel turns and then adjust your counter or timer based on how many times the wheels turns a full turn. I found wheel encoders out on ebay that fit the motor shafts on the Radio Shack Robot. Just have to make sure there is enough clearance for the encoders.

    I will post my code and hardware changes when I get some time. I have not started yet, Just got the encoders last week.