UpyCraft
This commit is contained in:
61
MicroController/project/workSpace/ldr.py
Normal file
61
MicroController/project/workSpace/ldr.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# main.py
|
||||
# MicroPython script for ESP8266 (using uPyCraft)
|
||||
# Controls an LED based on an LDR sensor reading.
|
||||
|
||||
from machine import Pin, ADC
|
||||
import time
|
||||
|
||||
# --- Configuration ---
|
||||
# LDR Sensor connected to the only ADC pin (A0 on most boards).
|
||||
# The ADC reads values from 0 (Darkest) to 1024 (Brightest) on the ESP8266.
|
||||
LDR_PIN = 0
|
||||
adc = ADC(LDR_PIN)
|
||||
|
||||
# LED connected to a digital pin (D4/GPIO2 is common on NodeMCU).
|
||||
# If you are using the built-in LED, setting Pin(2, Pin.OUT) is correct.
|
||||
# Note: The built-in LED on many ESP8266 boards is ACTIVE-LOW (0=ON, 1=OFF).
|
||||
LED_PIN = 2
|
||||
led = Pin(LED_PIN, Pin.OUT)
|
||||
|
||||
# Threshold: Adjust this value based on your LDR/resistor setup and environment.
|
||||
# Values below this threshold will be considered "dark" (LED ON).
|
||||
LIGHT_THRESHOLD = 500
|
||||
|
||||
print("LDR Sensor and LED control starting...")
|
||||
|
||||
def set_led_state(is_dark):
|
||||
"""Sets the LED state based on whether it is dark or bright."""
|
||||
|
||||
# We use active-low logic for the built-in LED (0=ON, 1=OFF)
|
||||
if is_dark:
|
||||
led.value(0) # Turn LED ON
|
||||
print("-> DARK: LED ON")
|
||||
else:
|
||||
led.value(1) # Turn LED OFF
|
||||
print("-> BRIGHT: LED OFF")
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Read the analog value from the LDR (0-1024)
|
||||
ldr_value = adc.read()
|
||||
|
||||
print(f"LDR Reading: {ldr_value}")
|
||||
|
||||
# Check if the reading is below the set threshold
|
||||
if ldr_value < LIGHT_THRESHOLD:
|
||||
set_led_state(True)
|
||||
else:
|
||||
set_led_state(False)
|
||||
|
||||
# Wait for a short period before reading again
|
||||
time.sleep(1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
# Gracefully handle stop command in the REPL
|
||||
led.value(1) # Turn LED OFF on exit
|
||||
print("\nScript stopped by user.")
|
||||
|
||||
# Clean up is not strictly necessary in MicroPython's simple loop,
|
||||
# but good for habit.
|
||||
finally:
|
||||
pass
|
||||
39
MicroController/project/workSpace/ldr_lamp.py
Normal file
39
MicroController/project/workSpace/ldr_lamp.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from machine import Pin, ADC
|
||||
import time
|
||||
|
||||
ldr = ADC(0)
|
||||
|
||||
# LEDs: dari gelap -> terang (D8..D0)
|
||||
led1 = Pin(15, Pin.OUT) # D8
|
||||
led2 = Pin(13, Pin.OUT) # D7
|
||||
led3 = Pin(12, Pin.OUT) # D6
|
||||
led4 = Pin(14, Pin.OUT) # D5
|
||||
led5 = Pin(16, Pin.OUT) # D0 (merah)
|
||||
|
||||
# Threshold LED (contoh)
|
||||
T1 = 1000
|
||||
T2 = 800
|
||||
T3 = 600
|
||||
T4 = 400
|
||||
T5 = 200
|
||||
|
||||
while True:
|
||||
value = ldr.read() # 0 - 1023
|
||||
print("LDR:", value)
|
||||
|
||||
# reset LED
|
||||
led1.off(); led2.off(); led3.off(); led4.off(); led5.off()
|
||||
|
||||
# Nyalakan LED sesuai threshold (semakin terang -> lebih banyak LED)
|
||||
if value <= T1:
|
||||
led1.on()
|
||||
if value <= T2:
|
||||
led2.on()
|
||||
if value <= T3:
|
||||
led3.on()
|
||||
if value <= T4:
|
||||
led4.on()
|
||||
if value <= T5:
|
||||
led5.on()
|
||||
|
||||
time.sleep_ms(5) # delay sangat responsif (sesuai diskusi)
|
||||
35
MicroController/project/workSpace/ldr_lamp2.py
Normal file
35
MicroController/project/workSpace/ldr_lamp2.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from machine import Pin, ADC
|
||||
import time
|
||||
|
||||
ldr = ADC(0)
|
||||
|
||||
# menggabungkan menjadi 1 list
|
||||
lamp = [
|
||||
machine.Pin(15, machine.Pin.OUT), # led1
|
||||
machine.Pin(13, machine.Pin.OUT), # led2
|
||||
machine.Pin(12, machine.Pin.OUT), # led3
|
||||
machine.Pin(14, machine.Pin.OUT), # led4
|
||||
machine.Pin(16, machine.Pin.OUT) # led5
|
||||
]
|
||||
|
||||
# Threshold LED (contoh)
|
||||
thresholds = [1000, 800, 600, 400, 200]
|
||||
|
||||
while True:
|
||||
value = ldr.read() # 0 - 1024
|
||||
print("LDR:", value)
|
||||
|
||||
# 3. Loop gabungan untuk cek threshold DAN kendalikan LED
|
||||
# zip() akan memasangkan setiap LED dengan threshold-nya
|
||||
# (leds[0] dengan thresholds[0], leds[1] dengan thresholds[1], dst.)
|
||||
|
||||
for led, threshold in zip(lamp, thresholds):
|
||||
# jika value lebih kecil dari trasehold yang dikasih, maka led akan menyala
|
||||
if value <= threshold:
|
||||
led.on()
|
||||
# jika tidak maka akan terjadi sebaliknya
|
||||
else:
|
||||
led.off()
|
||||
|
||||
time.sleep_ms(5)
|
||||
|
||||
34
MicroController/project/workSpace/led3.py
Normal file
34
MicroController/project/workSpace/led3.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import time
|
||||
|
||||
from machine import ADC, Pin
|
||||
|
||||
ldr = ADC(0)
|
||||
|
||||
# 1. Gabungkan semua LED ke dalam satu list
|
||||
lamp = [
|
||||
Pin(15, Pin.OUT), # led1
|
||||
Pin(13, Pin.OUT), # led2
|
||||
Pin(12, Pin.OUT), # led3
|
||||
Pin(14, Pin.OUT), # led4
|
||||
Pin(16, Pin.OUT), # led5
|
||||
]
|
||||
|
||||
# Threshold LED (contoh)
|
||||
thresholds = [1024, 900, 700, 500, 300]
|
||||
|
||||
while True:
|
||||
value = ldr.read() # 0 - 1023
|
||||
print("LDR:", value)
|
||||
|
||||
for i in range(len(lamp)): # mengambil range dari variable lamp
|
||||
# Mengambil led dan threshold menggunakan index 'i'
|
||||
led = lamp[i]
|
||||
threshold = thresholds[i]
|
||||
|
||||
if value < threshold: # jika nilai LDR lebih kecil dari threshold
|
||||
led.on() # led menyala
|
||||
else: # jika nilai led lebih besar dari trasehold
|
||||
led.off() # maka led akan mati
|
||||
|
||||
time.sleep_ms(10)
|
||||
|
||||
Reference in New Issue
Block a user