#include <Adafruit_NeoPixel.h>
#include <Servo.h>
// Define pin numbers
#define PHOTORESISTOR_PIN A0
#define NEOPIXEL_PIN 21
#define NUM_PIXELS 12
#define TRIGGER_PIN 18
#define ECHO_PIN 19
#define SERVO_PIN 20
#define SWITCH_PIN 22
#define STEPS_TO_MOVE 22
// Define stepper motor pins
const int stepperPins[] = {5, 6, 7, 8};
#define STEPS_TO_MOVE 80
// Create NeoPixel object
Adafruit_NeoPixel pixels(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Create Servo object
Servo fishServo;
// Function to map a value from one range to another
int mapValue(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// Function to get light percentage from photoresistor
float getLightPercentage(int pin) {
int raw_value = analogRead(pin);
// Adjust these values based on calibration
int max_value = 1023;
int min_value = 0;
float scaled_value = (float)(raw_value - max_value) / (min_value - max_value);
float light_percentage = scaled_value * 100;
// Clamp to 0-100 range and round
return max(0.0, min(light_percentage, 100.0));
}
// Function to control NeoPixel LED ring based on light intensity
void controlNeoPixel(float intensityThreshold_low, float intensityThreshold_high) {
float intensity = getLightPercentage(PHOTORESISTOR_PIN);
Serial.print("Light Percentage of the surrounding: ");
Serial.print(intensity);
Serial.println("%");
if (intensity > intensityThreshold_low && intensity < intensityThreshold_high) {
Serial.println("Adjusting NeoPixel brightness");
int brightness = 255 - (255 * (intensity - intensityThreshold_low) / (intensityThreshold_high - intensityThreshold_low));
Serial.print("Brightness: ");
Serial.println(brightness);
// Set all NeoPixel LEDs to the adjusted brightness
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(brightness, 0, 0)); // Red color with adjusted brightness
}
} else {
Serial.println("NeoPixel LEDs off");
// Turn off all NeoPixel LEDs
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Turn off
}
}
// Update NeoPixel LEDs
pixels.show();
}
// Function to measure distance using ultrasonic sensor
long measureDistance() {
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Convert the duration to distance (in cm)
long distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
return distance;
}
// Function to control stepper motor based on water level
void controlStepperMotor(int distanceThreshold, int stepsToMove) {
long distance = measureDistance();
if (distance > distanceThreshold) {
Serial.println("Water level is low. Starting stepper motor movement.");
int steps[4][4] = {
{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 (int i = 0; i < stepsToMove; i++) {
for (int step = 0; step < 4; step++) {
for (int pin = 0; pin < 4; pin++) {
digitalWrite(stepperPins[pin], steps[step][pin]);
}
delay(5); // Adjust delay as needed
}
}
Serial.println("Stepper motor movement complete.");
} else {
Serial.println("Water level is sufficient.");
// Turn off stepper motor
for (int pin = 0; pin < 4; pin++) {
digitalWrite(stepperPins[pin], LOW);
}
}
}
// Function to feed fish using servo motor
void feedFish(int angle) {
Serial.println("Feeding the fish");
// Smooth movement from 0 to the desired angle
for (int pos = 0; pos <= angle; pos += 1) {
fishServo.write(pos);
delay(15); // Adjust delay as needed
}
delay(1000); // Wait for the servo to reach the desired position
// Return to the initial position
for (int pos = angle; pos >= 0; pos -= 1) {
fishServo.write(pos);
delay(15); // Adjust delay as needed
}
Serial.println("Fish has been fed.");
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize NeoPixel
pixels.begin();
// Set up pin modes
pinMode(PHOTORESISTOR_PIN, INPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
for (int i = 0; i < 4; i++) {
pinMode(stepperPins[i], OUTPUT);
}
pinMode(SERVO_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
// Attach the servo
fishServo.attach(SERVO_PIN);
}
void loop() {
// Read light intensity and control NeoPixel LED ring
controlNeoPixel(0, 80); // Threshold percentage 80%
// Check the switch status to feed the fish
if (digitalRead(SWITCH_PIN) == HIGH) {
feedFish(90); // Adjust angle as needed
}
// Control stepper motor based on water level
controlStepperMotor(20, STEPS_TO_MOVE); // Adjust threshold and steps as needed
// Add delay before next iteration
delay(2000); // 2 second delay
}