import machine
from machine import Pin, I2C, RTC
from ssd1306 import SSD1306_I2C
from mpu6050 import accel
import time
# I2C Tanımlama
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
# RTC Oluşturma
rtc = RTC()
# MPU6050 Oluşturma
mpu = accel(i2c)
# SSD1306 Oluşturma
oled = SSD1306_I2C(128, 64, i2c)
# HC-SR04 Pin Tanımlama
trig_pin = Pin(23, Pin.OUT)
echo_pin = Pin(19, Pin.IN)
# Potansiyometre ve Buzzer Pin Tanımlama
pot_pin = Pin(34, Pin.IN)
buzzer_pin = Pin(26, Pin.OUT)
# Eşik değeri
distance_threshold = 200
def hareket_algilama():
# Hareket algılandığında yapılacak işlemler
oled.fill(0) # Clear the screen
oled.text("Hareket var!", 0, 0)
# Read distance from HC-SR04
distance = read_distance()
oled.text("Distance: {} cm".format(distance), 0, 20)
# Display current time
current_time = rtc.datetime()
oled.text("Time: {:02d}:{:02d}:{:02d}".format(current_time[4], current_time[5], current_time[6]), 0, 30)
# Check if distance exceeds the threshold
if distance > distance_threshold:
# Buzz for 1 second
buzzer_pin.on()
time.sleep(1)
buzzer_pin.off()
# Blink the screen
for _ in range(5): # Blink 5 times
oled.fill(1) # Turn on all pixels
oled.show()
time.sleep(0.2)
oled.fill(0) # Turn off all pixels
oled.show()
time.sleep(0.2)
oled.show()
def hibernate():
# Hareket algılanmadığında yapılacak işlemler
oled.fill(0) # Clear the screen
oled.text("Hareket yok!", 0, 0)
oled.show()
# ESP32'yi uyku moduna geçir
def read_distance():
# Trigger pulse
trig_pin.on()
time.sleep_us(10)
trig_pin.off()
# Wait for echo pulse
pulse_time = machine.time_pulse_us(echo_pin, 1, 30000) # 30ms timeout for ~5m range
distance = (pulse_time * 343) / 20000 # Speed of sound is 343 m/s
return distance
# Potansiyometre değeri için orantı
def map_pot_to_threshold(pot_value):
return int((pot_value / 4095) * 400) # Potansiyometre değeri ile distance_threshold arasında orantı
try:
while True:
# Potansiyometre değerini oku
pot_value = machine.ADC(pot_pin).read()
# Güncellenmiş distance_threshold'u al
distance_threshold = map_pot_to_threshold(pot_value)
# MPU6050'den ivme verilerini oku
x, y, z = mpu.get_values()["AcX"], mpu.get_values()["AcY"], mpu.get_values()["AcZ"]
# Hareket algılama
if abs(x) > 0.5 or abs(y) > 0.5 or abs(z) > 0.5:
hareket_algilama()
else:
hibernate()
time.sleep(0.5)
except KeyboardInterrupt:
# Handle KeyboardInterrupt (Ctrl+C) gracefully
print("Program terminated by user.")