Difference between revisions of "Meg's Engineering III Notebook"

From CHS Sigma
Jump to: navigation, search
(Code)
(Code)
(3 intermediate revisions by one user not shown)
Line 97: Line 97:
 
[http://wiki.chssigma.com/index.php?title=Gist_Resources Revised Code Here]
 
[http://wiki.chssigma.com/index.php?title=Gist_Resources Revised Code Here]
  
== Assignment Template Meg ==
+
== CircuitPython LCD Meg ==
  
 
=== Description ===
 
=== Description ===
Line 104: Line 104:
 
<div style="height:21em; overflow:auto; border: 1px solid #008">
 
<div style="height:21em; overflow:auto; border: 1px solid #008">
 
   <syntaxhighlight lang="c">
 
   <syntaxhighlight lang="c">
import time
+
# Meg Gist
 +
# CircuitPython LCD
 +
# Thanks Seanan
 +
# Again
 +
 
 
# https://learn.adafruit.com/sensor-plotting-with-mu-and-circuitpython/buttons-and-switch
 
# https://learn.adafruit.com/sensor-plotting-with-mu-and-circuitpython/buttons-and-switch
 
# https://learn.adafruit.com/i2c-spi-lcd-backpack/python-circuitpython
 
# https://learn.adafruit.com/i2c-spi-lcd-backpack/python-circuitpython
 
# https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
 
# https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
 +
import time
 
import board
 
import board
 
import digitalio
 
import digitalio
Line 113: Line 118:
 
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface
 
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface
 
from lcd.lcd import CursorMode
 
from lcd.lcd import CursorMode
 
counter = 0
 
switch = 0
 
  
 
lcd = LCD(I2CPCF8574Interface(0x3F), num_rows=2, num_cols=16)
 
lcd = LCD(I2CPCF8574Interface(0x3F), num_rows=2, num_cols=16)
 
# I2c adresses at 0X27 or 0X3F
 
# I2c adresses at 0X27 or 0X3F
 
# Our LCDs are 2X16
 
# Our LCDs are 2X16
 +
 +
buttonCounter = 0
 +
switchCounter = True
  
 
button = digitalio.DigitalInOut(board.D0)
 
button = digitalio.DigitalInOut(board.D0)
Line 128: Line 133:
 
switch.direction = digitalio.Direction.INPUT
 
switch.direction = digitalio.Direction.INPUT
 
switch.pull = digitalio.Pull.UP
 
switch.pull = digitalio.Pull.UP
 
 
lcd.set_cursor_pos(0, 0)
 
# Setting the cursor to the top left
 
lcd.set_cursor_mode(CursorMode.LINE)
 
# Setting the cursor as a visible line
 
lcd.print("Switch: ")
 
lcd.print(switch)
 
lcd.print("Presses: ")
 
lcd.print(counter)
 
 
print(switch)
 
print(counter)
 
  
 
while True:
 
while True:
 
     if switch.value:
 
     if switch.value:
 +
        switchCounter = True
 
         if button.value:
 
         if button.value:
             switch += 1
+
             buttonCounter += 1
             counter += 1
+
             print(buttonCounter)
            lcd.print(" ")
+
             print(switchCounter)
             time.sleep(0.5)
+
 
     else:
 
     else:
 +
        switchCounter = False
 
         if button.value:
 
         if button.value:
             switch -= 1
+
             buttonCounter -= 1
             counter -= 1
+
             print(buttonCounter)
             lcd.print(counter)
+
             print(switchCounter)
            lcd.print(" ")
+
    lcd.set_cursor_pos(1, 0)
            time.sleep(0.5)
+
    lcd.print("Presses:")
 +
    lcd.print(str(buttonCounter))
 +
    lcd.print("       ")
 +
    lcd.set_cursor_pos(0, 0)
 +
    # Setting the cursor to the top left
 +
    lcd.print("Switch:")
 +
    lcd.print(str(switchCounter))
 +
    lcd.print(" ")
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
</div>
 
</div>
Line 162: Line 162:
  
 
=== Reflections & Solutions ===
 
=== Reflections & Solutions ===
 
 
 
 
  
 
== Assignment Template Meg ==
 
== Assignment Template Meg ==
Line 174: Line 170:
 
<div style="height:21em; overflow:auto; border: 1px solid #008">
 
<div style="height:21em; overflow:auto; border: 1px solid #008">
 
   <syntaxhighlight lang="c">
 
   <syntaxhighlight lang="c">
 +
# Meg Gist
 +
# CircuitPython Photointerrupters
 +
 
import time
 
import time
# https://learn.adafruit.com/sensor-plotting-with-mu-and-circuitpython/buttons-and-switch
 
 
import board
 
import board
 
import digitalio
 
import digitalio
  
button_a = digitalio.DigitalInOut(board.D0)
+
interruptValue = True
button_a.direction = digitalio.Direction.INPUT
+
interruptCounter = 0
button_a.pull = digitalio.Pull.DOWN
+
 
+
photo = digitalio.DigitalInOut(board.D2)
switch = digitalio.DigitalInOut(board.D2)
+
photo.direction = digitalio.Direction.INPUT
switch.direction = digitalio.Direction.INPUT
+
photo.pull = digitalio.Pull.UP
switch.pull = digitalio.Pull.UP
+
  
 
while True:
 
while True:
     if switch.value:
+
     if photo.value:
         if button_a.value:
+
         interruptValue = False
            print("1,")
+
        time.sleep(0.5)
            time.sleep(0.5)
+
       
    else:
+
          
         if button_a.value:
+
interruptCounter += 1
            print("-1")
+
print("Interrupt count:")
            time.sleep(0.5)
+
print(interruptCounter)
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
</div>
 
</div>

Revision as of 10:33, 13 September 2019

Hello CircuitPython Meg

Description

Code

# Meg Gist
# Hello CircuitPython
 
import analogio
# importing the Analog In
import board
import time
 
#pin.deinit()
#led.direction = analogio.Direction.OUTPUT
 
led = analogio.AnalogOut(board.A0)
# like "dot = neopixel.NeoPixel(board.NEOPIXEL, 1)"
# but instead with the LED
B = 65000
# inital LED brightness value
amount = 1000
ceiling = 50000
# highest value the LED can omit
floor = 20000
# lowest value the LED can omit
 
while True:
    led.value = B
    B -= amount
    time.sleep(.01)
    if B < floor:
        amount = -1000
    elif B > ceiling:
        amount = 1000
    time.sleep(.01)

Wiring & Pictures

Reflections & Solutions

LED Basics with Metro M0 Express

CircuitPython Servo Meg

Description

Code

# Meg Gist
# CircuitPython Servo
# Thanks Seanan
 
import time
import pulseio
import board
import touchio
from adafruit_motor import servo
 
touch_A0 = touchio.TouchIn(board.A0)  # A0-A5 touch on Metro M0 Express
touch_A4 = touchio.TouchIn(board.A4)  # A0-A5 touch on Metro M0 Express
 
# create a PWMOut object on Pin A2
pwm = pulseio.PWMOut(board.A2, frequency=50)
# Create a servo object, mine's named "moth_slippers"
moth_slippers = servo.ContinuousServo(pwm)
 
while True:
    if touch_A0.value:
        moth_slippers.throttle = 1.0
        print("A0")
    if touch_A4.value:
        moth_slippers.throttle = -1.0
        print("A4")
    if not touch_A0.value and not touch_A4.value:
        # must use one if statement rather than two else
        # if else was used the statments would cancel out
        # making the A4 wire work but not the A0
        moth_slippers.throttle = 0.0
        # makes sure servo doesn't move when wires aren't touched
    time.sleep(0.1)
    # works best when the sleep is super low

Wiring & Pictures

Reflections & Solutions

Capacitive Touch Basic Code Metro M0 Express

Servo Basic Code Metro M0 Express

Revised Code Here

CircuitPython LCD Meg

Description

Code

# Meg Gist
# CircuitPython LCD
# Thanks Seanan
# Again
 
# https://learn.adafruit.com/sensor-plotting-with-mu-and-circuitpython/buttons-and-switch
# https://learn.adafruit.com/i2c-spi-lcd-backpack/python-circuitpython
# https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
import time
import board
import digitalio
from lcd.lcd import LCD
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface
from lcd.lcd import CursorMode
 
lcd = LCD(I2CPCF8574Interface(0x3F), num_rows=2, num_cols=16)
# I2c adresses at 0X27 or 0X3F
# Our LCDs are 2X16
 
buttonCounter = 0
switchCounter = True
 
button = digitalio.DigitalInOut(board.D0)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.DOWN
 
switch = digitalio.DigitalInOut(board.D2)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
 
while True:
    if switch.value:
        switchCounter = True
        if button.value:
            buttonCounter += 1
            print(buttonCounter)
            print(switchCounter)
    else:
        switchCounter = False
        if button.value:
            buttonCounter -= 1
            print(buttonCounter)
            print(switchCounter)
    lcd.set_cursor_pos(1, 0)
    lcd.print("Presses:")
    lcd.print(str(buttonCounter))
    lcd.print("       ")
    lcd.set_cursor_pos(0, 0)
    # Setting the cursor to the top left
    lcd.print("Switch:")
    lcd.print(str(switchCounter))
    lcd.print(" ")

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

# Meg Gist
# CircuitPython Photointerrupters
 
import time
import board
import digitalio
 
interruptValue = True
interruptCounter = 0
 
photo = digitalio.DigitalInOut(board.D2)
photo.direction = digitalio.Direction.INPUT
photo.pull = digitalio.Pull.UP
 
while True:
    if photo.value:
        interruptValue = False
        time.sleep(0.5)
 
 
interruptCounter += 1
print("Interrupt count:")
print(interruptCounter)

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

 

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

 

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

 

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

 

Wiring & Pictures

Reflections & Solutions