#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#include <Servo.h> // Include the Servo library
// Define the number of steps per revolution for your stepper motors
const int stepsPerRevolution = 100;
// Initialize the stepper library on pins 8 through 11 for Motor 1
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// Initialize the stepper library on pins 2 through 5 for Motor 2
Stepper myStepper2(stepsPerRevolution, 2, 3, 4, 5);
// Define I2C LCD address and size
#define LCD_ADDRESS 0x27 // Change if necessary
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// Create an LCD object
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Ultrasonic Sensor Pins
#define TRIG_PIN1 12
#define ECHO_PIN1 13
#define TRIG_PIN2 7
#define ECHO_PIN2 6
// Slide Potentiometer Pin
#define POT_PIN A0
// Servo Motor Pin (using an analog pin as digital)
#define SERVO_PIN A1
// Create a Servo object
Servo valveServo;
// Function to measure distance from ultrasonic sensor
long measureDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
return distance;
}
void setup() {
// Set the speed for the stepper motors
myStepper.setSpeed(60);
myStepper2.setSpeed(60);
// Initialize the serial port
Serial.begin(9600);
// Initialize the LCD
lcd.begin(LCD_COLUMNS, LCD_ROWS); // Initialize with columns and rows
lcd.backlight(); // Turn on the backlight if available
// Initialize ultrasonic sensor pins
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
// Attach the servo to the specified pin
valveServo.attach(SERVO_PIN);
// Set initial position of the servo (e.g., closed position)
valveServo.write(0); // Adjust angle as needed
// Display initial message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Ready");
delay(2000);
}
void loop() {
// Read the value from the slide potentiometer
int potValue = analogRead(POT_PIN);
// Determine the threshold for turning the system on/off
if (potValue > 512) { // Adjust threshold as needed
// System is on
// Measure distances from ultrasonic sensors
long distance1 = measureDistance(TRIG_PIN1, ECHO_PIN1);
long distance2 = measureDistance(TRIG_PIN2, ECHO_PIN2);
if (distance1 < 3) {
// Open the servo valve
valveServo.write(0); // Adjust angle for open position
// Display message for Motor 1
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Filling Grain...");
// Rotate Motor 1
myStepper.step(stepsPerRevolution);
// Display message while Motor 1 is running
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Filling Grain...");
delay(1000); // Adjust delay as needed
}
else{
// Open the servo valve
lcd.print("Valve 1 opening");
valveServo.write(90); // Adjust angle for open position
}
if (distance2 < 3) {
// Display message for Motor 2
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Grain Detected");
// Rotate Motor 2
myStepper2.step(-stepsPerRevolution);
// Display message while Motor 2 is running
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Grain Detected");
delay(1000); // Adjust delay as needed
} else {
// Clear the display and show waiting message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting...");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear second line
// Close the servo valve
valveServo.write(0); // Adjust angle for closed position
}
} else {
// Clear the display and show system off message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Off");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear second line
}
delay(500);
}