# Module 8 - Section 2 - Shape up

 Site: ΕΛ/ΛΑΚ Moodle Course: Python Lab Book: Module 8 - Section 2 - Shape up
 Printed by: Guest user Date: Wednesday, 22 May 2024, 9:00 PM

## Description

After the completion of this module the students will be able to:

• create custom made functions to navigate edison robot
• describe what is a Flow Chart in programming
• explain the use of a Flow Chart in programming
• predict the outcome of a Flow Chart in every case

## 8.2.1 - Shapes

Can you draw shapes using your edison and a marker?

For a first challenge try to draw a square.

You must remember to use a for loop and range command.

If you want a little hint look at the following code:

for i in range(4): Ed.PlayBeep() Ed.TimeWait(1, Ed.TIME_SECONDS)

So what code is needed do move drawing a square?

Do 4 times the following:

• Move forward in some distance
• Spin 90 degrees (to the left (or right))

Try to draw a triangle or a hexagon.

What if you want to draw a circle?

## 8.2.2 - Functions

Once you have been programming for some time you will want to make your own commands or functions. Watch the code.org video for a good explanation of why functions are useful.

In Python, functions look like the following:

def function_name(parameter_1, parameter_2):

{this is the code in the function}

{more code}

{this code isn’t in the function}

{because it isn’t indented}

You call a function from your code with or without parameters and the function usually returns a value to the calling code. You do not always have to return a value.

Write the following program.

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- directionToMove=Ed.SPIN_LEFT speedToMoveAt=Ed.SPEED_7 distanceToMove=10 #calling code moveOnClap(directionToMove,speedToMoveAt,distanceToMove) #user defined function def moveOnClap(direction,speed,distance): Ed.ReadClapSensor() while Ed.ReadClapSensor() == Ed.CLAP_NOT_DETECTED: pass Ed.Drive(direction,speed,distance)

Fill in the missing words in the function below to make this function drive the edison robot in a square shape.

def driveInaSquare(direction,speed,distance):

for i in range(?):

Ed.Drive(direction,?,?)

Ed.Drive(?,speed,?)

Write the syntax for code to call this function.

Now incorporate this code into a program which calls your function more than once to drive multiple squares and download and run it.

Does the program do as you expect?  Describe any errors you had in getting your program to work.

## 8.2.3 - Flash Clap

Professional programmers usually plan out their code before diving in to coding. One way programmers organize their code is by using flowchart diagrams which chart the ‘flow’ of the program from top to bottom.

In a flowchart, the program is represented as a series of shapes representing processes, inputs/outputs and decision. These shapes are connected by arrows that show how the program flows. The words inside the shapes can often be one-word summaries of the process or decision. The idea of a flowchart is to summarize graphically what is happening in the underlying code without revealing all the detail.

There is always a start and usually an end of the program which are both inside an Oval shape called a terminator. A rectangle represents a process or action and a diamond represents a decision point. Inputs or outputs are represented by a parallelogram shape. These are the basic shapes in a flowchart although you can create much more complicated flowcharts with additional shapes.

An example flowchart summarizing the program for the Edison robot waiting for a clap before flashing its left LED is shown below.

The above flowchart corresponds to the following code.

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- while True: if Ed.ReadClapSensor() == Ed.CLAP_DETECTED: Ed.LeftLed(Ed.On) Ed.TimeWait(50, Ed.TIME_MILLISECONDS) Ed.LeftLed(Ed.OFF) Ed.TimeWait(50, Ed.TIME_MILLISECONDS)

You can find the code here

What is the purpose of the TimeWait() function calls in the code? What would happen if we didn’t have them?

Can you make the robot light up both the LEDs when you clap?