Difference between revisions of "Robot Club"
From Mech
Jump to navigationJump to searchNickMarchuk (talk | contribs) |
NickMarchuk (talk | contribs) |
||
Line 1: | Line 1: | ||
Files for the programming tutorial: [[Media:nurobotics_tutorial.zip | nurobotics_tutorial.zip]] |
|||
== Halloween Tutorial 1== |
|||
We are using the [https://www.adafruit.com/product/3727 Itsy Bitsy M0 Express] from [https://www.adafruit.com/ Adafruit]. |
|||
[https://codewith.mu/ Mu ("Moo")] is a nice IDE used to edit the code and debug. |
|||
* First, plug the board in a open Mu. Load main.py from the CIRCUITPY drive. Open the serial monitor. |
|||
Download and install Arduino from [http://arduino.cc/en/Main/Software http://arduino.cc/en/Main/Software]. |
|||
* Second, go into REPL mode and communicate with the board. Practice entering (CTRL-c) and exiting (CTRL-D) REPL. |
|||
Run the program to see how it looks. Open an example from |
|||
* Third, make a local copy of main.py, in case you ever want to see the original examples again. |
|||
* Forth, replace the contents of main.py with: |
|||
''' |
|||
import board # pin definitions |
|||
import time # time functions |
|||
import touchio # capacitivectouch library |
|||
import pulseio # pwm library |
|||
import adafruit_dotstar # dotstar rgb led library |
|||
# One dotstar soldered on the board |
|||
File->Examples->01.Basics->Blink. |
|||
dot = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
|||
# Built in red LED |
|||
All Arduino code has two main functions: setup() and loop(). In Blink, |
|||
led = pulseio.PWMOut(board.D13, frequency=5000, duty_cycle=0) |
|||
setup() makes pin 13 an output, and loop() makes an LED on pin 13 |
|||
led.duty_cycle = 0 # initially off |
|||
blink once per second. We'll go over it in more detail. Close the |
|||
program for now. |
|||
# Capacitive touch on A2 |
|||
touch = touchio.TouchIn(board.A2) |
|||
# Helper to give us a nice color swirl |
|||
We are programming a slightly different chip, so download the extra |
|||
def wheel(pos): |
|||
parts file from |
|||
# Input a value 0 to 255 to get a color value. |
|||
# The colours are a transition r - g - b - back to r. |
|||
if (pos < 0): |
|||
return [0, 0, 0] |
|||
if (pos > 255): |
|||
return [0, 0, 0] |
|||
if (pos < 85): |
|||
return [int(pos * 3), int(255 - (pos*3)), 0] |
|||
elif (pos < 170): |
|||
pos -= 85 |
|||
return [int(255 - pos*3), 0, int(pos*3)] |
|||
else: |
|||
pos -= 170 |
|||
return [0, int(pos*3), int(255 - pos*3)] |
|||
######################### MAIN LOOP ############################## |
|||
[https://github.com/damellis/attiny/ https://github.com/damellis/attiny/] (Download ZIP button). |
|||
i = 0 # a variable to keep track of time |
|||
while True: |
|||
time.sleep(.01) # wait 0.01 seconds. if this isn't here the code might run too fast |
|||
i = (i+1) % 256 # run from 0 to 255 |
|||
dot[0] = wheel(i & 255) # set the color of the dotstar, [red green blue] |
|||
led.duty_cycle = i*255 # duty_cycle can be 0 to 65535 |
|||
if i%100==0: # if i is divisible by 100 |
|||
# printing at this slow speed because sending too often chokes the computer |
|||
print("A2 touch: %d" % touch.raw_value, end="\n") # print the value of the cap touch sensor on pin A2 |
|||
''' |
|||
Unzip the folder, rename it "hardware" and add it to the folder |
|||
...documents/arduino/. |
|||
The RGB LEDs on the tutorial board use a special library called |
|||
"Neopixel." Download the Neopixel library from |
|||
https://github.com/adafruit/Adafruit_NeoPixel |
|||
Unzip the folder, rename it "Adafruit_NeoPixel", and add the folder to |
|||
...documents/library/ (library might not exist, so make it there) |
|||
Tasks: |
|||
Edit the tutorial example files to: |
|||
* Read the push button, turn the RGB LEDs on and off |
|||
* Read the push button, toggle the RGB LEDs on and off |
|||
* Read the push button, use Delay() to make a blinky pattern with the RGB LEDs |
Revision as of 18:46, 3 October 2018
Halloween Tutorial 1
We are using the Itsy Bitsy M0 Express from Adafruit.
Mu ("Moo") is a nice IDE used to edit the code and debug.
- First, plug the board in a open Mu. Load main.py from the CIRCUITPY drive. Open the serial monitor.
- Second, go into REPL mode and communicate with the board. Practice entering (CTRL-c) and exiting (CTRL-D) REPL.
- Third, make a local copy of main.py, in case you ever want to see the original examples again.
- Forth, replace the contents of main.py with:
import board # pin definitions import time # time functions import touchio # capacitivectouch library import pulseio # pwm library import adafruit_dotstar # dotstar rgb led library
- One dotstar soldered on the board
dot = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
- Built in red LED
led = pulseio.PWMOut(board.D13, frequency=5000, duty_cycle=0) led.duty_cycle = 0 # initially off
- Capacitive touch on A2
touch = touchio.TouchIn(board.A2)
- Helper to give us a nice color swirl
def wheel(pos):
# Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if (pos < 0): return [0, 0, 0] if (pos > 255): return [0, 0, 0] if (pos < 85): return [int(pos * 3), int(255 - (pos*3)), 0] elif (pos < 170): pos -= 85 return [int(255 - pos*3), 0, int(pos*3)] else: pos -= 170 return [0, int(pos*3), int(255 - pos*3)]
- MAIN LOOP ##############################
i = 0 # a variable to keep track of time while True:
time.sleep(.01) # wait 0.01 seconds. if this isn't here the code might run too fast i = (i+1) % 256 # run from 0 to 255 dot[0] = wheel(i & 255) # set the color of the dotstar, [red green blue] led.duty_cycle = i*255 # duty_cycle can be 0 to 65535 if i%100==0: # if i is divisible by 100 # printing at this slow speed because sending too often chokes the computer print("A2 touch: %d" % touch.raw_value, end="\n") # print the value of the cap touch sensor on pin A2