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

From CHS Sigma
Jump to: navigation, search
(Code)
(Code)
(2 intermediate revisions by one user not shown)
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">
# Meg Gist
+
import time
# CircuitPython LCD
+
# 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 board
 
import board
 
import digitalio
 
import digitalio
Line 115: Line 113:
 
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)
Line 120: Line 121:
 
# Our LCDs are 2X16
 
# Our LCDs are 2X16
  
button = digitalio.DigitalInOut(board.D1)
+
button = digitalio.DigitalInOut(board.D0)
 
button.direction = digitalio.Direction.INPUT
 
button.direction = digitalio.Direction.INPUT
 
button.pull = digitalio.Pull.DOWN
 
button.pull = digitalio.Pull.DOWN
Line 127: Line 128:
 
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)
 
lcd.set_cursor_pos(0, 0)
 
# Setting the cursor to the top left
 
# Setting the cursor to the top left
 
lcd.print("Here I am")
 
 
 
lcd.set_cursor_mode(CursorMode.LINE)
 
lcd.set_cursor_mode(CursorMode.LINE)
 
# Setting the cursor as a visible line
 
# Setting the cursor as a visible line
# lcd.clear()
+
lcd.print("Switch: ")
 +
lcd.print(switch)
 +
lcd.print("Presses: ")
 +
lcd.print(counter)
 +
 
 +
print(switch)
 +
print(counter)
  
 
while True:
 
while True:
     if button.value:
+
     if switch.value:
         lcd.print("button")
+
         if button.value:
    elif switch.value:
+
            switch += 1
        print((-1,))
+
            counter += 1
 +
            lcd.print(" ")
 +
            time.sleep(0.5)
 
     else:
 
     else:
         print((0,))
+
         if button.value:
 +
            switch -= 1
 +
            counter -= 1
 +
            lcd.print(counter)
 +
            lcd.print(" ")
 +
            time.sleep(0.5)
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
</div>
 
</div>
Line 163: Line 175:
 
   <syntaxhighlight lang="c">
 
   <syntaxhighlight lang="c">
 
import time
 
import time
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
 
import board
 
import board
 
import digitalio
 
import digitalio
+
 
 
button_a = digitalio.DigitalInOut(board.D0)
 
button_a = digitalio.DigitalInOut(board.D0)
 
button_a.direction = digitalio.Direction.INPUT
 
button_a.direction = digitalio.Direction.INPUT
Line 174: Line 186:
 
switch.direction = digitalio.Direction.INPUT
 
switch.direction = digitalio.Direction.INPUT
 
switch.pull = digitalio.Pull.UP
 
switch.pull = digitalio.Pull.UP
+
 
 
while True:
 
while True:
     # if switch.value:
+
     if switch.value:
 
         if button_a.value:
 
         if button_a.value:
             print((1,))
+
             print("1,")
        else:
+
             time.sleep(0.5)
             print((0,))
+
     else:
     # else:
+
         if button_a.value:
         # if button_a.value:
+
             print("-1")
             # print((-1,))
+
            time.sleep(0.5)
        time.sleep(0.5)
+
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
</div>
 
</div>
Line 204: Line 215:
 
   <syntaxhighlight lang="c">
 
   <syntaxhighlight lang="c">
 
   
 
   
import time
 
import board
 
import digitalio
 
 
led = digitalio.DigitalInOut(board.D13)
 
led.switch_to_output()
 
 
button = digitalio.DigitalInOut(board.BUTTON_A)
 
button.switch_to_input(pull=digitalio.Pull.DOWN)
 
 
while True:
 
    if button.value:  # button is pushed
 
        led.value = True
 
    else:
 
        led.value = False
 
 
    time.sleep(0.01)
 
 
   </syntaxhighlight>
 
   </syntaxhighlight>
 
</div>
 
</div>

Revision as of 10:29, 9 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

Assignment Template Meg

Description

Code

import time
# 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 board
import digitalio
from lcd.lcd import LCD
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface
from lcd.lcd import CursorMode
 
counter = 0
switch = 0
 
lcd = LCD(I2CPCF8574Interface(0x3F), num_rows=2, num_cols=16)
# I2c adresses at 0X27 or 0X3F
# Our LCDs are 2X16
 
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
 
 
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:
    if switch.value:
        if button.value:
            switch += 1
            counter += 1
            lcd.print(" ")
            time.sleep(0.5)
    else:
        if button.value:
            switch -= 1
            counter -= 1
            lcd.print(counter)
            lcd.print(" ")
            time.sleep(0.5)

Wiring & Pictures

Reflections & Solutions

Assignment Template Meg

Description

Code

import time
# https://learn.adafruit.com/sensor-plotting-with-mu-and-circuitpython/buttons-and-switch
import board
import digitalio
 
button_a = digitalio.DigitalInOut(board.D0)
button_a.direction = digitalio.Direction.INPUT
button_a.pull = digitalio.Pull.DOWN
 
switch = digitalio.DigitalInOut(board.D2)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
 
while True:
    if switch.value:
        if button_a.value:
            print("1,")
            time.sleep(0.5)
    else:
        if button_a.value:
            print("-1")
            time.sleep(0.5)

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