PR 記事には広告が含まれています

Milk Carton How to Make a Microcomputer Robot ,No.2 (Program Modification)


The milk pack microcomputer robot kit has a program written in the microcomputer. So you can play it by assembling it. But by modifying the program, you can change the color of the LED, increase the speed, or make it an original robot can do.

The last time was assembly and work.

Here, we will introduce how to modify the program.


Arduino installation

Microcomputer programs are written in Arduino, a world-famous programming language. It is a great introduction to microcomputer programming languages ​​because it has a lot of information and can be easily searched on the Internet or in books.

In order to program with Arduino, you need to install a development environment called Arduino IDE (software that can write programs and write to microcomputers) on your computer and make some settings.

The microcomputer board of the milk pack robot is the same microcomputer board as Leonardo in Arduino, so install Arduino and change the settings for Leonardo.

Click here for information on installing Arduino and setting up Leonardo.


Modification of the program

Download the program

Click here for the milk pack microcomputer robot program.

After downloading this zip file and extracting it, a folder called "milkpackRobot" appears.

Open this folder and you will find the milk pack robot program.

Clicking this "milkPackRobot.ino" will launch the Arduino IDE and allow you to modify the program.

Procedure for modifying the program

The procedure for modifying the program consists of these two steps.

  1. Modify the program with Arduino IDE
  2. Compile and write the program

The following can be easily modified.

  • Change motor speed
  • Change LED color
  • Change LED brightness
  • Change the blinking interval
  • Change servo motor speed
  • Change the servo motor angle range
  • Change the interval when the robot's foot tires alternate
  • Change what happens when a button on the remote control is pressed (for advanced users)

I will explain how to modify it by changing the LED color as the most obvious modification.

Modify the program with Arduino IDE


Look at lines 18-20 of the program.

int ledR = 0; //Red LED 0:OFF 1:ON
int ledG = 1; //Green LED 0:OFF 1:ON
int ledB = 1; //Blue LED 0:OFF 1:ON

This ledR sets whether to turn on the red LED. Similarly, ledG is the setting of whether to turn on the green LED and ledB is whether to turn on the blue LED. 0 is OFF, 1 is ON.

By combining the ON, OFF of the three colors of red, green, blue, you can change to various colors as shown in the table below.

LED color Black red green yellow  blue pink light blue white
ledR 0 1 0 1 0 1 0 1
ledG 0 0 1 1 0 0 1 1
ledB 0 0 0 0 1 1 1 1

In the first setting, ledR = 0, ledG = 1, and ledB = 1, so the LED glows light blue.

Let's try it pink. Pink has ledR = 1, ledG = 0, ledB = 1 according to the table. Modify the program as follows.

int ledR = 1; //Red LED 0:OFF 1:ON
int ledG = 0; //Green LED 0:OFF 1:ON
int ledB = 1; //Blue LED 0:OFF 1:ON

Compile and write the program

After remodeling, click the right arrow on the upper left and the program will be converted into microcomputer instructions (called compile) and written to the microcomputer.

Translate the Japanese in red letters from top to bottom
Remodel inside
Click the button
White text is displayed below


The LED that glowed in light blue once disappears and glows pink.

Modification of the program was successful.

If you get an error

If you enter numbers in full-width

When such an error occurs, the numbers are written with full-width characters "0" and "1".
When no microcomputer is connected

If "An error occurred while writing sketch" is displayed, the microcomputer and the PC are not connected by USB.
Specify the serial port to which the microcomputer is connected with [Tool]-> [Serial port]. For mac, it is displayed as usbmodem **** (* is a number). In the case of windows, it is COM ** (* is a number).


Translate the Japanese in red letters from top to bottom
← Change motor speed
← Change LED brightness
← Change blink interval
← Change servo motor movement
← Change the left and right alternate movement of the foot motor
Do not change here

Just like the color of the LED, by changing the number in the upper place,

  • Motor speed
  • LED color
  • LED brightness
  • Flashing interval
  • Servo motor rotation speed
  • Servo motor angle range
  • Interval when moving the foot motor alternately

You can easily change the above. Please make various changes and enjoy remodeling the program.


Structure of robot program

Robot programs always loop

The robot program is simple and looks like this

  1. Acquire sensor information
  2. Decide how to move
  3. Move motors, servos, etc.

These three are repeated at high speed, and the robot moves at short time intervals. This interval is called a control cycle. Normal robots repeat this loop at a rate of about 100 times per second, while robots that require faster and more accurate movement, such as factory robots, repeat more than 1000 times per second. The milk pack robot does not need to be very fast, so it loops at about 10 times per second.

Applying this loop to the milk pack robot program,

  1. Check which switch on the remote control is pressed
  2. The direction of the motor is determined by the combination of the switches
  3. Servo operation or blinking eyes

As above.

Check which switch on the remote control is pressed

The program is from the line "Remote control judgment" on the 132nd line to the 163rd line.

The remote control switches are paired with an upper and lower switch and a left and right switch.

  • When nothing is pressed: 5V
  • When the top (left) is pressed: 3V
  • When the bottom (right) is pressed: 2.5V
  • When both upper (left) and lower (right) are pressed: 1.9V

The voltage is as above. This signal is converted to a digital value by an AD converter.

  int upDownSwitch_ADValue = analogRead(UpDownSwitchPin);     //Read input pins of front and back buttons
  int LRSwitch_ADValue = analogRead(LRSwitchPin);     //Read input pins of left and right buttons

This value is distinguished by an "if" (programming language) that determines which button was pressed.

  if( upDownSwitch_ADValue < switchADValue_AB ) { //If the AD value is smaller than the switch AD Value_AB value, both up Dowm are pressed
    upSwitch = 1;
    downSwitch = 1;
  }else if( upDownSwitch_ADValue < switchADValue_B ) { //If the AD value is smaller than the switch AD Value_B value, Dowm is pressed
    upSwitch = 0;
    downSwitch = 1;
  }else if( upDownSwitch_ADValue < switchADValue_A ) { //If the AD value is smaller than the switch AD Value_A value, up is pressed
    upSwitch = 1;
    downSwitch = 0;
  }else { //Nothing is pressed if the AD value is greater than the a value
    upSwitch = 0;
    downSwitch = 0;

Determine the direction of the motor by the combination of switches

Now,  you know which switch is being pressed, up, down, left, and right, decide how it will work with that combination. In the program, from line 170 "According to remote control" to line 438.

As an example, let's look at the program when the UP is pressed from line 187.


  if( upSwitch == 1 & downSwitch == 0 & RSwitch== 0 & LSwitch== 0 ) {

Here, specify the combination in which the switch is pressed. upSwitch == 1 & downSwitch == 0 & RSwitch == 0 & LSwitch == 0
When the upper switch is ON, the lower switch is 0FF, the right switch is OFF, and the left switch is OFF, that is, only the upper switch is pressed.

in this case,

//Specify LED color
digitalWrite(ledRPin, ledR);
digitalWrite(ledGPin, ledG);
digitalWrite(ledBPin, ledB);

Set the color of each LED to the originally set value.

//Specify motor speed
analogWrite( motorR_PWMPin, speedR );
analogWrite( motorL_PWMPin, speedL );

Set the motor speed to the originally set value.

digitalWrite(motorR_APin, HIGH);
digitalWrite(motorR_BPin, LOW);
digitalWrite(motorL_APin, HIGH);
digitalWrite(motorL_BPin, LOW);

The motor has two signals, A and B. When A is ON (HIGH) and B is OFF (LOW), the motor moves forward, and when A is OFF (LOW) and B is ON (HIGH), the motor is back. When both A and B are turned OFF (LOW), the motor stops.

In this program, I want to move forward, so I turn on A and turn off B for both motors and go forward.

//Change the angle of both arms
servoRAngle = servoRAngle + servoSpeed;
servoLAngle = servoLAngle + servoSpeed;

Calculate the servo motor angle. Add the angle (servoSpeed) originally set to the current angle (servoRAngle). This will increase the angle and change the angle of the servo arm.

In this way, the future operation is determined by the state of the switch.


A slightly interesting feature is the hidden action of pressing up and down at the same time to dash, and pressing left and right at the same time to" Walk" by moving the motor alternately left and right.
For a "dash", the program starts at line 417.

//--- Behavior when both up and down are pressed ---
if( upSwitch == 1 & downSwitch == 1 & RSwitch== 0 & LSwitch== 0 ) {
  //Make the LED red
  digitalWrite(ledRPin, HIGH);
  digitalWrite(ledGPin, LOW);
  digitalWrite(ledBPin, LOW);

  //Maximize motor speed
  analogWrite( motorR_PWMPin, 255 );
  analogWrite( motorL_PWMPin, 255 );

The LED is forcibly set to red and the motor speed is also forcibly set to 255 and maximum. Your eyes will now glow red and dash at maximum speed.

Servo operation or blinking eyes

Line 442 of the program "LED blinking operation" to line 479.

First, start blinking
mabatakiCounter = mabatakiCounter -1; //Decrease blink counter by 1
if( mabatakiCounter == 1 ) {          //If the blink counter is 1, turn off the LED and blink
  ledRSave = digitalRead(ledRPin);     //Save current LED color
  ledGSave = digitalRead(ledGPin);
  ledBSave = digitalRead(ledBPin);
  digitalWrite(ledRPin, LOW );        //Turn off the LED and blink
  digitalWrite(ledGPin, LOW );
  digitalWrite(ledBPin, LOW );

Blinking decrements the first created random value by 1 each time the loop ends and turns off the LED when only the last one remains. It flashes once randomly. So it looks like the eyes of the robot are blinking.

if( mabatakiCounter <= 0 ) {          //When the blink counter goes below 0, light up the LED
  digitalWrite(ledRPin, ledRSave);
  digitalWrite(ledGPin, ledGSave);
  digitalWrite(ledBPin, ledBSave);
  mabatakiCounter = random(mabatakiMin, mabatakiMax);    //Set a random number on the counter    

In the next cycle that is turned off, the LED changes to the originally set color. Then, set the time until the next blink at random.

Shake your arm with the servo
servoRAngle = servoRAngle % 360;  //Angle values ​​range from 0 to 360
servoLAngle = servoLAngle % 360;

int servoRAngleTemp = sin( (double)servoRAngle/180.0*3.14 )*servoMax+90;  //The angle changes within the range of plus or minus specified angle around 90 degrees
int servoLAngleTemp = sin( (double)servoLAngle/180.0*3.14 )*servoMax+90;

Using a complicated formula, the angle is calculated so that the angle of the servo motor swings back and forth.

servoR.write(servoRAngleTemp);  //Set the angle on the servo

The angle is transmitted to the servo to change the angle.

Loop time interval

This robot loops 10 times per second. Line 489 determines the time interval.


delay (100) is an instruction to wait 100 milliseconds. 100 milliseconds means 10 times per second.

By setting 100 to 10, the loop time will be 10 milliseconds, and the loop will repeat 100 times per second. With a setting of 10, the loop will be faster, the blink will be faster, and the speed of swinging the arm will be faster.

Conversely, a setting of 1000 slows down the loop, slows down the response of the remote control, and slows down blinking and arm swing.


Modified the movement when the switch is pressed

For advanced users, let's modify the behavior when the switch is pressed.

Turn like a tank

When the right switch is pressed, it moves to the right, and when the left switch is pressed, it moves to the left.At this time, how the motor works

When moving to the right

  • Left motor forward
  • Right motor stopped

When moving to the left

  • Left motor stopped
  • Right motor moves forward

By doing this, you can move to the right or move to the left, but you can modify it to make a sharper turn. By rotating the stopped motor in reverse, you can turn on the spot like a tank.

When moving to the right

  • Left motor forward
  • Right motor rotates in reverse

When moving to the left

  • Left motor rotates in reverse
  • Right motor moves forward

Remodel like this. Let's remodel it.

First, lines 235, "Action when right is pressed." Next, Let's look at lines 248-251.

//Turn left motor to rotate right
digitalWrite(motorR_APin, LOW);
digitalWrite(motorR_BPin, HIGH);
digitalWrite(motorL_APin, HIGH);
digitalWrite(motorL_BPin, LOW);

Remodel motorR_BPin to "HIGH". Now the right motor has been modified from stop to reverse rotation.

In addition, let's look at lines 271 through 274 after line 258, “Action when left is pressed”.

digitalWrite(motorR_APin, HIGH);
digitalWrite(motorR_BPin, LOW);
digitalWrite(motorL_APin, LOW);
digitalWrite(motorL_BPin, HIGH);

Remodel motorL_BPin to "HIGH". Now the left motor has been modified from stop to reverse rotation.

This completes the conversion.

Click the right arrow button on the upper left to compile and write to the microcomputer.

Press the right and left buttons to rotate on the spot.

In this way, various modifications are possible.

If you get a compile error and you can't do anything else, download the original program again and try modifying it.

Robots that can be played with this program are sold here.