I’ve added a volume controller and servo to the Raspberry Pi Pico, so I’ll upload it.
Sorry for the rough image. This time, I prioritized speed over quality.
Sites I used as reference:
LCD display
Servo (a device that spins)
Right-Left Switch
LED light
For the LED, I used a 330R resistor for red, a 330R resistor for white, and a 330R+220R=550R resistor for blue.
For the switch, I used a 330R resistor.
Please be sure to check the resistors as they may damage the LEDs, etc.
def servo_value(degree):
return int((degree * 9.5 / 180 + 2.5) * 65535 / 100)
import _thread, time
from lcd1602 import LCD
from machine import Pin, PWM
led=Pin(25,Pin.OUT)
led.value(1)
lcd = LCD()
string = 'Valid Code Co.,\n'
lcd.message(string)
led_blue=Pin(16,Pin.OUT)
led_white=Pin(17,Pin.OUT)
led_red=Pin(18,Pin.OUT)
button = machine.Pin(14, machine.Pin.IN)
mode = 'auto'
buf = ''
pwm = PWM(Pin(25))
pwm.freq(1000)
pwmServo = PWM(Pin(19))
pwmServo.freq(50)
sensor_adc = machine.ADC(0)
conversion_factor = 3.3 / (65535)
cnt = [0] # スレッドで共有する変数
# Core1 並列処理(本体LED点滅&カウント数表示) ---------------------------------------------------------------
def core0(cnt):
try:
while True: # ずっと繰り返し
if button.value() == 0:
#Manual
lcd.clear()
lcd.message(string + ' ON')
mode = 'manual'
led.value(1)
if 'blue' == buf:
led_blue.value(1)
led_white.value(0)
led_red.value(0)
elif 'white' == buf:
led_blue.value(0)
led_white.value(1)
led_red.value(0)
elif 'red' == buf:
led_blue.value(0)
led_white.value(0)
led_red.value(1)
else:
#auto
lcd.clear()
lcd.message(string + ' It\'s my turn.')
mode = 'auto'
led.value(0)
led_blue.value(1)
time.sleep(1)
led_blue.value(0)
led_white.value(1)
time.sleep(1)
led_white.value(0)
led_red.value(1)
time.sleep(1)
led_red.value(0)
except KeyboardInterrupt:
pass
def core1(cnt):
try:
while True: # ずっと繰り返し
reading = sensor_adc.read_u16()
pwm.duty_u16(reading)
#print('ADC', reading)
pwmServo.duty_u16(servo_value(sensor_adc.read_u16() * 180 / 65535))
time.sleep(0.1)
except KeyboardInterrupt:
pass
# 新しいスレッドでcore1関数を実行 ※引数はタプルで指定(要素1つはカンマ付)
_thread.start_new_thread(core1, (cnt,))
# メインスレッドでcore0関数を実行
core0(cnt)