コード例 #1
0
led1 = 4                    # LED D1 on GPIO 4 (active high)
led2 = 17                   # LED D2 on GPIO 17 (active high)

# configure I/O
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# configure pins
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)

# setup PWM outputs
p1 = GPIO.PWM(led1, 100)    # use 100 Hz for both
p2 = GPIO.PWM(led2, 100)    
p1.start(0)                 # D1 initially off
p2.start(100)               # D2 initially on (duty cycle = 100)

try:
    while True:
        # calculate duty cycle from current value (0-1023) of ADC input
        dc = readadc(adc_chan)/1023*100

        # adjust PWM outputs
        p1.ChangeDutyCycle(dc)
        p2.ChangeDutyCycle(100-dc)

except KeyboardInterrupt:
    p1.stop()
    p2.stop()
    GPIO.cleanup()
コード例 #2
0
# Continually prints temperature sensed by MCP9700
#  on Wombat board analog input 2
#  in degrees Celsius and Fahrenheit
#
#   v1.0    6/5/15
#
#   David Meiklejohn
#   Gooligum Electronics
#

import time
from wombat import readadc

adc_chan = 2        # MPC9700 temp sensor is on analog input CH2

try:
  while True:
    # get current voltage (0-3.3V) on ADC input
    Vout = readadc(adc_chan)*3.3/1024

    # convert voltage to temperature in degrees C and F
    TempC = (Vout - 0.5) * 100
    TempF = TempC * 9/5 + 32

    print("temperature = %4.1f C" % TempC, "= %4.1f F" % TempF)
    time.sleep(0.5)

except KeyboardInterrupt:
  print("done")

コード例 #3
0
#
# Simple light meter
#
# Continually prints light level detected by LDR/resistor divider
#  on Wombat board analog input 1
#  as percentage between 0 (dark) and 100 (bright light)
#
#   v1.0    3/5/15
#
#   David Meiklejohn
#   Gooligum Electronics
#

import time
from wombat import readadc

adc_chan = 1        # LDR is on analog input CH1

try:
  while True:
    # get current value (0-1023) of ADC input
    adc_out = readadc(adc_chan)

    light_level = (adc_out * 100)/1023
    print("light level = %4.1f %%" % (light_level))
    time.sleep(0.5)

except KeyboardInterrupt:
  print("done")

コード例 #4
0
# constants
threshold = 50              # light level to turn on/off LED

# configure I/O
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# configure pins
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)

# setup PWM outputs
p1 = GPIO.PWM(led1, 100)    # use 100 Hz
p1.start(0)                 # D1 initially off

try:
    while True:
        # derive light level (0-100) from current value (0-1023) of ADC input
        light_level = readadc(adc_chan)/1023*100

        # adjust PWM output on LED 1
        # (duty cycle is inverse of light level -> bright LED in low light)
        p1.ChangeDutyCycle(100-light_level)

        # turn on LED 2 if light level below threshold
        GPIO.output(led2, light_level < threshold)

except KeyboardInterrupt:
    p1.stop()
    GPIO.cleanup()
コード例 #5
0
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

# setup PWM outputs
pr = GPIO.PWM(red, 100)     # use 100 Hz for all
pg = GPIO.PWM(green, 100)    
pb = GPIO.PWM(blue, 100)    
pr.start(0)                 # all initially off
pg.start(0)
pb.start(0)

try:
    while True:
        # convert ADC input level (0 - 1023) to temperature (-50 - 280 deg C)
        temp = (readadc(adc_chan)*3.3/1023 - 0.5) * 100

        # map temperature to hue
        #   min_temp corresponds to hue = 0,
        #   max_temp corresponds to hue = 1
        if temp < min_temp:
            hue = 0
        elif temp > max_temp:
            hue = 1
        else:
            hue = (temp - min_temp) / (max_temp - min_temp)

        # convert hue to RGB values (0-1)
        rgb = colorsys.hsv_to_rgb(hue, 1, 1)  # colours are fully saturated, max value

        # adjust PWM outputs
コード例 #6
0
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

# setup PWM outputs
pr = GPIO.PWM(red, 100)     # use 100 Hz for all
pg = GPIO.PWM(green, 100)    
pb = GPIO.PWM(blue, 100)    
pr.start(0)                 # all initially off
pg.start(0)
pb.start(0)

try:
    while True:
        # derive hue (0-1) from current value (0-1023) of ADC input
        hue = readadc(adc_chan)/1023

        # convert to RGB values (0-1)
        rgb = colorsys.hsv_to_rgb(hue, 1, 1)  # colours are fully saturated, max value

        # adjust PWM outputs
        pr.ChangeDutyCycle(rgb[0]*100)      # red
        pg.ChangeDutyCycle(rgb[1]*100)      # green
        pb.ChangeDutyCycle(rgb[2]*100)      # blue

except KeyboardInterrupt:
    pr.stop()
    pg.stop()
    pb.stop()
    GPIO.cleanup()