Inputs & Outputs
Invent Logo

Computers can use both input and output devices. An input device sends information to a computer. An example would be a keyboard or a mouse. An output device receives information from a computer. An example is a computer monitor or speakers.

We've already used one output device. The drive module is a good example. The two motors are controlled by a signal from the robot so they are output devices. Let's now use some more outputs. We'll use the in-built LEDs.

Inputs & Outputs
The LEDs

We've already looked at some of the other LEDs that show the status of the robot, but there are two additional LEDs that you can control at the front of the robot; one on the left and one on the right.

The LEDs
Testing them out

Start a new program and drag the 'LED' block into your code. This block allows you to select either the left or right LED and specify the colour you want. You can select any colour for either LED.

Try this out now. Set one of your LEDs to green and the other to red.

Testing them out
Morse code

Morse code was invented by Samuel Morse and Alfred Vail in the early 1830s. He wanted a way to send messages using electricity over telegraph wires. They assigned each letter of the alphabet and some symbols a unique combination of dots (short taps) and dashes (long taps). This allowed messages to be transmitted using simple codes, revolutionizing communication over long distances before phones or the internet.

Morse code
SOS

Morse code is like a secret language using sounds and pauses instead of words. Imagine tapping a pencil to talk to a friend far away. A short tap means a dot, a long tap means a dash, and pauses separate letters and words. So, "SOS" would be three short taps, three long taps, then three short taps again. It's a clever way to send messages without needing to talk or write!

"SOS" is a distress signal used to ask for help when someone is in trouble, like when they're lost at sea or in danger. It's like saying "please help me" in a simple and urgent way that people all around the world understand. It stands for 'Save Our Souls!'.

Sending SOS

Can you write a program to send SOS on one of your LEDs? You can send it using any colour you like. To use this, you'll need to use the 'delay' block. You'll find it in the 'Timing' group.

You'll need to use a combination of LED blocks and delay blocks to send your SOS (three short blinks, three long blinks, three short blinks again). You'll want delays less than 1 second, so you can change the '1' to a fraction of a second, or you can change where it says 'seconds' to 'milliseconds'. 1 millisecond is one thousandths of a second.

Experiment with the delays until you get a recognisable pattern, and then put your code in a big loop so that it flashes 'SOS' twice. You'll want a bigger delay between each 'SOS' than you have between each letter.

Sending SOS
Making sounds

The speaker is another example of an output as it can be controlled by your robot.

We can use the speaker to play notes.

The play block

You can find the 'Play music note' block under 'Sounds' (which is under 'Invent!').

Drag in one of these blocks and experiment with different notes and durations. The 1 in the note 'B1' refers to the octave, so low numbers are low pitches and high numbers are higher pitches. You'll probably want to use a higher pitch that is easier to hear. You'll also want to set the duration to a fraction of a second.

The play block
SOS buzzer

Can you change your 'SOS' program so it plays beeps for the 'dots' and 'dashes' on the speaker? Once you've done this, we want you to repeat your previous 'Save the Astronaut' challenge by playing an 'SOS' message after the astronaut is rescued.

So ... you robot should go forward, rescue the astronaut, turn around and return back to where it started, and then play the 'SOS' message on the speaker!

Playing a tune

Can you play a tune using a combination of notes? There are blocks to play pre-defined tunes but that's too easy! We want you to play a tune by specifying individual notes and durations. You can pick the tune. Feel free to keep it simple. How about 'Twinkle twinkle little star' or 'Happy birthday'?

The button

Let's now look at some input options. There's an inbuilt button on the main board as shown here. Let's find out how we can detect whether it's pressed or not.

The button
The button block

Start a new program and drag in the 'Button pressed' block.

The button block
The if block

Now drag in an 'if' block from the 'Logic' group. Make sure that the 'Button pressed' block is attached to the 'if' block like it is shown here.

The if block
The move block

Finally drop a 'move' block in within the 'if' block. What this should do is when you press the button, the robot should move forward 5cm.

Try it out ... does it work? You might find that it doesn't! Can you guess what the problem is? If not, we'll tell you in the next step!

The move block
The problem

You should find that the button doesn't do anything at all! Why is this?

What happens is that when you press the play button, your code quickly checks if the button is pressed, and as it isn't (yet!), it doesn't move. Then your program is finished so it has nothing more to do.

Your robot runs its program so quickly that the whole program is over within milliseconds, and it will never check if the button is pressed again. What we need to do is continually check for button presses ...

The problem
Repeat while

Drag a 'repeat while' block from the 'Loops' group into your program. Make sure that your existing code is within the 'repeat' block as shown.

We've used a repeat block before that repeats a certain number of times. Now we're using a repeat block that only repeats if a certain condition is true. But we haven't yet specified the condition.

So how do we repeat all the time forever? It's easy, all we need is to drop a 'True' block into the space for the condition ...

Forever loops

The 'true' block is found in the 'Logic' group. Drag it inside the 'repeat' block as shown.

As this repeat block repeats while the condition is true, and we've dragged in a 'true' block, then the condition will always be true, so the loop will repeat forever.

This is an important trick we've learnt. We often need to run code continually and this is how we do it! Try your program now. It should move forward 5cm every time you press the button.

N.B. When you're running your code, the 'play' button turns to a 'stop' button. Now we've got an endless loop, our code will never finish, so you'll need to use the 'stop' button to stop it every time you want to make changes and run it again!

Forever loops
Obstacle sensor

Get the obstacle sensor and attach it by aligning the four pins on each plug with the holes in the sockets underneath.

Be careful here - make sure all 8 pins go into the socket holes.

Obstacle sensor
... attached

When the sensor is fully attached it should look like this. It has two eyes that can detect objects that are with 1 or 2 cm.

... attached
The endless loop

We need to write a program that continually checks the obstacle sensors and responds appropriately. We'll need to start with an 'infinite loop'. Create this part of the program now.

The endless loop
The print statement

In the 'Text' group, find the 'print' block and drop it into the infinite loop.

This block is really useful for testing purposes as it can display some text on your computer. If we ran it now, it would endlessly print the text 'abc'. Instead we want to print what one of the robot eyes sees ...

The print statement
Using the sensor

Find the 'obstacle sensor' block and drag it into the print statement.

Using the sensor
Interpreting the numbers

Now run the program, but this time click on the 'Console' tab. This is where we see what the robot is 'printing'.

Move your finger near the left eye and look at the numbers that are being displayed. You should notice that with your finger away from the sensor, the number is close to zero, and with it very close it goes to about 4000.

Interpreting the numbers
How it works

The sensors we are using work with invisible 'infrared' light. The sensor sends out some light and detects how much light is reflected back from an object.

That's why it only shows a high number when an object is held close to it.

We can use this to know if the robot is close to an object. As with no object the amount of light coming back was close to zero, and with an object really close, the amount of light was around 4000, let's choose half of this amount (2000) as a threshold. If there's more than 2000 returned by the sensor, we'll know an object is there!

Adding a comparison

Now drag an 'if' block into the top of the infinite loop.

Also drag one of the 'comparison' blocks shown here into the condition of the 'if' block.

Adding a comparison
Comparing with the sensor

Now move the 'Obstacle' block from your print statement into the 'if' block as shown, and change the comparison to a 'greater than'.

Comparing with the sensor
Comparing with '2000'

From the 'Math' group, drag a number block into the right side of the comparison and change the value to '2000' as shown.

Comparing with '2000'
Printing 'Obstacle'

Now drag the print block up into inside the 'if' block and change the text so it shows

Printing 'Obstacle'
Adding an 'else'

Now click on the gear icon on the 'if' block and drag an 'else' under the 'if' as shown.

Once you've done this, you should see your 'if' block change underneath this popup window and an 'else' should appear.

Adding an 'else'
Printing 'No Obstacle'

Finally add a print statement if no obstacle is detected inside the 'else' part of the block.

Hint: If you right click on the first 'print' block you can use the 'Duplicate' option to get another copy. Then you just need to change the text.

Now run your program and look at the console. You should see your robot deciding if there is an obstacle or not.

Printing 'No Obstacle'
The autonomous robot

You're now ready to create your autonomous robot. What you really just need to do is remove the two 'Print' blocks and replace them with the code you want to run if the robot detects an obstacle or not.

Hint: You probably want to go forward when there's no obstacle. When there is an obstacle, we suggest you reverse a little while, and turn around a bit. We also suggest that when you go forward, you should select a small distance - perhaps just 1cm at a time. Remember that your robot will only check if it has hit an obstacle after doing the move!

Experiment with your code. You should now be able to let your robot loose to roam around. When it hits something it should turn around and move away from it.

An improved robot

Rather than just go forward 1cm at a time and check, there's an even better way ...

Simply change the 'wait' on the first move block to 'no wait'. What this does, is that if it detects no obstacle, it will START moving 5cm forward, but it won't wait to proceed with the next part of the program until the 5cm movement has finished. This means it will be continually checking if an obstacle has been hit, even when it's moving. Your robot should be more responsive if your code is written like this.

Of course the code here will be different, particularly the 'Print' block - you'll have replaced it with the code you want to run if there is no obstacle! You've probably also got the robot moving 1cm rather than 5cm, but actually with the 'no wait' option it doesn't make any practical difference as it's really just telling the robot to start moving forward a maximum of 5cm and it will check again if it needs to move further very soon afterwards when it goes through the loop another time.

An improved robot
Even more improved

Can you improve your code further so it uses both the left and the right obstacle sensor?

You could use an 'and' block from the 'Logic' group to only move forward if both the left and right sensor DON'T see an object. But an even more clever robot would turn away from the obstruction.

Can you get your robot to turn clockwise if its left sensor is activated, and anticlockwise if its right sensor is activated?

Obstacle course

Your last challenge this lesson is to complete an obstacle course. Your teacher will set one up for you with several obstacles. Written on each obstacle will be an instruction. You might have to detect the obstacle and go around it, play a tune, flash some lights, or do a particular type of movement.

Have fun!