0% found this document useful (0 votes)
21 views51 pages

Robotics Program Report - Mbot2

The document outlines a multi-day introduction to Python programming, covering various tasks and projects including displaying numbers, creating a secret code, and basic calculations related to a fictional bakery. It includes practical coding examples and keywords for using the CyberPi and mBot2 devices. The content is structured into tasks that progressively teach programming concepts through engaging scenarios.

Uploaded by

Rohini Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views51 pages

Robotics Program Report - Mbot2

The document outlines a multi-day introduction to Python programming, covering various tasks and projects including displaying numbers, creating a secret code, and basic calculations related to a fictional bakery. It includes practical coding examples and keywords for using the CyberPi and mBot2 devices. The content is structured into tasks that progressively teach programming concepts through engaging scenarios.

Uploaded by

Rohini Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Day 1 - Intro to Python

Task 1 - Info about Python:


Founder Guido van Rossum
Found in 1989
What is Python? Python is a high-level, easy-to-learn
programming language used to build
websites, apps, games, AI, data science
projects—and robots like mBot2!
!
"
#

Why is Python • Simple syntax (easy to read and write)
Popular? • Tons of libraries (pre-written code to
do tasks easily)
• Beginner-friendly
• Powerful for automation, AI, and
robotics

Task 2 - Display Program:


! Keywords Learnt:
• import cyberpi
• import event
• import time
• @event.is_press('a')
• cyberpi.display.set_brush({R}, {G}, {B})
• cyberpi.display.show_label(“{TEXT}”, {FONTSIZE},
int({X}), int({Y}), index=0)
• cyberpi.display.clear()
• time.sleep(1)
• cyberpi.console.print(“{TEXT}”)
• cyberpi.console.println(“{TEXT}”)

“To display the numbers 1 to 8 on the cyberpi display one by


one in different colors.”
" Image Preview:

# main.py: Clear
import cyberpi
import event
import time
@event.is_press('a')
def is_btn_press():
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("1", 24, int(0), int(0), index=0)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("2", 24, int(55), int(0), index=1)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("3", 24, int(110), int(0), index=2)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("4", 24, int(0), int(50), index=3)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("5", 24, int(55), int(50), index=4)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("6", 24, int(110), int(50), index=5)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("7", 24, int(0), int(100), index=6)
time.sleep(0.3)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("8", 24, int(55), int(100), index=7)
time.sleep(0.3)
cyberpi.display.clear()
Day 2 - Python Programs
Task 1 - Secret Code Maker:
“Create word1 and word2, combine using +, and print the
secret code.”

# main.py: Clear

word_1 = "dragon"
word_2 = "fly"
print(word_1+word_2)

Task 2 - Potion Calculator:


“1 bottle = 150 ml. We are mixing 3 bottles. Calculate and
print total ml.”

# main.py: Clear

pot_1=150
pot_2=150
pot_3=150
print("Your Potion has",pot_1+pot_2+pot_3,"ml in total!")

Task 3 - Energy Points Doubler:


“You start with 45 energy. You collect a crystal that doubles it.
Print new energy.”

# main.py: Clear

energy_points=45
print("After the crystal, my energy is", energy_points*2,
"Points!")
Task 4 - Treasure Counter:
“Add 15 gold + 23 silver coins. Print total coins.”

# main.py: Clear

gold_coins=15
silv_coins=23
print("Total Coins:", gold_coins+silv_coins, "coins")

Task 5 - Magic Power Upgrade:


“Magic level = 40. Add 25 power. Print new level.”

# main.py: Clear

magic_level=40
magic_spell=25
print("New Magic Level:", magic_level+magic_spell)

Task 6 - Knight Name Generator:


“Combine first_name and title. Print knight name.”

# main.py: Clear

first_name="Arthur"
title=" the brave."
print("Your knight name is:", first_name+title)

Task 7 - Speed Booster:


“Speed = 12 km/h. Add 8 km/h. Print new speed.”
# main.py: Clear

current_speed=12
booster_speed=8
new_speed=current_speed+booster_speed
print("New Speed:",new_speed,"km/h")
Task 8 - Dragon Age Doubler:
“Age = 150. Double it. Print new age.”

# main.py: Clear

current_age=150
magic_age=2
new_age=current_age*magic_age
print("The dragon's new age is:", new_age, "years.")

Task 9 - Wizard’s Book Collection:


“Start with 85 books. Donate 17. Print books left.”

# main.py: Clear

my_books=85
donated_books=17
new_books= my_books-donated_books
print("Books left:", new_books)

Task 10 - Rocket Launch Countdown:


“Store countdown = 10. Print “T-minus 10 seconds!””

# main.py: Clear

for i in range(1,11):
print("T-Minus", i, "seconds!")
i=i+1
print("Launch!")

Task 11 - Fruit Seller:


“Sold 12 apples and 15 bananas. Print total fruits sold.”

# main.py: Clear

apples_sold=12
bananas_sold=15
total_sales=apples_sold+bananas_sold
print("Total Fruits Sold:", total_sales)
Task 12 - Lucky Number Creator:
“Multiply 7 × 9. Print lucky number.”

# main.py: Clear

no_1=7
no_2=9
new_no=no_1*no_2
print("Your lucky number is:", new_no)

Task 13 - Balloon Inflation:


“Each balloon = 2 liters. You have 7 balloons. Print total air
needed.”
# main.py: Clear

air_held=2
balloons=7
air_needed=air_held*balloons
print("Total air needed:", air_needed,"litres.")

Task 14 - Intro to Movement ( Square &


Rectangle with Swing Turn and Point Turn):
“Assign button A to make square with point turn and button B
to make rectangle with swing turn.”

! Keywords Learnt:
• mbot2.straight({DISTANCE IN CM})
• mbot2.forward({SPEED IN RPM})
• mbot2.backward({SPEED IN RPM})
• mbot2.turn({ANGLE})
• mbot2.EM_turn({DEGREES}, {TIME})
• mbot2.EM_stop({MOTOR})
• mbot2.drive_speed({SPEED OF EM1}, {SPEED OF EM2})
# main.py: Clear
import cyberpi
import mbot2
import event
import time

@event.is_press('a')
def is_btn_press():
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward", 24, 20, 50, index=0)
mbot2.straight(60)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Turn Left", 24, 10, 50, index=0)
mbot2.turn(92)
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward", 24, 20, 50, index=0)
mbot2.straight(60)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Turn Left", 24, 10, 50, index=0)
mbot2.turn(92)
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward", 24, 20, 50, index=0)
mbot2.straight(60)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Turn Left", 24, 10, 50, index=0)
mbot2.turn(92)
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward", 24, 20, 50, index=0)
mbot2.straight(60)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Turn Left", 24, 10, 50, index=0)
mbot2.turn(92)
cyberpi.display.clear()

@event.is_press('b')
def rectangle_swing_turn():
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward 60", 24, 20, 50, index=0)
mbot2.straight(60)

cyberpi.display.set_brush(255, 100, 0)
cyberpi.display.show_label("Swing Turn", 24, 10, 50, index=0)
mbot2.EM_turn(-90, 1)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward 120", 24, 20, 50, index=0)
mbot2.straight(120)

cyberpi.display.set_brush(255, 100, 0)
cyberpi.display.show_label("Swing Turn", 24, 10, 50, index=0)
cyberpi.display.show_label("Swing Turn", 24, 10, 50, index=0)
mbot2.EM_turn(-90, 1)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward 60", 24, 20, 50, index=0)
mbot2.straight(60)

cyberpi.display.set_brush(255, 100, 0)
cyberpi.display.show_label("Swing Turn", 24, 10, 50, index=0)
mbot2.EM_turn(-90, 1)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Forward 120", 24, 20, 50, index=0)
mbot2.straight(120)

cyberpi.display.set_brush(255, 100, 0)
cyberpi.display.show_label("Swing Turn", 24, 10, 50, index=0)
mbot2.EM_turn(-90, 1)

cyberpi.display.clear()

Task 15 - Intro to Movement (Triangle):


“Make the robot move in triangular shape with display
showing turns and which side(out of 3) the robot is moving in.”

# main.py: Clear

import cyberpi
import mbot2
import event
import time

# Draw equilateral triangle on 'up' press


@event.is_press('up')
def is_triangle_press():
cyberpi.display.clear()

# 1st side
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("1st Side", 24, 20, 50, index=0)
mbot2.straight(60)
mbot2.EM_stop()
mbot2.turn(120)

# 2nd side
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("2nd Side", 24, 20, 50, index=0)
mbot2.straight(60)
mbot2.EM_stop()
mbot2.turn(120)

# 3rd side
cyberpi.display.set_brush(0, 0, 255)
cyberpi.display.show_label("3rd Side", 24, 20, 50, index=0)
mbot2.straight(60)
mbot2.EM_stop()
mbot2.turn(120)

$ Issues Faced:
Some times the parenthesis is missed at end end functions and that line
doesnt execute.
Day 3 - The Bakery of Numbers
% Storytime:
In a peaceful village called Digiton, there lived a young baker named Nora.
Nora was famous for baking the finest cakes, cookies, and breads — but not
with regular ingredients. She used numbers to bake!

One day, the Mayor of Digiton announced a math festival. To win the Golden
Spoon, Nora must bake using perfect calculations — involving addition,
subtraction, multiplication, and division. With the help of her clever cat
Minus, Nora must solve a series of challenges in her magical bakery.

Task 1 - Basic Ingredients:


“Nora starts with 10 eggs and buys 6 more. Store both in
variables and print the total eggs.”

# main.py: Clear

current_eggs=10
eggs_added=6
new_eggs1=current_eggs+eggs_added
print("Eggs Nora already has:",current_eggs,"eggs.")
print("Eggs Nora bought now:",eggs_added,"eggs.")
print("Total Eggs Nora has now:",new_eggs1,"eggs.")

Task 2 - Flour Usage:


“Nora has 25 cups of flour. She uses 7 cups to bake cookies.
How much is left?”

# main.py: Clear

current_flour=25
flour_used=7
new_flour=current_flour-flour_used
print("Flour Nora already has:",current_flour,"cups.")
print("Flour Nora has used to bake cookies:",flour_used,"cups.")
print("Remaining flour Nora has now:",new_flour,"cups.")
Task 3 - Bread Orders:
“Each loaf of bread requires 4 cups of flour. How many loaves
can she bake with 36 cups?”

# main.py: Clear

flour_1bread=4
flour_available=36
bread=flour_available/flour_1bread
print("Flour available for baking bread:",flour_available,"cups.")
print("Flour needed for baking 1 bread:",flour_1bread,"cups.")
print("No. of Loaves that can be made:",bread,"loaves.")

Task 4 - Pie Profit:


“Each pie sells for 8 coins. Nora sells 5 pies. How much money
does she make?”

# main.py: Clear

pies_sold=5
cost_1pie=8
profit=pies_sold*cost_1pie
print("Pies sold by Nora:",pies_sold,"pies.")
print("Cost of 1 pie:",cost_1pie,"coins.")
print("Money earned by Nora:",profit,"coins.")

Task 5 - Sharing Cookies:


“Nora bakes 48 cookies. She wants to pack them into boxes of
6. How many boxes does she need?”

# main.py: Clear

cookies_baked=48
box_capacity=6
boxes=cookies_baked/box_capacity
print("Cookies baked by Nora:",cookies_baked,"cookies.")
print("Each box capacity:",box_capacity,"cookies.")
print("Total boxes needed:",boxes,"boxes.")
Task 6 - Total Ingredients:
“Nora uses 2 cups of sugar, 3 cups of flour, and 1 cup of milk
for a new cake. What’s the total ingredient amount?”

# main.py: Clear

sugar=2
flour=3
milk=1
total_ingredients=sugar+flour+milk
print("Cups of sugar used:",sugar)
print("Cups of flour used:",flour)
print("Cups of milk used:",milk)
print("Total ingredients used:",total_ingredients,"cups.")

Task 7 - Scaling Recipe:


“To double a cake recipe, multiply each ingredient by 2: 3 eggs,
2 cups of milk, 4 spoons of sugar. Store and print the doubled
quantities.”
# main.py: Clear

pies_sold=5
cost_1pie=8
profit=pies_sold*cost_1pie
print("Pies sold by Nora:",pies_sold,"pies.")
print("Cost of 1 pie:",cost_1pie,"coins.")
print("Money earned by Nora:",profit,"coins.")

Task 8 - Leftover Math:


“Nora made 45 tarts. She gave away 38. How many are left?”
# main.py: Clear

pies_sold=5
cost_1pie=8
profit=pies_sold*cost_1pie
print("Pies sold by Nora:",pies_sold,"pies.")
print("Cost of 1 pie:",cost_1pie,"coins.")
print("Money earned by Nora:",profit,"coins.")
Task 9 - Equal Division:
“Nora made 60 mini cakes for 4 customers. How many does
each customer get?”

# main.py: Clear

total_mini_cakes=60
customers=4
cakes_each=total_mini_cakes/customers
print("Total mini cakes made:",total_mini_cakes)
print("Total customers:",customers)
print("Each customer gets:",cakes_each,"cakes")

Task 10 - Bakery Expenses:


“Nora spends 20 coins on flour, 15 on sugar, and 10 on eggs.
What’s the total cost?”
# main.py: Clear

flour_cost=20
sugar_cost=15
eggs_cost=10
total_cost=flour_cost+sugar_cost+eggs_cost
print("Cost of flour:",flour_cost,"coins")
print("Cost of sugar:",sugar_cost,"coins")
print("Cost of eggs:",eggs_cost,"coins")
print("Total cost:",total_cost,"coins")
Day 4 - Intro to LED
! Keywords Learnt:
• cyberpi.led.on({RGB VALUE})
• cyberpi.led.show({COLOUR FOR EACH LED})
• cyberpi.led.play({ANIMATION_NAME})
• cyberpi.led.set_bri({BRIGHTNESS})
• cyberpi.led.add_bri({INCREMENT OF BRIGHTNESS})
• cyberpi.led.get_bri()

" Image Preview:

Task 1 - Rainbow LED Display:


When the “up” button is pressed on the CyberPi, display the
colors of the rainbow (VIBGYOR) one by one on both the LED
and the screen. Each color should show for 1 second with its
name labeled on the screen. After displaying all the colors,
turn off the LED and clear the screen.
# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('up')
def led():
cyberpi.display.set_brush(148, 0, 211)
cyberpi.display.show_label("Violet", 24, 40, 50, index=0)
cyberpi.display.show_label("Violet", 24, 40, 50, index=0)
cyberpi.led.on(148, 0, 211)
time.sleep(1)

cyberpi.display.set_brush(75, 0, 130)
cyberpi.display.show_label("Indigo", 24, 40, 50, index=0)
cyberpi.led.on(75, 0, 130)
time.sleep(1)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.display.show_label("Blue", 24, 40, 50, index=0)
cyberpi.led.on(0, 0, 255)
time.sleep(1)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Green", 24, 40, 50, index=0)
cyberpi.led.on(0, 255, 0)
time.sleep(1)

cyberpi.display.set_brush(255, 255, 0)
cyberpi.display.show_label("Yellow", 24, 40, 50, index=0)
cyberpi.led.on(255, 255, 0)
time.sleep(1)

cyberpi.display.set_brush(255, 127, 0)
cyberpi.display.show_label("Orange", 24, 40, 50, index=0)
cyberpi.led.on(255, 127, 0)
time.sleep(1)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Red", 24, 40, 50, index=0)
cyberpi.led.on(255, 0, 0)
time.sleep(1)

cyberpi.led.off()
cyberpi.display.clear()

$ Issues Faced:
When joystick “up” and joystick “down” are two different events, the code will
not execute if both the functions are defined in the same name, i.e
“def is_btn_press:”
Task 2 - Control LED Brightness and Display
Colors Dynamically:
When the up arrow is pressed, the LED brightness increases
step by step and changes colour based on how bright it is. The
display also shows the brightness value in matching colours.
Once brightness reaches 100, it shows the word “OVER” using
colourful letters.
# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('up')
def led():
cyberpi.led.on(255,255,255)
cyberpi.led.set_bri(0)
for i in range(0,11):
time.sleep(1)
cyberpi.led.add_bri(10)
if cyberpi.led.get_bri()>0 and cyberpi.led.get_bri()<21:
cyberpi.led.on(255,0,0)
cyberpi.display.set_brush(255,0,0)
cyberpi.display.show_label(cyberpi.led.get_bri(), 24,
int(50), int(50), index=0)
elif cyberpi.led.get_bri()>21 and cyberpi.led.get_bri()<41:
cyberpi.led.on(0,255,0)
cyberpi.display.set_brush(0,255,0)
cyberpi.display.show_label(cyberpi.led.get_bri(), 24,
int(50), int(50), index=0)
elif cyberpi.led.get_bri()>41 and cyberpi.led.get_bri()<61:
cyberpi.led.on(0,0,255)
cyberpi.display.set_brush(0,0,255)
cyberpi.display.show_label(cyberpi.led.get_bri(), 24,
int(50), int(50), index=0)
elif cyberpi.led.get_bri()>61 and cyberpi.led.get_bri()<81:
cyberpi.led.on(255,0,255)
cyberpi.display.set_brush(255,0,255)
cyberpi.display.show_label(cyberpi.led.get_bri(), 24,
int(50), int(50), index=0)
elif cyberpi.led.get_bri()>81 and cyberpi.led.get_bri()<99:
cyberpi.led.on(255,255,0)
elif cyberpi.led.get_bri()>81 and cyberpi.led.get_bri()<99:
cyberpi.led.on(255,255,0)
cyberpi.display.set_brush(255,255,0)
cyberpi.display.show_label(cyberpi.led.get_bri(), 24,
int(50), int(50), index=0)
elif cyberpi.led.get_bri()==100:
cyberpi.display.clear()
cyberpi.display.set_brush(255,0,0)
cyberpi.display.show_label("O", 24, int(30), int(50),
index=1)
time.sleep(1)
cyberpi.display.set_brush(0,255,0)
cyberpi.display.show_label("V", 24, int(50), int(50),
index=2)
time.sleep(1)
cyberpi.display.set_brush(0,0,255)
cyberpi.display.show_label("E", 24, int(70), int(50),
index=3)
time.sleep(1)
cyberpi.display.set_brush(255,255,0)
cyberpi.display.show_label("R", 24, int(90), int(50),
index=4)
time.sleep(1)

Task 3 - Spiral Movement (Draw & Erase):


# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('a')
def spiral():
mbot2.drive_speed(100,-80,5)
mbot2.drive_speed(100,-60,5)
mbot2.drive_speed(100,-40,5)
mbot2.drive_speed(100,-20,5)
mbot2.drive_speed(100,-5,5)
mbot2.drive_speed(-5,100,5)
mbot2.drive_speed(-20,100,5)
mbot2.drive_speed(-40,100,5)
mbot2.drive_speed(-60,100,5)
mbot2.drive_speed(-80,100,5)
Day 5 - Conditional Statements
Task 1 – Spaceship Direction Check:
“If the assigned_value is greater than or equal to the
direction_code, print that the spaceship entered the wormhole.
Otherwise, print that it got lost in deep space.”
# main.py: Clear

direction_code = 6
assigned_value = 6
if assigned_value < direction_code:
print("The spaceship got lost in deep space!")
else:
print("The spaceship entered the wormhole!")

Task 2 - Exam Pass or Fail:


“If Raju’s score is equal to or greater than the pass mark, print
that he passed the exam.”
# main.py: Clear

pass_mark = 40
your_score = 40
if your_score >= pass_mark:
print("You have successfully passed the exam!")

Task 3 - Temperature Category:


“Check if the temperature is cold, normal, or hot. Cold is below
30, normal is exactly 30, and hot is above 30.”
# main.py: Clear

temperature = 30
if temperature == 30:
print("Normal Temperature!")
elif temperature > 30:
print("Warm Temperature!")
else:
print("Cold Temperature!")
Task 4 – Rocket Fuel Check:
“Fuel below 70: Low fuel. Fuel 70 or more: Sufficient. If fuel is
100 or more: Rocket is ready.”
# main.py: Clear

fuel = 75
if fuel >= 100:
print("Rocket is ready to launch!")
elif fuel >= 70:
print("Sufficient fuel to launch the rocket!")
else:
print("Low fuel! Can't launch the rocket!")

Task 5 - Radiation Danger Levels:


“Radiation level ≤ 2: Safe Zone. ≤ 4: Moderate Danger. ≥ 8:
Critical. Otherwise: High Danger.”
# main.py: Clear

radiation_level = 6
if radiation_level <= 2:
print("Safe Zone")
elif radiation_level <= 4:
print("Caution: Moderate Danger")
elif radiation_level >= 8:
print("Caution: Critical")
else:
print("Caution: High Danger")

Task 6 - Oxygen Level Status:


“Oxygen ≤ 20: Critical. ≥ 80: Optimal. ≥ 40: Acceptable.”
# main.py: Clear

oxygen = 45
if oxygen <= 20:
print("Critical! Oxygen very low!")
elif oxygen >= 80:
print("Oxygen optimal")
elif oxygen >= 40:
print("Oxygen level Acceptable")
Task 7 – Distance to Planet:
“< 1: Near Planet. > 10: Far Planet. ≥ 4: Medium Range Planet.”
# main.py: Clear

distance = 4.5
if distance < 1:
print("Near Planet")
elif distance > 10:
print("Far Planet")
elif distance >= 4:
print("Medium Range Planet")

Task 8 - Battery Level:


“≤ 10: Battery Critical. ≥ 80: Battery Full. ≥ 50: Battery OK.”
# main.py: Clear

battery = 10
if battery <= 10:
print("Battery Critical")
elif battery >= 80:
print("Battery Full")
elif battery >= 50:
print("Battery OK")

Task 9 - Match Outcome:


“If England’s score > Australia: England won. If less: England
lost. Else: Match tied.”
# main.py: Clear

england = 270
australia = 270
if england > australia:
print("England won the match")
elif england < australia:
print("England lost the match")
else:
print("Match tied")
Task 10 – Bowler’s Economy Rate:
“≤ 3: Highly economical. ≤ 5: Good control. Else: Too expensive.”
# main.py: Clear

economy_rate = 4.2
if economy_rate <= 3:
print("Highly economical")
elif economy_rate <= 5:
print("Good control")
else:
print("Too expensive")

Task 11 - Bowler’s Wickets:


“If 5 wickets: Match-winning spell. ≥ 2 wickets: Effective
bowler. Else: Needs improvement.”
# main.py: Clear

wickets = 3
if wickets == 5:
print("Match-winning spell!")
elif wickets >= 2:
print("Effective bowler")
else:
print("Needs improvement")

Task 12 - Cricket Player Runs:


“≤ 50: Below average. ≥ 100: Outstanding. Else: Decent knock.”
# main.py: Clear

runs = 105
if runs <= 50:
print("Below average performance")
elif runs >= 100:
print("Outstanding! Century scored")
else:
print("Decent knock")
Day 6 - Maze Solving
" Image Preview:

Task 1 - Solving the Maze:


Make the robot move precisely to solve the maze.
# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('a')
def spiral():
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(40)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(27.5)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(24)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(28)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(25)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.straight(25)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(30)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(42)

Task 2 - Add LED & Display:


# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('a')
def spiral():
cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(40)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(27.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(28.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(25)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(30.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 255, 0)
cyberpi.led.on(0, 255, 0)
cyberpi.display.show_label("Go Forward", 16, 24, 56, index=0)
mbot2.straight(60)

cyberpi.display.set_brush(255, 215, 0)
cyberpi.led.on(255, 215, 0)
cyberpi.display.show_label("Victory!", 16, 32, 56, index=0)
Task 3 – Reverse Solving the Maze:
Make the robot start from the finishing point and finish the
maze entirely in reverse.
# main.py: Clear

import cyberpi
import mbot2
import event
import time

@event.is_press('a')
def spiral():
cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-60)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-30.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-25)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-28.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Right", 16, 24, 56, index=0)
mbot2.turn(92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-24)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-27.5)

cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0)
cyberpi.display.show_label("Turn Left!", 16, 28, 56, index=0)
mbot2.turn(-92)

cyberpi.display.set_brush(0, 0, 255)
cyberpi.led.on(0, 0, 255)
cyberpi.display.show_label("Go Backward", 16, 16, 56, index=0)
mbot2.straight(-40)
Day 7 - Data Type Conversions
in Python
Task 1 - Potion Upgrade:
“Convert the potion strength from string to float, multiply by 2,
and print the upgraded potion strength.”
# main.py: Clear

pot_strength = "3.5"
new_strength = float(pot_strength) * 2
print("The new potion strength is:", new_strength)

Task 2 - Coin Collector:


“Change both coin values from strings to integers, add them
together, and display the total number of coins.”
# main.py: Clear

coin_1 = "15"
coin_2 = "25"
total = int(coin_1) + int(coin_2)
print("The new total is", total, "coins.")

Task 3 - Spaceship Fuel:


“Subtract used fuel from total available, convert the result to
integer, and display how much fuel is left.”
# main.py: Clear

fuel_load = 80.75
fuel_used = 30.50
fuel_left = int(fuel_load - fuel_used)
print("Fuel Left:", fuel_left, "l.")
Task 4 - Brain Power Boost:
“Convert the string IQ and puzzle bonus to integers, add them,
and print the updated IQ score.”
# main.py: Clear

iq = "120"
iq_puzzle = "10"
new_iq = int(iq) + int(iq_puzzle)
print("Your IQ Level is:", new_iq)

Task 5 - Armor Weight:


“Convert both armor and player weight from strings to floats,
add them, convert to int, and print total weight.”
# main.py: Clear

armor_weight = "18.6"
your_weight = "11.4"
tot_weight = int(float(armor_weight) + float(your_weight))
print("Your Total Weight with Armor is:", tot_weight, "kgs.")

Task 6 - Racing Speed:


“Convert speed and turbo boost from strings to floats, add
them, convert to int, and show final speed.”
# main.py: Clear

speed = "97.5"
boost = "12.5"
total_speed = int(float(speed) + float(boost))
print("The car's speed is:", total_speed, "km/hr.")

Task 6 - Racing Speed:


“Convert speed and turbo boost from strings to floats, add
them, convert to int, and show final speed.”
# main.py: Clear

speed = "97.5"
boost = "12.5"
total_speed = int(float(speed) + float(boost))
print("The car's speed is:", total_speed, "km/hr.")
Task 7 - Building Blocks:
“Convert current and collected blocks to integers, add them,
and print the total number of building blocks.”
# main.py: Clear

blocks = "300"
collected = "125"
total_blocks = int(int(blocks) + int(collected))
print("Total number of blocks:", total_blocks)

Task 8 - Accuracy Rating:


“Convert accuracy from string to float, round it to the nearest
whole number, and print the rounded value.”
# main.py: Clear

accuracy = "82.5"
rounded_accuracy = round(float(accuracy))
print("Rounded accuracy is:", rounded_accuracy)

Task 9 - Battery Charge:


“Convert charge from string to float, add a 10.5% boost, and
print the updated battery percentage.”
# main.py: Clear

charge = "65.25"
charge_float = float(charge)
new_charge = float(charge_float + (0.105 * charge_float))
print("Battery charge after boost is:", new_charge, "%")

Task 10 - Crafting Cost:


“Convert wood and metal costs from strings to integers, add
them, and print the total crafting cost.”
# main.py: Clear

speed = "97.5"
boost = "12.5"
total_speed = int(float(speed) + float(boost))
print("The car's speed is:", total_speed, "km/hr.")
Task 11 - Magic Power Multiplier:
“Convert power from string to float, multiply it by 2.5, and
print your new magic power level.”
# main.py: Clear

blocks = "300"
collected = "125"
total_blocks = int(int(blocks) + int(collected))
print("Total number of blocks:", total_blocks)

Task 12 - Backpack Upgrade:


“Convert original and extra slots to integers, add them, and
display the upgraded backpack slot capacity.”
# main.py: Clear

slots = "25"
extra_slots = "15"
total_slots = int(int(slots) + int(extra_slots))
print("Total backpack slots:", total_slots)

Task 13 - Fruit Juice Mixer:


“Convert both juice quantities from strings to floats, add them,
and print the total juice volume mixed.”
# main.py: Clear

apple_juice = "1.5"
orange_juice = "2.0"
total_juice = float(float(apple_juice) + float(orange_juice))
print("Total juice mixed is:", total_juice, "liters")

Task 14 - Engine Overheat Check:


“Convert the temperature and cooling values to floats, subtract
them, and print the reduced engine temperature.”
# main.py: Clear

temperature = "102.7"
cooling = "7.2"
new_temperature = float(float(temperature) - float(cooling))
print("New engine temperature is:", new_temperature, "°C")
Day 8 - Audio & Recording
! Keywords Learnt:
• cyberpi.audio.record()
• cyberpi.audio.stop_record()
• cyberpi.audio.play_record_until()
• cyberpi.audio.play_record()
• cyberpi.audio.play_music({NOTE_VALUE}, {DURATION})
• cyberpi.audio.add_tempo({TEMPO_INCREMENT})
• cyberpi.audio.set_tempo({NEW_TEMPO})
• cyberpi.audio.stop()
• cyberpi.audio.play_tone({FREQUENCY}, {DURATION})
• cyberpi.audio.add_vol({VOLUME_INCREMENT})
• cyberpi.audio.set_vol({VOLUME_LEVEL})
• cyberpi.audio.get_vol()
• cyberpi.audio.get_tempo()
• cyberpi.display.show_label(cyberpi.audio.get_vol(),
{FONT_SIZE}, {X_POSITION}, {Y_POSITION}, index =
{LABEL_INDEX})
• cyberpi.display.show_label(cyberpi.audio.get_tempo(),
{FONT_SIZE}, {X_POSITION}, {Y_POSITION}, index =
{LABEL_INDEX})

Task 1 - Count from 1 to 5:


“The robot must record the audio of the user from 1 to 5 and
repeat every number after the user. It shows the number said
on the display and also three colours for the LED must be
used; Green for Recording; Red for Stopped Recording; and
Blue during playback.”
# main.py: Clear

import event, time, cyberpi

@event.start
def on_start():
for i in range(1, 6):
say_number(i)

def say_number(n):
if n == 1:
if n == 1:
say_text, said_text, say_chars, said_chars = "Say: 1", "You
said: 1", 6, 11
elif n == 2:
say_text, said_text, say_chars, said_chars = "Say: 2", "You
said: 2", 6, 11
elif n == 3:
say_text, said_text, say_chars, said_chars = "Say: 3", "You
said: 3", 6, 11
elif n == 4:
say_text, said_text, say_chars, said_chars = "Say: 4", "You
said: 4", 6, 11
else:
say_text, said_text, say_chars, said_chars = "Say: 5", "You
said: 5", 6, 11

x1 = (128 - say_chars * 12) // 2


x2 = (128 - said_chars * 12) // 2
y = (128 - 24) // 2

cyberpi.display.clear()
cyberpi.display.show_label(say_text, 24, x1, y, index=0)
cyberpi.led.on(0, 255, 0)
time.sleep(0.5)
cyberpi.audio.record()
time.sleep(5)
cyberpi.audio.stop_record()

time.sleep(1)
cyberpi.led.on(255, 0, 0)
cyberpi.display.clear()
cyberpi.display.show_label(said_text, 24, x2, y, index=0)
cyberpi.audio.set_vol(100)
time.sleep(1)
cyberpi.led.on(0, 0, 255)
cyberpi.audio.play_record_until()
cyberpi.led.off()
time.sleep(1)
Day 9 - Ultrasonic Sensor
! Keywords Learnt:
• import mbuild
• mbuild.ultrasonic2.get()
• mbuild.ultrasonic2.set_bri({BRIGHTNESS}, "all")
• mbuild.ultrasonic2.add_bri({BRIGHTNESS}, "all”)
• mbuild.ultrasonic2.get_bri()
• mbuild.ultrasonic2.play(“{EMOTION}”)

Task 1 - Ambulance(Audio & Ultrasonic):


“This robot behaves like an emergency vehicle when an
obstacle is detected. If no object is within 20 cm, the robot
moves forward and displays “Go” with a green LED and green
text. But when it detects an obstacle, the robot stops
completely and switches to emergency mode — it displays
“Stop”, turns the LED red, and plays a 1000 Hz alert tone, just
like an ambulance siren, warning of danger or obstruction.”
# main.py: Clear

import event, time, cyberpi, mbuild, mbot2

@event.start
def on_start():
while True:
if mbuild.ultrasonic2.get(1) <= 20:
mbot2.forward()
cyberpi.led.on(0,255,0)
cyberpi.display.set_brush(0,255,0)
cyberpi.display.show_label("Go", 24, "center", index=0)
else:
mbot2.EM_stop("all")
cyberpi.audio.set_vol(100)
cyberpi.audio.play_tone(int(1000), 0.7)
cyberpi.led.on(255,0,0)
cyberpi.display.set_brush(255,0,0)
cyberpi.display.show_label("Stop", 24, "center", index=0)
Task 2 - Ultrasonic Range Sensor:
“The robot continuously checks the distance using an
ultrasonic sensor. If an object is less than 20 cm away, it stops,
plays a 1000 Hz warning tone, displays “Too Close!” in red, and
the LED turns red, simulating an ambulance alert. If the path
is clear, the robot moves forward and shows “Moving…”. Based
on the distance range, the LED changes colour and displays
messages like “Range 20-30”, “Range 50-60”, etc., giving real-
time feedback with both text and colour.”
# main.py: Clear

import event, time, cyberpi, mbuild, mbot2

@event.start
def on_start():
while True:
distance = mbuild.ultrasonic2.get(1)
cyberpi.display.clear()

if distance < 20:


mbot2.EM_stop("all")
cyberpi.audio.set_vol(100)
cyberpi.audio.play_tone(1000, 0.7)
cyberpi.led.on(255, 0, 0)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Too Close!", 24, "center",
index=0)

else:
mbot2.forward()
cyberpi.display.set_brush(255, 255, 255)
cyberpi.display.show_label("Moving...", 18, "top",
index=0)

if 20 <= distance <= 30:


cyberpi.led.on(0, 255, 255)
cyberpi.display.set_brush(0, 255, 255)
cyberpi.display.show_label("Range 20-30", 24,
"center", index=1)

elif 35 <= distance <= 45:


cyberpi.led.on(0, 0, 255)
cyberpi.display.set_brush(0, 0, 255)
cyberpi.display.show_label("Range 35-45", 24,
"center", index=1)

elif 50 <= distance <= 60:


cyberpi.led.on(128, 0, 128)
cyberpi.display.set_brush(128, 0, 128)
cyberpi.display.show_label("Range 50-60", 24,
"center", index=1)

elif 65 <= distance <= 75:


cyberpi.led.on(255, 165, 0)
cyberpi.display.set_brush(255, 165, 0)
cyberpi.display.show_label("Range 65-75", 24,
"center", index=1)

elif 80 <= distance <= 95:


cyberpi.led.on(255, 255, 0)
cyberpi.display.set_brush(255, 255, 0)
cyberpi.display.show_label("Range 80-95", 24,
"center", index=1)

elif 100 <= distance <= 110:


cyberpi.led.on(255, 0, 0)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.display.show_label("Range 100-110", 24,
"center", index=1)

else:
cyberpi.led.on(0, 255, 0)
cyberpi.display.set_brush(0, 255, 0)
cyberpi.display.show_label("Moving...", 24,
"center", index=1)
Day 9 - Ultrasonic Sensor Maze
" Image Preview:

Task 1 - Solving the Maze:


“Make the robot move precisely to solve the maze using
ultrasonic sensor to navigate.”
# main.py: Clear

import event, time, cyberpi, mbuild, mbot2


import time

@event.is_press('a')
def is_btn_press():
while True:
cyberpi.display.show_label(str(str('U.S.= ') +
str(mbuild.ultrasonic2.get(1))) + str('cm'), 16, "bottom_mid",
index= 1)

@event.is_press('a')
def is_btn_press1():
time.sleep(4)
Go_Forward()
Turn_Left()
Go_Forward()
Turn_Left()
Go_Forward()
Turn_Right()
Go_Forward()
Turn_Right()
Go_Forward()
Turn_Left()
Go_Forward()
Turn_Left()
Stop()

def Turn_Right():
mbot2.turn(90)
cyberpi.display.set_brush(0, 186, 255)
cyberpi.led.on(0, 186, 255, "all")
cyberpi.display.show_label("Turn Right!", 16, "center", index=
0)

def Turn_Left():
mbot2.turn(-90)
cyberpi.display.set_brush(255, 226, 0)
cyberpi.led.on(255, 227, 0, "all")
cyberpi.display.show_label("Turn Left!", 16, "center", index=
0)

def Go_Forward():
while mbuild.ultrasonic2.get(1) > 7:
mbot2.forward(20)
cyberpi.display.set_brush(0, 255, 8)
cyberpi.led.on(0, 255, 21, "all")
cyberpi.display.show_label("Go Forward!", 16, "center",
index= 0)

mbot2.EM_stop("ALL")
time.sleep(1)

def Stop():
cyberpi.display.set_brush(63, 255, 0)
cyberpi.led.on(42, 255, 0, "all")
cyberpi.display.show_label("Go Forward!", 16, "center", index=
0)
mbot2.straight(60)
cyberpi.display.set_brush(255, 0, 0)
cyberpi.led.on(255, 0, 0, "all")
cyberpi.display.show_label("Stop!", 16, "center", index= 0)
Day 10 - Ultrasonic Sensor
contd.
Task 1 - Knock the Pins:
“The robot spins continuously to search for pins using its
ultrasonic sensor. When it detects a pin within 45 cm, it moves
forward to knock it down, plays a buzzing sound, updates the
number of knocked pins on the display, and returns to its
original position. The display shows the current movement
status, the number of pins knocked, and the ultrasonic sensor
reading in real-time. The process starts when button ‘A’ is
pressed.”
# main.py: Clear

import mbot2,mbuild,event,time,cyberpi

Pins_Pushed=0
Direction=0

def Sense():
global Pins_Pushed
mbot2.drive_speed(20,20)
time.sleep(0.2)
while not mbuild.ultrasonic2.get(1)<45:pass
mbot2.EM_stop("ALL")
time.sleep(2)
cyberpi.audio.play_until('buzzing')
mbot2.forward(120,1.4)
Pins_Pushed+=1
time.sleep(0.2)
mbot2.turn(-360)
time.sleep(0.2)
mbot2.backward(120,1.4)
mbot2.EM_stop("ALL")
time.sleep(1)

@event.start
def on_start():
global Pins_Pushed,Direction
Pins_Pushed=0
Direction="Not Moving!"
cyberpi.audio.set_vol(100)
while True:
cyberpi.audio.set_vol(100)
while True:
cyberpi.display.show_label(Direction,12,"top_mid",index=0)
cyberpi.display.show_label('Pushed '+str(Pins_Pushed)+'
Pins',24,"center",index=1)
cyberpi.display.show_label('U.S.=
'+str(mbuild.ultrasonic2.get(1))+'cm',16,"bottom_mid",index=2)

@event.is_press('a')
def is_btn_press():
global Pins_Pushed,Direction
while True:Sense()
Day 10 - Quad. RGB Sensor
Task 1 - Display:
“The robot checks the color detected by the Quad RGB sensor
on port R1. If the color is black, it lights up LEDs in different
colors and displays the word “black” letter by letter on the
screen. For any other color, it clears the display, sets the LED
and screen brush to the detected color, and shows the color
name at the center.”
# main.py: Clear

import event, time, cyberpi, mbuild


import time

@event.start
def on_start():
while True:
if mbuild.quad_rgb_sensor.get_color_sta("R1",1) == 'black':
cyberpi.led.on(165, 0, 255, 1)
cyberpi.display.set_brush(225, 0, 255)
cyberpi.display.show_label("b", 24, int(30), int(52), index = 1)
time.sleep(0.5)
cyberpi.led.on(40, 2, 255, 2)
cyberpi.display.set_brush(38, 0, 255)
cyberpi.display.show_label("l", 24, int(35), int(52), index = 2)
time.sleep(0.5)
cyberpi.led.on(0, 144, 255, 3)
cyberpi.display.set_brush(0, 255, 255)
cyberpi.display.show_label("a", 24, int(50), int(52), index = 3)
time.sleep(0.5)
cyberpi.led.on(0, 255, 46, 4)
cyberpi.display.set_brush(0, 255, 42)
cyberpi.display.show_label("c", 24, int(65), int(52), index = 4)
time.sleep(0.5)
cyberpi.led.on(255, 208, 0, 5)
cyberpi.display.set_brush(212, 255, 0)
cyberpi.display.show_label("k", 24, int(80), int(52), index = 5)
time.sleep(0.5)

else:
cyberpi.console.clear()
cyberpi.led.on(mbuild.quad_rgb_sensor.get_color_sta("R1",1),
"all")
cyberpi.display.set_brush(mbuild.quad_rgb_sensor.get_color_sta("R1",1))
cyberpi.display.set_brush(mbuild.quad_rgb_sensor.get_color_sta("R1"
,1))
cyberpi.display.show_label(mbuild.quad_rgb_sensor.get_color_sta("R2
",1), 24, "center", index= 0)

Task 2 - Multi-Line Stopper:


“The robot initializes with the message “Not Moving!” and
displays the number of lines it has sensed. When button A is
pressed, it runs the Go_Forward() function four times, which is
meant to handle forward movement and possibly line sensing
(though the movement logic is not fully shown).”
# main.py: Clear

import event, time, cyberpi


# initialize variables
Direction = 0
Lines = 0

@event.start
def on_start():
global Direction, Lines
Direction = "Not Moving!"
while True:
cyberpi.display.show_label(Direction, 16, "top_mid", index= 0)
cyberpi.display.show_label(str('Sensed') + str(str(Lines) +
str('Lines.')), 24, "center", index= 1)

@event.is_press('a')
def is_btn_press():
global Direction, Lines
for count in range(4):
Go_Forward()

def Go_Forward():
global Direction, Lines
cyberpi.audio.set_vol(100)
Task 3 - Kinds of Line Stoppers:
“The robot uses the joystick and button inputs to follow lines
based on sensor readings. When the joystick ‘up’ is pressed, it
performs a smooth line following action until it detects red.
Pressing ‘a’ triggers a right-sided line follower that continues
until it sees yellow, while ‘b’ activates a left-sided line follower
that runs until it detects blue. In each case, the robot adjusts
its wheel speeds based on black and white line detection
using its left and right sensors, and stops upon reaching the
specified color.”
# main.py: Clear

import event, time, cyberpi, mbot2, mbuild


@event.is_press('up')
def is_smooth():
while not (mbuild.quad_rgb_sensor.is_color("red","R2",1)):
if (mbuild.quad_rgb_sensor.is_color("white","R2",1)) and
(mbuild.quad_rgb_sensor.is_color("black","L2",1)):
mbot2.drive_speed(30, -20)
else:
mbot2.drive_speed(20, -30)
mbot2.EM_stop("all")

@event.is_press('b')
def is_left():
while not (mbuild.quad_rgb_sensor.is_color("blue","R2",1)):
if (mbuild.quad_rgb_sensor.is_color("white","R2",1)) and
(mbuild.quad_rgb_sensor.is_color("black","L2",1)):
mbot2.drive_speed(30, 0)
else:
mbot2.drive_speed(0, -30)
mbot2.EM_stop("all")

@event.is_press('a')
def is_right():
while not (mbuild.quad_rgb_sensor.is_color("yellow","R2",1)):
if (mbuild.quad_rgb_sensor.is_color("white","R2",1)) and
(mbuild.quad_rgb_sensor.is_color("black","L2",1)):
mbot2.drive_speed(0, -30)
else:
mbot2.drive_speed(30, -0)
mbot2.EM_stop("all")
Day 11 - MakeX Challenges
Task 1 - Launch the Cube:
“The robot moves from the start point to the launcher and hits
the launcher inorder to launch the cube without pushing over
the launcher.”
# main.py: Clear

import event, time, cyberpi, mbot2, mbuild


@event.is_press('a')
def is_push():
mbot2.straight(73, 150)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.forward(90, 0.4)
time.sleep(1)
mbot2.backward(90, 0.4)
Day 12 - Gyro Sensor
Task 1 - Gyro Straight:
“The robot moves to the starting position whatever angle it is
placed in after calibrating the default position using the
gyroscope.”
# main.py: Clear

import event, time, cyberpi, mbot2

@event.start
def on_start():
while True:
cyberpi.display.show_label(round(-cyberpi.get_yaw(), 1), 24,
"center", index=0)

@event.is_press('b')
def is_btn_press():
cyberpi.reset_rotation('all')
cyberpi.reset_yaw()
cyberpi.led.on(0,0,255) # Indicate reset

@event.is_press('a')
def is_btn_press1():
moving_straight = False # Track straight state

while True:
yaw = -cyberpi.get_yaw()

if -1.5 <= yaw <= 1.5:


if not moving_straight:
mbot2.straight(25,100)
cyberpi.display.show_label("Parking!", 16, "bottom_mid",
index=1)
cyberpi.led.on(0,255,0) # Green LED
cyberpi.audio.play_until('ring') # Play ring when
aligned
moving_straight = True
else:
cyberpi.display.show_label("Turning!", 16, "bottom_mid",
index=1)
cyberpi.led.on(255,0,0) # Red LED
mbot2.EM_stop("ALL")
time.sleep(0.1)

# Turn in the correct direction


if yaw > 0:
(
mbot2.turn(-abs(yaw))
else:
mbot2.turn(abs(yaw))

# Repeated short beep while turning


for _ in range(3):
cyberpi.audio.play('buzzing')
time.sleep(0.05)

moving_straight = False

time.sleep(0.05)

Task 2 - Gyro Straight with Parking:


“The robot parks a cube after being placed in whatever
starting position in the given spot.”
# main.py: Clear
✅ )
*
import event, time, cyberpi, mbot2

@event.start
def on_start():
while True:
cyberpi.display.show_label(round(-cyberpi.get_yaw(), 1), 24,
"center", index=0)

@event.is_press('b')
def is_btn_press():
cyberpi.reset_rotation('all')
cyberpi.reset_yaw()
cyberpi.led.on(0, 0, 255) # Blue for reset

@event.is_press('a')
def is_btn_press1():
# Get current yaw angle
yaw = -cyberpi.get_yaw()

# Show turning status


cyberpi.display.show_label(f"Aligning: {round(yaw, 1)}", 16,
"bottom_mid", index=1)
cyberpi.led.on(255, 0, 0) # Red while turning

# ➤ Turn full angle at once — smooth!


mbot2.turn(yaw) # Positive or negative based on yaw direction

# Wait for turning to finish


time.sleep(1)

# Aligned — status
cyberpi.display.show_label("Aligned!", 16, "bottom_mid", index=1)
cyberpi.display.show_label("Aligned!", 16, "bottom_mid",
index=1)
cyberpi.led.on(0, 255, 0)
cyberpi.audio.play_until('ring')

# ➤ Continue path
mbot2.straight(25, 50)
time.sleep(0.5)

mbot2.turn(90)
time.sleep(0.5)

mbot2.straight(10, 50)
time.sleep(0.5)

mbot2.turn(180)
time.sleep(0.5)

mbot2.straight(10, 50)
time.sleep(0.5)

mbot2.turn(-90)
time.sleep(0.5)

mbot2.straight(25, 50)
time.sleep(0.5)

cyberpi.led.on(255, 255, 0)
cyberpi.display.show_label("Done!", 16, "bottom_mid", index=1)
cyberpi.audio.play_until('ring')
Day 12 - Gyro + Quad RGB Sensor
Task 1 - Color Cards Parking:
“The robot senses the color shown to it and parks it self in the
same color shown to the robot and goes to next colour shown
directly from that place.”
# main.py: Clear

import event, time, cyberpi, mbuild, mbot2


import time

@event.start
def on_start():
cyberpi.display.show_label("Start Program!", 24, "center", index=0)
while True:
cyberpi.display.show_label(-cyberpi.get_yaw(), 16, "top_mid",
index=1)

cyberpi.display.show_label(mbuild.quad_rgb_sensor.get_color_sta("R2", 1),
16, "bottom_mid", index=2)

@event.is_press('a')
def is_btn_press():
cyberpi.reset_yaw()
cyberpi.reset_rotation('all')

@event.is_press('b')
def is_btn_press1():
cyberpi.display.show_label("Sensing Color!", 24, "center", index=0)
while True:
if mbuild.quad_rgb_sensor.is_color("yellow", "R2", 1):
cyberpi.display.show_label("Sensed Yellow!", 24, "center",
index=0)
while not (-cyberpi.get_yaw() > 43 and -cyberpi.get_yaw() <
47):
mbot2.drive_speed(15, 15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(50)
cyberpi.display.show_label("Reached Yellow!", 24, "center",
index=0)
while not not (mbuild.quad_rgb_sensor.is_color("black", "R2",
1)):
pass
if mbuild.quad_rgb_sensor.is_color("yellow", "R2", 1):
cyberpi.display.show_label("Already at Yellow!", 24,
"center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("blue", "R2",
1):
cyberpi.display.show_label("Sensed Blue!", 24,
"center", index=0)
while not (-cyberpi.get_yaw() > 178 and
-cyberpi.get_yaw() < 180):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached Blue!", 24,
"center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("green",
"R2", 1):
cyberpi.display.show_label("Sensed Green!",
24, "center", index=0)
while not (-cyberpi.get_yaw() > -137 and
-cyberpi.get_yaw() < -133):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Green!", 24, "center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("red",
"R2", 1):
cyberpi.display.show_label("Sensed
Red!", 24, "center", index=0)
while not (-cyberpi.get_yaw() > -88 and
-cyberpi.get_yaw() < -92):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Red!", 24, "center", index=0)
time.sleep(1)
else:
if mbuild.quad_rgb_sensor.is_color("blue", "R2", 1):
cyberpi.display.show_label("Sensed Blue!", 24,
"center", index=0)
while not (-cyberpi.get_yaw() > 133 and
-cyberpi.get_yaw() < 137):
mbot2.drive_speed(15, 15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(50)
cyberpi.display.show_label("Reached Blue!", 24,
"center", index=0)
while not not (mbuild.quad_rgb_sensor.is_color("black", "R2",
1)):
pass
if mbuild.quad_rgb_sensor.is_color("yellow", "R2",
1):
cyberpi.display.show_label("Sensed Yellow!",
24, "center", index=0)
while not (-cyberpi.get_yaw() > -2 and
-cyberpi.get_yaw() < 2):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached Yellow!",
24, "center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("blue",
"R2", 1):
cyberpi.display.show_label("Already at
Blue!", 24, "center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("green",
"R2", 1):
cyberpi.display.show_label("Sensed
Green!", 24, "center", index=0)
while not (-cyberpi.get_yaw() > -92 and
-cyberpi.get_yaw() < -88):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Green!", 24, "center", index=0)
else:
if
mbuild.quad_rgb_sensor.is_color("red", "R2", 1):
cyberpi.display.show_label("Sensed
Red!", 24, "center", index=0)
while not (-cyberpi.get_yaw() > -47
and -cyberpi.get_yaw() < -43):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Red!", 24, "center", index=0)
time.sleep(1)
else:
if mbuild.quad_rgb_sensor.is_color("green", "R2",
1):
cyberpi.display.show_label("Sensed Green!", 24,
"center", index=0)
while not (-cyberpi.get_yaw() > -137 and
-cyberpi.get_yaw() < -135):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(50)
cyberpi.display.show_label("Reached Green!",
24, "center", index=0)
while not not
(mbuild.quad_rgb_sensor.is_color("black", "R2", 1)):
pass
if mbuild.quad_rgb_sensor.is_color("yellow",
"R2", 1):
while not (-cyberpi.get_yaw() > 43 and
-cyberpi.get_yaw() < 47):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Yellow!", 24, "center", index=0)
else:
if mbuild.quad_rgb_sensor.is_color("blue",
"R2", 1):
cyberpi.display.show_label("Sensed
Blue!", 24, "center", index=0)
while not (-cyberpi.get_yaw() > 88 and
-cyberpi.get_yaw() < 92):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Blue!", 24, "center", index=0)
else:
if
mbuild.quad_rgb_sensor.is_color("green", "R2", 1):
cyberpi.display.show_label("Already
at Green!", 24, "center", index=0)
else:
if
mbuild.quad_rgb_sensor.is_color("red", "R2", 1):

cyberpi.display.show_label("Sensed Red!", 24, "center", index=0)


while not (-cyberpi.get_yaw() >
-2 and -cyberpi.get_yaw() < 2):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)

cyberpi.display.show_label("Reached Red!", 24, "center", index=0)


time.sleep(1)
else:
if mbuild.quad_rgb_sensor.is_color("red", "R2",
1):
cyberpi.display.show_label("Sensed Red!",
24, "center", index=0)
while not (-cyberpi.get_yaw() > -47 and
-cyberpi.get_yaw() < -43):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(50)
cyberpi.display.show_label("Reached Red!",
24, "center", index=0)
while not not
(mbuild.quad_rgb_sensor.is_color("black", "R2", 1)):
pass
if
mbuild.quad_rgb_sensor.is_color("yellow", "R2", 1):
while not (-cyberpi.get_yaw() > 88 and
-cyberpi.get_yaw() < 92):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Yellow!", 24, "center", index=0)
else:
if
mbuild.quad_rgb_sensor.is_color("blue", "R2", 1):
cyberpi.display.show_label("Sensed
Blue!", 24, "center", index=0)
while not (-cyberpi.get_yaw() > 133
and -cyberpi.get_yaw() < 137):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)
cyberpi.display.show_label("Reached
Blue!", 24, "center", index=0)
else:
if
mbuild.quad_rgb_sensor.is_color("green", "R2", 1):

cyberpi.display.show_label("Sensed Green!", 24, "center", index=0)


while not (-cyberpi.get_yaw() >
178 and -cyberpi.get_yaw() < 180):
mbot2.drive_speed(-15, -15)
mbot2.EM_stop("ALL")
time.sleep(1)
mbot2.straight(70)

cyberpi.display.show_label("Reached Green!", 24, "center", index=0)


else:
if
mbuild.quad_rgb_sensor.is_color("red", "R2", 1):

cyberpi.display.show_label("Already at Red!", 24, "center",


index=0)

time.sleep(1)

You might also like