top of page

Block Programming - Complex Examples with Loops

Example 1: IR Remote Control to Move a Robot with Speed Control

Let's consider a more complex example of an IR remote-controlled robot with a speed control. In this case, we will use two buttons on the remote, "UP" and "DOWN", to increase and decrease the speed of the robot respectively.


variable speed = 0


forever {

if (IR_Received == "UP" && speed < 10) {

speed = speed + 1

} else if (IR_Received == "DOWN" && speed > 0) {

speed = speed - 1

}

set robot speed to speed

}


This block code initializes a variable 'speed' to zero. Then, in a forever loop, it listens for the "UP" or "DOWN" signal from the IR remote. If the "UP" signal is received and the speed is less than 10, it increases the speed by 1. If the "DOWN" signal is received and the speed is more than 0, it decreases the speed by 1. Then, it sets the robot's speed to the updated value.


Example 2: IR Remote-Controlled Obstacle-Avoiding Robot

Here's an example of a robot that listens for signals from an IR remote to move around but also uses an onboard distance sensor to avoid obstacles.


variable thresholdDistance = 10


forever {

if (IR_Received == "FORWARD") {

if (get distance from sensor < thresholdDistance) {

stop robot

play alarm sound

} else {

move robot forward

}

} else if (IR_Received == "BACKWARD") {

move robot backward

} else if (IR_Received == "LEFT") {

turn robot left

} else if (IR_Received == "RIGHT") {

turn robot right

} else if (IR_Received == "STOP") {

stop robot

}

}


In this case, when the "FORWARD" signal is received, the robot checks its front distance sensor before moving forward. If an obstacle is detected within a certain threshold distance, it stops and plays an alarm sound. For other signals, it performs the corresponding actions.


Example 3: Using Loops and IR Remote for a Game of Robot Tag

Let's take a look at a more complex example - using block programming to create a game of tag between two robots, controlled by two different IR remotes.

variable isTagged = false


forever {

if (isTagged == false && IR_Received == "TAG") {

change robot color to red

isTagged = true

} else if (isTagged == true && IR_Received == "TAG") {

change robot color to green

isTagged = false

}

}


In this code, the isTagged variable tracks whether the robot is "it". If the robot isn't "it" and receives the "TAG" command from its IR remote, it changes color to red to show that it's "it" and sets the isTagged variable to true. If it is "it" and receives the "TAG" command, it changes back to green and sets the isTagged variable to false.


Example 4: Using a loop and IR remote to control a robot arm


Here is another example, this time using a loop and an IR remote to control a robotic arm.


forever {

if (IR_Received == "UP") {

move arm up

} else if (IR_Received == "DOWN") {

move arm down

} else if (IR_Received == "GRAB") {

close gripper

} else if (IR_Received == "RELEASE") {

open gripper

}

}


In this case, the loop continuously checks for signals from the IR remote and moves the robotic arm or controls the gripper based on the received commands.


These examples demonstrate how loops, in conjunction with IR remote signals, can bring interactivity and control to a variety of robot designs and can be used to control a robot in sophisticated ways.


Recent Posts

See All
Block Programming - Operators

Operators are another key concept in programming that you'll often use in block programming as well. They let you perform operations on...

 
 
Block Programming - Functions

Functions are a fundamental concept in programming, including block programming, because they allow you to group a set of actions...

 
 

Commentaires


Les commentaires sur ce post ne sont plus acceptés. Contactez le propriétaire pour plus d'informations.

Tel: 1-888-876-4265

© 2019 Rohbot Kit by illuminEd Inc

Sign up to receive Rohbot news and updates.

Thanks for submitting!

© Rohan  Jay  - High School Senior at  PRISMSUS 

american_citizenship_award_pin.jpg
image.png
image.png
bottom of page