from machine import Pin
from utime import sleep
from machine import ADC
from math import log
from machine import Timer
import time
import math
import rp2
from rp2 import PIO
last_button_time_stamp=0
def interupt_call_back(pin):
global last_button_time_stamp
cur_b_ts= time.ticks_ms()
bpd = cur_b_ts - last_button_time_stamp
if bpd > 200:
last_button_time_stamp=cur_b_ts
key_press.append(pin)
print(f'key press: {PinID(pin)-bstart}')
def main():
DIR_PIN = 2
STEP_PIN = 3
step_pin = Pin(STEP_PIN, Pin.OUT)
dir_pin = Pin(DIR_PIN, Pin.OUT)
def step_motor(steps, direction, delay_ms):
dir_pin.value(direction)
for _ in range(steps):
step_pin.value(1)
time.sleep_ms(delay_ms)
step_pin.value(0)
time.sleep_ms(delay_ms)
def setup():
dir_pin.value(0)
step_pin.value(0)
def loop():
# Move 200 steps (one rotation) CW over one second
step_motor(200, 1, 5) # 5 ms delay between steps
time.sleep_ms(500) # Wait half a second
# Move 200 steps (one rotation) CCW over 400 millis
step_motor(200, 0, 2) # 2 ms delay between steps
time.sleep_ms(1000) # Wait another second
setup()
global key_press
global last_button_time_stamp
button= machine.Pin(16 , machine.Pin.IN, machine.Pin.PULL_DOWN)
# while True:
# loop()
GAMMA = 0.7
RL10 = 50
light = ADC(26) # Assuming ADC is initialized on pin A0
def read_lux():
analog_value = light.read_u16
total_value=0
for _ in range(16):
total_value += light.read_u16()
average_value = total_value // 16
ADC_RANGE = float((math.pow(2, 16) - 1))
# Convert the average digital value to an analogue voltage value
analogue_voltage = round((average_value / ADC_RANGE) * 3.3, 4 - 1)
resistance = 2000 * analogue_voltage / (1 - analogue_voltage / 5.0)
lux = math.pow(RL10 * 1e3 * math.pow(10, GAMMA) / resistance, (1 / GAMMA))
return lux
# Example usage:
ok=0
pretemp=0
motion = Pin(5, Pin.IN)
led= Pin(28,Pin.OUT)
buzzer = Speaker(11)
thermistor_adc = ADC(27)
@rp2.asm_pio(out_init=[PIO.OUT_LOW])
def echo():
wrap_target()
mov(pins, isr)
mov(isr, invert(isr))
pull(noblock)
mov(x, osr)
mov(y, x)
label("loop")
jmp(y_dec, "loop")
wrap()
sm = rp2.StateMachine(0, echo, freq=1_000_000, out_base=Pin(7))
sm.active(1)
def play(freq):
if freq!=0:
sm.put(1_000_000//freq)
else:
sm.put(0)
while True:
lux_value = read_lux()
if button.value()==1:
print(f'light:{lux_value}')
BETA = 3950 # Beta coefficient of the thermistor
temp_analogValue = thermistor_adc.read_u16() # Assuming adc is an ADC object and A0 is connected to pin 0
celsius = 1 / (log(1 / ((2**16-1) / temp_analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15
if celsius!=pretemp:
pretemp=celsius
print(f'Temprature = {celsius} C')
if celsius>=38:
loop()
if motion.value()==1:
led.value(1)
play(500)
else:
led.value(0)
ok=0
play(0)
if led.value()==1 and ok==0:
print("motion detection alert")
ok=1
sleep(0.5)
# main()
if __name__ == "__main__":
main()
# 1.686683ERC Warnings
gnd1:GND: Short circuit