We are finalizing the total source code of our "Smart pet care system", there are 2 servo motors, 1 Ultrasonic sensor, 1 water level sensor, 1 water pump, and 1 LED...
following is the connection pins on the Arduino Uno,
"0-
1-
2- trigger
3- echo
4- Servo2
5- LED1
6-
7- Water Pump
8- Servo19- RST
10- SDA
11- MOSI
12- MISO
13- SCK"
we have did our code by 4 parts which are working fine individually, following will explain those 4 types of codes,
Code 1: This code is to control the maingate of our system, it uses Servo1 to control the system according to the following code,
"
#include <Servo.h>
#include <MFRC522.h>
#include <SPI.h>
// Pin definitions
#define SS_PIN 10
#define RST_PIN 9
// Create instances for Servo and RFID reader
Servo myServo1;
MFRC522 mfrc522(SS_PIN, RST_PIN);
// RFID UID
byte targetUID[] = {0xA0, 0x84, 0x2C, 0x0E};
void setup() {
// Attach the servo to pin 8
myServo1.attach(8);
// Initialize SPI bus and MFRC522 RFID reader
SPI.begin();
mfrc522.PCD_Init();
// Start with the gate closed
myServo1.write(90); // 90 degrees = closed position
}
void loop() {
// Look for new RFID cards
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// Check if the detected UID matches the target UID
if (checkUID(mfrc522.uid.uidByte)) {
// Open the gate (move servo to 0 degrees)
for (int angle = 90; angle >= 0; angle--) {
myServo1.write(angle);
delay(15); // Adjust delay for smooth movement
}
delay(5000); // Keep the gate open for 5 seconds
// Close the gate (move servo back to 90 degrees)
for (int angle = 0; angle <= 90; angle++) {
myServo1.write(angle);
delay(15); // Adjust delay for smooth movement
}
}
// Halt PICC so it stops reading
mfrc522.PICC_HaltA();
}
}
// Function to check if the UID matches the target UID
bool checkUID(byte *uid) {
for (byte i = 0; i < 4; i++) {
if (uid[i] != targetUID[i]) {
return false;
}
}
return true;
}
"
Code2: this code is for controlling the food dispenser, which will open the door of the hole of the food chamber for 2 seconds for every 1 min by using Servo2. which shows the following code,
"
#include <Servo.h>
// Create servo objects
Servo myServo2; // Servo2 for food dispensing
void setup() {
// Attach the servos to their respective pins
myServo2.attach(4); // Servo2 on pin 4
}
void loop() {
// Control Servo2 (food dispensing)
myServo2.write(0); // Move servo2 to 0 degrees (initial position)
delay(60000); // Wait for 2 seconds
myServo2.write(45); // Move servo2 to 45 degrees
delay(2000); // Wait for 2 seconds
}
"
Code 3: this code is to check the level of the food, my container's length is 27cm(Vertically). if the empty space exceeds 20cms the LED will blink, which shows the following code,
"
const int trigPin = 2; // Trig pin connected to pin 2
const int echoPin = 3; // Echo pin connected to pin 3
const int ledPin = 5; // LED connected to pin 5
long duration;
int distance;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set the trigPin as an output and the echoPin as an input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Set the ledPin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, which returns the duration of the pulse in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2; // Speed of sound is 343 m/s or 0.034 cm/µs
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the distance is 20 cm or less
if (distance >= 20) {
// Blink the LED
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(250); // Wait for 250 milliseconds
digitalWrite(ledPin, LOW); // Turn off the LED
delay(250); // Wait for 250 milliseconds
} else {
// Ensure the LED is off when the distance is greater than 20 cm
digitalWrite(ledPin, LOW);
}
// Wait before the next measurement
delay(500);
}
"
Code 4: This is for managing the water level using the water level sensor. the following code shows that,
"
// Constants for the pins used
const int analogInPin = A0; // Analog input pin for the water level sensor
const int pumpPin = 7; // Digital output pin for the water pump
int sensorValue = 0; // Value read from the water level sensor
void setup() {
// Initialize serial communication at 9600 bps
Serial.begin(9600);
// Set the pumpPin as an output
pinMode(pumpPin, OUTPUT);
}
void loop() {
// Read the analog value from the water level sensor
sensorValue = analogRead(analogInPin);
// Print the sensor value to the Serial Monitor
Serial.print("Water level sensor value = ");
Serial.println(sensorValue);
// Check if the sensor value is less than 280
if (sensorValue < 280) {
// Turn on the water pump
digitalWrite(pumpPin, HIGH);
// Keep the pump on for 5 seconds
delay(5000);
// Turn off the water pump
digitalWrite(pumpPin, LOW);
}
// Wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading
delay(2);
}
"
Finally, I want to combine the whole code into one Arduino code for to work the whole system. without changing any meaning of the codes... I have tried some ways, but they didn't resolve my problem.. please help me to find the solution