---
title: "Random Bug Bot 2"
date: "2022-03-13"
categories: ["Activities", "All"]
tags: ["Colour Sensors", "Branching", "Iteration", "Variables", "Motors", "Maths", "Advanced"]
excerpt: "Program your rover to move like a bug!"
featuredImage: "/images/resources/random-bug-bot-2.png"
---

** **

Build from the Basic Bug Bot exercise but program the rover to have a more complex lifecycle. Create a mesh of food markers on the ground for the rover to detect with colour sensors. The new bug bot rover should now have health that goes down each second as the rover gets hungry. When the rover detects food on the floor, increase the rover's health. If the rover reaches zero health it will stop (and die).

![](/images/content/c8d42e-random-bug-bot-2.png)

Activity Demonstration

### Setup

You can use coloured tape on the playing surface to act as food. Randomly place food in a 30cm by 30cm playing area where each piece of food is a roughly 1-2cm square. Rovers will have difficulty detecting food chunks that are any smaller. Students will have trouble getting their rovers to detect food if they program the rover to move at max speed. A slower rover will a better chance to detect food.

### Here’s Our Approach

#### Stage 1

Firstly we use a variable block for our health, we will set it to 20. We want to enter our normal lifecycle activity so we add a repeat while true loop.

#### **Stage 2**

There will be three stages to our lifecycle. Firstly, our bug bot will look for food using the colour sensor block. If it detects the colour of our food with the sensor we add 3 to the health variable and play the confirmation sound.

#### **Stage 3**

The second stage of our lifecycle is moving. We will use a left and right variable block here to make random numbers, then use those in our set motor speeds block to create random movement. We will also make the duration of movement set to 1 second all the time, this is important for our death countdown.

#### **Stage 4**

The final stage of our lifecycle is reducing life and checking for death. First we reduce our life by 1. We then use an if statement to check if we have reached 0 health. If we have, we are dead, break the loop to stop our lifecycle. If we have more than 0 health, we will display the health count on the rover screen.

![](/images/content/ab5bbe-random-bug-bot-2-answer.png)

Example Code
