import neopixel
from machine import Pin, ADC, PWM
import utime as time
# Define pin numbers
photoresistorPin = Pin(28)
adc = ADC(photoresistorPin) # Analog pin for photoresistor
neopixelPin = Pin(21, Pin.OUT) # GPIO pin for NeoPixel LED ring
numPixels = 12 # Number of pixels in the LED ring
triggerPin = Pin(18, Pin.OUT) # GPIO pin for ultrasonic sensor trigger
echoPin = Pin(19, Pin.IN) # GPIO pin for ultrasonic sensor echo
servoPin = Pin(20) # GPIO pin for servo motor
stepperPins = [5, 6, 7, 8] # GPIO pins for stepper motor
switchPin = Pin(22, Pin.IN) # GPIO pin for slide switch
# Define NeoPixel parameters
brightness = 50 # NeoPixel brightness (0-255)
# Create NeoPixel object
pixels = neopixel.NeoPixel(neopixelPin, numPixels)
def map(x, in_min, in_max, out_min, out_max):
return int((x-in_min)*(out_max-out_min)/ (in_max-in_min)+out_min)
# Function to read light intensity from photoresistor
def readLightIntensity():
# Read analog value from the photoresistor
value = adc.read_u16()
# Convert analog value to light intensity
intensity = value / 65535.0 * 100 # Scale to percentage (0-100)
return intensity
# Define a boolean variable to indicate the status of NeoPixel LEDs
isNeoOn=0
# Function to control NeoPixel LED ring based on light intensity
# If night occurs, Neopixel turns on
def controlNeoPixel(intensityThreshold):
global isNeoOn # Declare isNeoOn as a global variable
intensity = readLightIntensity()
if intensity > intensityThreshold:
print("Turning NeoPixel LEDs on")
isNeoOn = 1
# Set all NeoPixel LEDs to red or any other color you prefer
for i in range(numPixels):
pixels[i] = (255, 0, 0) # Red color
else:
print("Turning NeoPixel LEDs off")
isNeoOn = 0
# Turn off all NeoPixel LEDs
for i in range(numPixels):
pixels[i] = (0, 0, 0) # Turn off
# Update NeoPixel LEDs
pixels.write()
# Function to measure distance using ultrasonic sensor
def measureDistance():
# Send a pulse to trigger the ultrasonic sensor
triggerPin.value(1)
time.sleep_us(10)
triggerPin.value(0)
# Measure the duration of the echo pulse
while echoPin.value() == 0:
pulse_start = time.ticks_us()
while echoPin.value() == 1:
pulse_end = time.ticks_us()
pulse_duration = time.ticks_diff(pulse_end, pulse_start)
# Convert the duration to distance (in cm)
distance = pulse_duration * 17150 // 1000000
print("Distance:", distance, "cm")
return distance
isMotorRunning = 0 # Initialize the status of the stepper motor
# Function to control stepper motor based on water level
def controlStepperMotor(distanceThreshold, stepsToMove):
global isMotorRunning # Declare isMotorRunning as a global variable
distance = measureDistance()
if distance < distanceThreshold:
print("Water level is low. Starting stepper motor movement.")
isMotorRunning = 1 # Update motor status
steps = [
[1, 0, 0, 1], # Step 1
[0, 1, 0, 1], # Step 2
[0, 1, 1, 0], # Step 3
[1, 0, 1, 0] # Step 4
]
# Rotate the stepper motor in one direction (increase water level)
for i in range(stepsToMove):
for step in steps:
for j, pin in enumerate(stepperPins):
Pin(pin, Pin.OUT).value(step[j])
time.sleep_ms(1) # Adjust delay as needed
else:
print("Water level is sufficient. Stopping stepper motor.")
isMotorRunning = 0 # Update motor status
# Turn off stepper motor
for pin in stepperPins:
Pin(pin, Pin.OUT).value(0)
# Function to feed fish using servo motor
def feedFish(angle, servo_pin):
# Control servo motor to feed fish
pwm = PWM(Pin(servo_pin))
pwm.freq(50) # Set PWM frequency to 50 Hz
# Smooth movement from 0 to the desired angle
for i in range(angle,0,-(int(angle/5))):
duty = map(i, 0, 180, 1000, 9000) # Convert angle to duty cycle range
pwm.duty_u16(duty)
time.sleep(0.5)
# Set the final position
final_duty = map(angle, 0, 180, 1000, 9000)
pwm.duty_u16(final_duty)
# Wait for the servo to reach the desired position
time.sleep(1) # Adjust delay as needed
# Main loop
try:
while True:
# Read light intensity and control NeoPixel LED ring
controlNeoPixel(20) # Adjust threshold as needed
# Check slide switch state to control feeding
if switchPin.value() == 1:
# If switch is ON, feed fish
feedFish(90,20) # Adjust angle as needed
# Control stepper motor based on water level
controlStepperMotor(100, 100) # Adjust threshold and steps as needed
# Add delay before next iteration
time.sleep(1) # 1 second delay
except KeyboardInterrupt:
pass