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
stepperPins = [5, 6, 7, 8] # GPIO pins for stepper motor
servoPin = Pin(20) # GPIO pin for servo motor
switchPin = Pin(22, Pin.IN) # GPIO pin for slide switch
# 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)
def get_light_percentage(pin):
ldr_pin = ADC(Pin(pin))
raw_value = ldr_pin.read_u16()
# Adjust these values based on calibration
max_value = 65535
min_value = 0
scaled_value = (raw_value - max_value) / (min_value - max_value)
light_percentage = scaled_value * 100
# Clamp to 0-100 range and round
return round(max(0, min(light_percentage, 100)), 2)
# 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 (acts as an aquarium lighting system)
def controlNeoPixel(intensityThreshold_low, intensityThreshold_high):
global isNeoOn # Declare isNeoOn as a global variable
intensity = get_light_percentage(photoresistorPin)
print(f"\nLight Percentage of the surrounding: {intensity}%")
if intensity > intensityThreshold_low and intensity < intensityThreshold_high:
print("Adjusting NeoPixel brightness")
brightness = 255 - int(255 * (intensity - intensityThreshold_low) / (intensityThreshold_high - intensityThreshold_low))
print("Brightness:", brightness)
isNeoOn = 1
# Set all NeoPixel LEDs to the adjusted brightness
for i in range(numPixels):
pixels[i] = (brightness, 0, 0) # Red color with adjusted brightness
else:
print("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
# If the water level decreases at a particular threshold distance, stepper motor turns on, increases the water level, and purifies it
# Can replace Ultrasonic sensor with a pH level indicator
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]) # Adjust delay as needed
time.sleep(3)
print("\nStepper motor movement complete.")
else:
print("Water level is sufficient.")
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)
print(".", end="")
time.sleep(0.3) # Small delay between each dot
print("\nServo movement complete.")
# Set the final position
final_duty = map(angle, 0, 180, 1000, 9000)
pwm.duty_u16(final_duty)
print("The fish has been fed.")
# 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(0, 80) # Threshold percentage 80%
if switchPin.value() == 1:
print("\nThe switch is on! Feeding the fish")
# If switch is ON, feed fish
feedFish(90, 20) # Adjust angle as needed
# Control stepper motor based on water level
print("\nChecking water level")
time.sleep(1)
controlStepperMotor(20, 80) # Adjust threshold and steps as needed
# Add delay before next iteration
time.sleep(2) # 3 second delay
except KeyboardInterrupt:
pass