Esempio n. 1
0
import pyb

CRITICAL_THRESHOLD = 150
WARNING_THRESHOLD = 100

dac = pyb.DAC(1)  # create a DAC object
adc = pyb.ADC('X22')  #  setup X22 pin ADC channel
tim = pyb.Timer(6, freq=20000)

critical_pin = pyb.Pin.board.LED_RED
warning_pin = pyb.Pin.board.LED_YELLOW
works_pin = pyb.Pin.board.LED_BLUE


def record():
    buf = bytearray(20000)  #  create a 20000 byte array to store samples
    adc.read_timed(buf, tim)  #  start the ADC sampling
    return buf


while True:
    # 1 sec
    buf = record()
    delta = max(buf) - min(buf)

    if delta >= CRITICAL_THRESHOLD:
        critical_pin.on()
        warning_pin.off()
        works_pin.off()
    elif WARNING_THRESHOLD <= delta <= CRITICAL_THRESHOLD:
        critical_pin.off()
Esempio n. 2
0
import gc, utime

import pyb, micropython, LPF2Class

adc = pyb.ADC('X6')  # set up analog pin

micropython.alloc_emergency_exception_buf(200)  # shows errors

red_led = pyb.LED(1)
red_led.on()

lpf2 = LPF2Class.LPF2(
    3, 'Y9', 'Y10', timer=4,
    freq=5)  # OpenMV UART #, Tx, Rx, callback timer, and timer frequency
lpf2.initialize()

# Loop
while True:

    if not lpf2.connected:
        lpf2.sendTimer.callback(None)  # clear any earlier
        red_led.on()  # tell user connecting
        utime.sleep_ms(200)
        lpf2.initialize()
    else:
        red_led.off()  # signal connected
        lpf2.load_payload(
            int(adc.read() / 500)
        )  # range of FSR should be 0-5000 mV, so divde by 500 to fit in the 0-9 digit range for SPIKE
        data = lpf2.readIt()
Esempio n. 3
0
import setup
import pyb
import time

#joystick
joystic_y = pyb.ADC('A0')
joystic_x = pyb.ADC('A1')


def joystic_val():
    global joystic_x, joystic_y
    y = joystic_y.read() - 2047
    if abs(y) < 100:
        y = 0

    y = y / 20.48

    x = joystic_x.read() - 2047
    if abs(x) < 100:
        x = 0

    x = x / 20.48
    return x, y


m1 = setup.get_motor_with_encoder1()

m2 = setup.get_motor_with_encoder2()
m2_ls = setup.get_limit_switches_motor2()

m3 = setup.get_motor_with_encoder3()
# ----------------------------------------------------------------- #

# imports
import machine
import framebuf
import pyb
import time
import sys

# ----------------------------------------------------------------- #

# hardware components settings
i2c = machine.I2C('X')
y4 = machine.Pin('Y4')
adc = pyb.ADC(y4)

# ----------------------------------------------------------------- #

# LCD settings
width, high = 64, 32
fbuf = framebuf.FrameBuffer(bytearray(width * high // 8), width, high,
                            framebuf.MONO_HLSB)
fbuf.fill(0)

# ----------------------------------------------------------------- #

# init settings
playerWidth, playerHigh = 2, 2
posPlayerX = int(
    (width - playerWidth) / 2)  # int(): in case of a decimal number
Esempio n. 5
0
        for a in range(100):
            cr.pulse_width_percent((rf - ri) * 0.01 * (100 - a) + ri)
            cg.pulse_width_percent((gf - gi) * 0.01 * (100 - a) + gi)
            cb.pulse_width_percent((bf - bi) * 0.01 * (100 - a) + gi)
            pyb.delay(wait)


gradient(0, 0, 0, 200, 0, 0, 10, 3)
gradient(0, 0, 50, 100, 0, 200, 10, 3)
gradient(40, 0, 250, 0, 40, 80, 10, 3)

r = Led(Pin.cpu.A1)
g = Led(Pin.cpu.A2)
b = Led(Pin.cpu.A3)

adc = pyb.ADC(Pin.cpu.A4)
val = adc.read()
ADCMAX = 4096
F = 100
wait = 10
tim = Timer(2, freq=1000)
cr = tim.channel(2, Timer.PWM_INVERTED, pin=r.pin)
cg = tim.channel(3, Timer.PWM_INVERTED, pin=g.pin)
cb = tim.channel(4, Timer.PWM_INVERTED, pin=b.pin)

################ Red color intensity by PWM

while True:
    val = adc.read()
    width = int(F * val / ADCMAX)
    tim = Timer(2, freq=1000)
Esempio n. 6
0
 def _mode_analog(self):
     self._mode_input()
     self._adc = pyb.ADC(self._name)
Esempio n. 7
0
import task_share
import print_task

import controller
import encoder
import motor
import time
import utime

# Allocate memory so that exceptions raised in interrupt service routines can
# generate useful diagnostic printouts
micropython.alloc_emergency_exception_buf (100)

pinC0 = pyb.Pin (pyb.Pin.board.PC0, pyb.Pin.ANALOG)
queue = task_share.Queue('f', 1000)
adc = pyb.ADC(pinC0)
pinC1 = pyb.Pin (pyb.Pin.board.PC1, pyb.Pin.OUT_PP)

def main():
    global pinC0 
    global queue
    global adc
    global pinC1
    tim = pyb.Timer(1)   
    tim.init(freq= 1000)
    tim.callback(interrupt)

    while True :
        pinC1.high ()
        
        start_time = utime.ticks_ms()
Esempio n. 8
0
import createMouseXY
import pyb

adc = pyb.ADC('X19')
adc2 = pyb.ADC('X20')
key = pyb.Pin('X1', pyb.Pin.IN)
# hid的1代表按下
hid = pyb.USB_HID()

while True:
    # 归一化
    value_x = adc.read() / 4096
    value_y = adc2.read() / 4096
    # 值匹配
    value_x = value_x * 2 - 1
    value_y = value_y * -2 + 1
    value_left = not key.value()  # 我的这个设备按钮按下是0,所以取反
    # 开始输入
    ret_x, ret_y = createMouseXY.getValue(value_x, value_y)
    # print('value_x:%g,value_y:%g' % (value_x, value_y))
    # print('ret_x:%g,ret_y:%g' % (ret_x, ret_y))
    hid.send((value_left, int(ret_x), int(ret_y), 0))
    pyb.delay(20)
Esempio n. 9
0
 def __init__(self, rangeFinderPin, cal):
     ''' Calibration: Distance [m] = a + b /( V + k ) , cal [ a b k ]'''
     self.adc = pyb.ADC(rangeFinderPin)
     self.cal = cal
     return
Esempio n. 10
0
'''
http://wiki.seeedstudio.com/Grove-Light_Sensor/

connections: 
Ground  - GND 
Power    - 3V3 
NC
Signal - A0
'''

import pyb, utime

Light = pyb.ADC('W24')

while True:
    Light.read()
    utime.sleep_ms(200)
Esempio n. 11
0
#定义常用颜色
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

########################
# 构建4.3寸LCD对象并初始化
########################
d = LCD43M(portrait=1)  #默认方向

d.fill(WHITE)  #填充白色

#构建ADC对象,引脚PA5
adc = pyb.ADC('A5')

#显示标题
d.printStr('01Studio ADC', 100, 10, BLACK, size=4)

while True:

    #电压采集
    value = str(adc.read())  #原始值
    vol = str('%.2f' % (adc.read() / 4095 * 3.3))  #电压值,0-3.3V

    #显示测量值和电压值
    d.printStr('Vol:' + vol + " V", 10, 100, BLACK, size=4)
    d.printStr('Value:' + value + "   ", 10, 200, BLACK, size=4)
    d.printStr("(4095)", 300, 200, BLACK, size=4)
    print(value)
Esempio n. 12
0
 def __init__(self, rangePinNumber, rangeMax=50):
     self.adc = pyb.ADC(rangePinNumber)
     self.rangeMax = rangeMax