Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python Functions exercise #131

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 123 additions & 2 deletions site/content/exercises/circuitpython/robotics/E10/index.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,131 @@
---
template: exercise
title: Crickit and Motors
title: Functions
level: 4
exercise: 10
category: Robotics
tags: ['CircuitPython']
---

Coming soon!
Up to this point we've made use of many functions in our programs: some of them are Python built-in functions:
* `abs` - return absolute value of a number
* `int` - return closest integer (rounded down) of a number
* `isinstance` - return True if variable is of the given data type (otherwise False)
* `len` - return length (number of items) of an object (such as a list)
* `print` - output string to the Serial console
* `range` - return a sequence of integers

Some are provided by Python's standard library:
* `time.sleep` - suspend execution for the given number of seconds

Others are provided by Adafruit modules:
* `cp.pixels.fill` - color all pixels the given color
* `cp.play_tone` - produce a tone using the speaker
* `cp.stop_tone` - stop producing the tone
* `ble.start_advertising` - start advertising Bluetooth availability
* `ble.stop_advertising` - stop advertising Bluetooth availability
* `Packet.from_stream` - read the next packet from the incoming stream

### Defining Your Own Functions

Recall how we used loops and lists to simplify our code when we needed to perform a task multiple times. Functions are another technique to simplify our code and prevent us from having to rewrite the same code in multiple places.

Let's go back to the Touch/Serial program from Exercise 4:
```python
from adafruit_circuitplayground import cp

while True:
if cp.switch:
print("Slide switch off!")
cp.pixels.fill((0, 0, 0))
cp.stop_tone()
continue
if cp.touch_A4:
print('Touched A4!')
cp.pixels.fill((15, 0, 0))
cp.start_tone(262)
elif cp.touch_A5:
print('Touched A5!')
cp.pixels.fill((15, 5, 0))
cp.start_tone(294)
elif cp.touch_A6:
print('Touched A6!')
cp.pixels.fill((15, 15, 0))
cp.start_tone(330)
elif cp.touch_A1:
print('Touched A1!')
cp.pixels.fill((0, 15, 15))
cp.start_tone(392)
elif cp.touch_A2 and not cp.touch_A3:
print('Touched A2!')
cp.pixels.fill((0, 0, 15))
cp.start_tone(440)
elif cp.touch_A3 and not cp.touch_A2:
print('Touched A3!')
cp.pixels.fill((5, 0, 15))
cp.start_tone(494)
elif cp.touch_A2 and cp.touch_A3:
print('Touched "8"!')
cp.pixels.fill((15, 0, 15))
cp.start_tone(523)
else:
cp.pixels.fill((0, 0, 0))
cp.stop_tone()
```

Note that each time a touchpad is touched, the same 3 actions are taken: print to serial console, color pixels, and play a tone. This is a perfect opportunity to simplify our code by defining a function:
```python
from adafruit_circuitplayground import cp

def my_function(message, color, tone):
print(message)
cp.pixels.fill(color)
cp.start_tone(tone)

while True:
if cp.switch:
print("Slide switch off!")
cp.pixels.fill((0, 0, 0))
cp.stop_tone()
continue
if cp.touch_A4:
my_function('Touched A4!', (15, 0, 0), 262)
elif cp.touch_A5:
my_function('Touched A5!', (15, 5, 0), 294)
elif cp.touch_A6:
my_function('Touched A6!', (15, 15, 0), 330)
elif cp.touch_A1:
my_function('Touched A1!', (0, 15, 15), 392)
elif cp.touch_A2 and not cp.touch_A3:
my_function('Touched A2!', (0, 0, 15), 440)
elif cp.touch_A3 and not cp.touch_A2:
my_function('Touched A3!', (5, 0, 15), 494)
elif cp.touch_A2 and cp.touch_A3:
my_function('Touched "8"!', (15, 0, 15), 523)
else:
cp.pixels.fill((0, 0, 0))
cp.stop_tone()
```

**Defining a function**: `def` keyword followed by the name of the function, parentheses, and a colon

- The body of the function, or the code for what the function will actually do, comes after the colon on an **indented** line

**Calling a function**: writing the function name followed by parentheses, for example: `ble.start_advertising()`

**Function parameters**:

- Sometimes functions require input to provide data for their code. This input is defined using parameters.
- Parameters are variables that are defined in the function definition.
- They are assigned the values which were passed as arguments when the function was called, elsewhere in the code.
- For example `my_function` has 3 parameters: `message`, `color`, and `tone`.

**Function arguments**:

- Parameters in python are variables — placeholders for the actual values the function needs.
- When the function is called, these values are passed in as arguments.
- For example: `my_function('Touched A4!', (15, 0, 0), 262)`

### Challenge Problem

Combine the code in Exercise 8 (Bluetooth) and Exercise 9 (Accelerometer) so that the Bluefruit device can accept both packets of type ColorPacket (from the Color Picker screen) and packets of type AccelerometerPacket (from the Accelerometer controller screen), and color the pixels accordingly. Use functions to simplify your code.
2 changes: 1 addition & 1 deletion site/content/exercises/circuitpython/robotics/E11/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
template: exercise
title: Robot Assembly
title: Crickit and Motors
level: 4
exercise: 11
category: Robotics
Expand Down
10 changes: 10 additions & 0 deletions site/content/exercises/circuitpython/robotics/E12/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
template: exercise
title: Robot Assembly
level: 4
exercise: 12
category: Robotics
tags: ['CircuitPython']
---

Coming soon!
6 changes: 3 additions & 3 deletions site/content/exercises/circuitpython/robotics/E9/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ while True:
if isinstance(packet, AccelerometerPacket):
x, y, z = packet.x, packet.y, packet.z
print((x, y, z))
R = 10 * abs(int(x))
G = 10 * abs(int(y))
B = 10 * abs(int(z))
R = abs(int(10 * x))
G = abs(int(10 * y))
B = abs(int(10 * z))
cp.pixels.fill((R, G, B))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this change: do the kids know about order of operations yet?

```

Expand Down