- Table of Contents
- Hello_CircuitPython
- CircuitPython_Servo
- CircuitPython Photointerrupter
- CircuitPython_LCD
- NextAssignmentGoesHere
Makes the on-board neopixel on the metro express blink, in my case, blink different, random, colours. I used a bunch of random values between 0, 255 to control each of the colour values with integers hooked up to
random.randint(0, 255)
Also it should be noted I turned down the brightness with
dot.brightness = 0.1
so it was a little less blinding and less likely to burn out. Other than that, it was basically the same as coding an off-board LED on an arduino, except with specific colour values and mildly different code.
image credit goes to Josie Muss
Twas' but making a neopixel blink, no challenges were encountered. Just turn down the brightness so it doesn't burn out.
Makes a 180 degree micro servo move in different directions based on interacting with wires connected to different ports. Uses the 'touchio' and 'servo' libraries which allow you to take input from interactions with the input ports, and of course move the servo. Here's a sample of the kinda most vital part of code:
while True:
if touchA3.value:
print("A3 Touch")
for angle in range(0, 180, 5):
my_servo.angle = angle
time.sleep(0.3)
No GIF for this, you can see the angle of the servo change between the pictures.
image credit goes to Josie Muss the two wires coming off of the board are the capacitive touch wires.
Had some trouble in regards to code, don't remember what I did exactly to fix it, but just using previous servo code worked, just make sure to reuse code to prevent further problems.
Uses the HCSR04 to find the distance and translate said distance to an RGB value on the neopixel. I used the "map" function from the simpleio library to translate it into an RGB value:
red = int(simpleio.map_range(distance, 5, 20, 255, 0))
Otherwise the code is pretty much a matter of coordinating all those values, and getting the distance with
distance = sonar.distance
which is remarkably easy.
Image credit goes to [Benton House](https://github.com/Jhouse53/CircuitPython)DEFINITELY had some problems with this one. For the whole time my code was being frustrating and it would stop working every time that I actually got a distance value. It took a lot of skillful use of the serial monitor using the "print" function in order to deduce exactly what was wrong with it. In the end the main problem I was having is that most of the RGB values returned a 'float' value instead of an integer (the numbers had decimals in them) and apparently the neopixel does not work with float values. All it took was adding that little 'int' at the front of the line of code which translated the distance into RGB to fix it. P.S. also make sure your
dot.fill((value))
has two parentheses in it.
Thank you Ghislain Ventre on github for the code. Had to be adapted a bit though, as it's rather outdated and not perfect for the assignment. Uses a counter and if statements as opposed to time.sleep so a photointerrupter is able to count the amount of times it has been interrupted, while still having the same effect as a delay. Also flickers the neopixel on the board every time it is interrupted.
start = time.time()
while True:
photo = interrupter.value
if photo and not state:
counter += 1
state = photo
This code looks real nice doesn't it
Wiring diagram credit to Josie Muss
Biggest lesson learned from this is that life is much easier when using other people's code. Not only did it save me a lot of time but simply seeing the code helped me understand how it was used. Make sure you don't mess up your photointerrupter wiring though.
Uses a capacitive touch circuit as an input to add to a counter whenever the wire is touched, with it toggling whether it's adding or subtracting with a different wire. Uses an LCD backpack to display the number.
if touchA2.value and salad1 == 0:
print("A2 Touch")
if direction == 1:
print(direction)
counter += 1
lcd.clear()
lcd.print(str(counter))
lcd.print("\nCounting Up")
if direction == 0:
print(direction)
counter -= 1
lcd.clear()
lcd.print(str(counter))
lcd.print("\nCounting Down")
As you can see I used a variable named "direction" to toggle whether it counted up or down.
The main problem I had was making it so it didn't continue counting if you held it down. I kind of overcomplicated things at first but eventually found a simpler way of doing it. The LCD wiring was a bit annoying to do but turns out you need to wire like 4 different wires so it's pretty easy in the end.
Code goes here