Skip to content

Commit

Permalink
Merge pull request #102 from morganstanley/robotics
Browse files Browse the repository at this point in the history
Robotics
  • Loading branch information
mimiflynn authored Feb 7, 2024
2 parents c072fea + 62a3146 commit 825e8ef
Show file tree
Hide file tree
Showing 138 changed files with 1,478 additions and 331 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,5 @@ venv.bak/
# OS files
.DS_Store

.vscode

docs/
site/public
17 changes: 17 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"dbaeumer.vscode-eslint",
"eamodio.gitlens",
"ecmel.vscode-html-css",
"github.vscode-github-actions",
"joedevivo.vscode-circuitpython",
"ms-vscode.vscode-serial-monitor",
"unifiedjs.vscode-mdx"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
22 changes: 22 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true,
"editor.renderWhitespace": "all",
"editor.renderControlCharacters": true,
"html.format.indentInnerHtml": true,
"html.format.wrapAttributes": "force-aligned",
"css.lint.duplicateProperties": "warning",
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.format.enable": true,
"eslint.validate": ["javascript"],
"search.exclude": {
"**/node_modules": true
},
"javascript.preferences.quoteStyle": "single",
"diffEditor.ignoreTrimWhitespace": false
}
52 changes: 52 additions & 0 deletions site/content/exercises/circuitpython/robotics/E1/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
template: exercise
title: Blinky
level: 4
exercise: 1
category: Robotics
tags: ['CircuitPython']
---

Remember Blinky?

The very first project from the Makecode Robotics course caused a single LED to cycle back and forth between on and off.

Recall it used a single while loop containing 4 blocks:

1. set the LED to on
2. sleep for half a second
3. set the LED to off
4. sleep for half a second

Now we do the same thing, just using 4 lines of Python code instead of 4 Makecode blocks:

```python
# This section lets the system know which libraries we want to use
from adafruit_circuitplayground import cp
import time

# This section is where our main code goes
# Everything indented under the 'while True' will repeat forever
# We call this a 'while loop'
while True:
cp.red_led = True
time.sleep(0.5)
cp.red_led = False
time.sleep(0.5)

# Note - anything with a '#' infront is a comment and is ignored
```

![CPX Blink](../../../circuitpython/level-1/L1-E1/blink.gif)

### Challenge Problem

Can you do the same thing using only 2 lines of Python code within the while loop?

### References

- [cp.red_led](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.red_led)

- [Python: while loops](https://www.w3schools.com/python/python_while_loops.asp)

- [Python: time.sleep](https://docs.python.org/3/library/time.html#time.sleep)
10 changes: 10 additions & 0 deletions site/content/exercises/circuitpython/robotics/E10/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
template: exercise
title: Bluetooth
level: 4
exercise: 10
category: Robotics
tags: ['CircuitPython']
---

Coming soon!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions site/content/exercises/circuitpython/robotics/E2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
template: exercise
title: Color Wheel
level: 4
exercise: 2
category: Robotics
tags: ['CircuitPython']
---

Technically the Blinky project we created with CircuitPython doesn't do exactly the same thing as the one we previously created with Makecode:

![MakeCode Blink](blink.png)

Recall our Python code made use of the `cp.red_led` property, which toggles the small red D13 LED. To light up any of the 10 larger pixels on the device we make use of the `cp.pixels` property:

```python
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0))

while True:

# notice how these lines are similar
cp.pixels[0] = (255, 0, 0)
cp.pixels[1] = (255, 127, 0)
cp.pixels[2] = (0, 255, 0)
cp.pixels[3] = (0, 255, 127)
cp.pixels[4] = (75, 0, 130)
cp.pixels[5] = (60, 0, 255)
cp.pixels[6] = (255, 255, 255)
cp.pixels[7] = (0, 100, 0)
cp.pixels[8] = (100, 0, 0)
cp.pixels[9] = (0, 0, 100)

# the [] with a number determines which light to turn on
# and the numbers in () deterine the color!
# It says how much (red, green, blue) we want.
# Try changing the numbers!
```

### Challenge Problem

Change your Blinky code from Exercise 1 to use LED 0 instead of D13. Try having it blink yellow instead of red.

### References

- [cp.pixels](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.pixels)

- [RGB Color Values](https://www.w3schools.com/colors/colors_rgb.asp)
28 changes: 28 additions & 0 deletions site/content/exercises/circuitpython/robotics/E3/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
template: exercise
title: Sounds
level: 4
exercise: 3
category: Robotics
tags: ['CircuitPython']
---

Let's have a listen to the device's speakers!

Here the first number is the pitch, and the second number is the duration of the note. Play different notes depending on which button is pressed.

```python
from adafruit_circuitplayground import cp

while True:
if cp.button_a:
cp.play_tone(329,1)
cp.play_tone(261,1)
elif cp.button_b:
cp.play_tone(440,1)
cp.play_tone(400,1)
```

### References

- [cp.play_tone](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.play_tone)
70 changes: 70 additions & 0 deletions site/content/exercises/circuitpython/robotics/E4/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
template: exercise
title: Touch and Serial
level: 4
exercise: 4
category: Robotics
tags: ['CircuitPython']
---

We've played with lights, sounds, and buttons - now let's play with the touch sensors.

Touching different pins on the board will trigger different lights and sounds.

Also try playing with the slide switch to enable and disable it entirely.

The touchpad is assigned by its number on the circuit playground.

In order to view the output of the `print()` statements, ensure **Serial** is enabled in the Mu Editor.

```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()
```

### Challenge Problem

Run the above program and get it to print "Touched 8" to the serial console.

### References

- [cp.touch_A1](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.touch_A1)

- [Python: print](https://www.w3schools.com/python/ref_func_print.asp)
38 changes: 38 additions & 0 deletions site/content/exercises/circuitpython/robotics/E5/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
template: exercise
title: Buttons
level: 4
exercise: 5
category: Robotics
tags: ['CircuitPython']
---

Now let's use the buttons on the device to turn individual LEDs on and off. We use the `cp.button_a` and `cp.button_b` properties to determine if the given button is being pressed down:

```python
from adafruit_circuitplayground import cp

while True:
if cp.button_a:
cp.pixels[0] = (255, 255, 255)
elif cp.button_b:
cp.pixels[1] = (255, 255, 255)
```

### Challenge Problem 1

Modify the code above so that only one pixel is on at a time. In other words, if multiple buttons are pressed in a sequence, only the most recent pixel stays on.

_Hint: consider using `cp.pixels.fill`_

### Challenge Problem 2

Now modify the code so that each pixel is on only as long as the user holds down the button.

### References

- [Python: if/elif/else](https://www.w3schools.com/python/python_conditions.asp)

- [cp.button_a](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.button_a)

- [cp.button_b](https://docs.circuitpython.org/projects/circuitplayground/en/latest/api.html#adafruit_circuitplayground.circuit_playground_base.CircuitPlaygroundBase.button_b)
Loading

0 comments on commit 825e8ef

Please sign in to comment.