Skip to content

Commit

Permalink
i'll do the rest on OKmeque1 account
Browse files Browse the repository at this point in the history
  • Loading branch information
GmaerSoft42 authored Jun 7, 2024
1 parent 8fe54e1 commit 7923ba2
Show file tree
Hide file tree
Showing 46 changed files with 3,013 additions and 0 deletions.
15 changes: 15 additions & 0 deletions PythonSoft/Kits/KS3021/1. LED/Blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 1.2
* Blink
* http://www.keyestudio.com
'''
from machine import Pin
import time

led = Pin(0, Pin.OUT)# 构建led对象,外接LED灯连接与引脚0相连,并设置引脚0为输出模式
while True:
led.value(1)# led点亮
time.sleep(1)# 等待1秒
led.value(0)# led熄灭
time.sleep(1)# 等待1秒
12 changes: 12 additions & 0 deletions PythonSoft/Kits/KS3021/1. LED/LED.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 1.1
* turn on led
* http://www.keyestudio.com
'''
from machine import Pin

led = Pin(0, Pin.OUT)# 构建led对象,外接LED灯连接与引脚0相连,并设置引脚0为输出模式

led.value(1)# 高电平点亮

26 changes: 26 additions & 0 deletions PythonSoft/Kits/KS3021/10. RGB/rgb1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 10.1
* RGB
* http://www.keyestudio.com
'''
from machine import Pin
from time import sleep

red = Pin(9, Pin.OUT)
green = Pin(10, Pin.OUT)
blue = Pin(11, Pin.OUT)

while 1:
red.value(1)
green.value(0)
blue.value(0)
sleep(1)
red.value(0)
green.value(1)
blue.value(0)
sleep(1)
red.value(0)
green.value(0)
blue.value(1)
sleep(1)
36 changes: 36 additions & 0 deletions PythonSoft/Kits/KS3021/10. RGB/rgb2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 10.2
* RGB
* http://www.keyestudio.com
'''
from machine import Pin, PWM
from time import sleep
pwm_r = PWM(Pin(9))
pwm_g = PWM(Pin(10))
pwm_b = PWM(Pin(11))

pwm_r.freq(1000)
pwm_g.freq(1000)
pwm_b.freq(1000)

def light(red, green, blue):
pwm_r.duty_u16(red)
pwm_g.duty_u16(green)
pwm_b.duty_u16(blue)

while 1:
light(65535, 0, 0)#红
sleep(1)
light(65535, 25088, 0)#橙
sleep(1)
light(65535, 65535, 0)#黄
sleep(1)
light(0, 65535, 0)#绿
sleep(1)
light(0, 0, 65535)#蓝
sleep(1)
light(0, 65535, 65535)#青
sleep(1)
light(41216, 8448, 61696)#紫
sleep(1)
15 changes: 15 additions & 0 deletions PythonSoft/Kits/KS3021/11. potentiometer/potentiometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 11
* Rotary potentiometer
* http://www.keyestudio.com
'''
import machine
import utime

potentiometer = machine.ADC(26)

while True:
pot_value = potentiometer.read_u16()
print(pot_value,end='\r')
utime.sleep(0.1)
14 changes: 14 additions & 0 deletions PythonSoft/Kits/KS3021/12. MicroPhone/MicroPhone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 12
* MicroPhone
* http://www.keyestudio.com
'''
import machine
import utime

MicroPhone = machine.ADC(27)
while True:
value = MicroPhone.read_u16()
print(value)
utime.sleep(0.1)
15 changes: 15 additions & 0 deletions PythonSoft/Kits/KS3021/13. Photoresistance/photoresistance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 13
* Photoresistance
* http://www.keyestudio.com
'''
import machine
import utime

photoresistance = machine.ADC(28)
while True:
value = photoresistance.read_u16()
print(value)
utime.sleep(0.1)

16 changes: 16 additions & 0 deletions PythonSoft/Kits/KS3021/14. Temperature sensor/temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 14
* Temperature sensor
* http://www.keyestudio.com
'''
import machine
import utime
import math

sensor = machine.ADC(2)
while True:
temp = sensor.read_u16()
print("Temperature ADC: ", end = " ")
print(temp)
utime.sleep(0.1)
14 changes: 14 additions & 0 deletions PythonSoft/Kits/KS3021/15. film pressure sensor/film pressure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 15
* Film pressure sensor
* http://www.keyestudio.com
'''
import machine
import utime

film = machine.ADC(1)
while True:
value = film.read_u16()
print(value)
utime.sleep(0.1)
23 changes: 23 additions & 0 deletions PythonSoft/Kits/KS3021/16. Joystick/joystick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 16
* Joystick
* http://www.keyestudio.com
'''
import machine
import utime

B = machine.Pin(22, machine.Pin.IN)
X = machine.ADC(26)
Y = machine.ADC(27)
while True:
B_value = B.value()
X_value = X.read_u16()
Y_value = Y.read_u16()
print("button:", end = " ")
print(B_value, end = " ")
print("X:", end = " ")
print(X_value, end = " ")
print("Y:", end = " ")
print(Y_value)
utime.sleep(0.1)
74 changes: 74 additions & 0 deletions PythonSoft/Kits/KS3021/17. SK6812/RGB Led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 17
* 6812 RGB LED
* http://www.keyestudio.com
'''
import array, time
from machine import Pin
import rp2

# Configure the number of sk6812 LEDs, pins and brightness.
NUM_LEDS = 4
PIN_NUM = 16
brightness = 0.1


@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def sk6812():
T1 = 2
T2 = 5
T3 = 3
wrap_target()
label("bitloop")
out(x, 1) .side(0) [T3 - 1]
jmp(not_x, "do_zero") .side(1) [T1 - 1]
jmp("bitloop") .side(1) [T2 - 1]
label("do_zero")
nop() .side(0) [T2 - 1]
wrap()


# Create the StateMachine with the sk6812 program, outputting on Pin(16).
sm = rp2.StateMachine(0, sk6812, freq=8_000_000, sideset_base=Pin(PIN_NUM))

# Start the StateMachine, it will wait for data on its FIFO.
sm.active(1)

# Display a pattern on the LEDs via an array of LED RGB values.
ar = array.array("I", [0 for _ in range(NUM_LEDS)])

def pixels_show():
dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)])
for i,c in enumerate(ar):
r = int(((c >> 8) & 0xFF) * brightness)
g = int(((c >> 16) & 0xFF) * brightness)
b = int((c & 0xFF) * brightness)
dimmer_ar[i] = (g<<16) + (r<<8) + b
sm.put(dimmer_ar, 8)
time.sleep_ms(10)

def pixels_set(i, color):
ar[i] = (color[1]<<16) + (color[0]<<8) + color[2]

def pixels_fill(color):
for i in range(len(ar)):
pixels_set(i, color)

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

pixels_set(0, RED)
pixels_set(1, GREEN)
pixels_set(2, BLUE)
pixels_set(3, WHITE)
pixels_show()
time.sleep(5)
'''
for i in range(len(ar)):
pixels_set(i, BLACK)
pixels_show()
'''
32 changes: 32 additions & 0 deletions PythonSoft/Kits/KS3021/18. Encoder/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 18
* Encoder
* http://www.keyestudio.com
'''
import time
from rotary_irq_rp2 import RotaryIRQ
from machine import Pin
SW=Pin(20,Pin.IN,Pin.PULL_UP)
r = RotaryIRQ(pin_num_clk=18,
pin_num_dt=19,
min_val=0,
reverse=False,
range_mode=RotaryIRQ.RANGE_UNBOUNDED)
val_old = r.value()
while True:
try:
val_new = r.value()
if SW.value()==0 and n==0:
print("Button Pressed")
print("Selected Number is : ",val_new)
n=1
while SW.value()==0:
continue
n=0
if val_old != val_new:
val_old = val_new
print('result =', val_new)
time.sleep_ms(50)
except KeyboardInterrupt:
break
30 changes: 30 additions & 0 deletions PythonSoft/Kits/KS3021/19. Servo/Servo test 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 19.1
* Servo test 1
* http://www.keyestudio.com
'''
from machine import Pin, PWM
import time
pwm = PWM(Pin(0))
pwm.freq(50)

'''
角度对应的占空比
0°----2.5%----1638
45°----5%----3276
90°----7.5%----4915
135°----10%----6553
180°----12.5%----8192
'''
angle_0 = 1638
angle_90 = 4915
angle_180 = 8192

while True:
pwm.duty_u16(angle_0)
time.sleep(1)
pwm.duty_u16(angle_90)
time.sleep(1)
pwm.duty_u16(angle_180)
time.sleep(1)
38 changes: 38 additions & 0 deletions PythonSoft/Kits/KS3021/19. Servo/Servo test 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
* Keyestudio 24 in 1 Starter Kit for Raspberry Pi Pico
* lesson 19.2
* Servo test 2
* http://www.keyestudio.com
'''
from utime import sleep
from machine import Pin
from machine import PWM

pwm = PWM(Pin(0))#舵机引脚接GP0
pwm.freq(50)#20ms的周期,所以频率为50Hz
'''
角度对应的占空比
0°----2.5%----1638
45°----5%----3276
90°----7.5%----4915
135°----10%----6553
180°----12.5%----8192
考虑到误差,将占空比定在1000~9000,这样可以顺利转动0~180度
'''
# 设置伺服马达的转动角度
def setServoCycle (position):
pwm.duty_u16(position)
sleep(0.01)

# 将转动角度换算成占空比
def convert(x, i_m, i_M, o_m, o_M):
return max(min(o_M, (x - i_m) * (o_M - o_m) // (i_M - i_m) + o_m), o_m)

while True:
for degree in range(0, 180, 1):#从0°转动到180°
pos = convert(degree, 0, 180, 1000, 9000)
setServoCycle(pos)

for degree in range(180, 0, -1):#从180°转动到0°
pos = convert(degree, 0, 180, 1000, 9000)
setServoCycle(pos)
Loading

0 comments on commit 7923ba2

Please sign in to comment.