// Define the pins for Red, Yellow, Blue LEDs, L298N Motor Driver, Buzzer, Ultrasonic Sensor, and Button
const int redLedPin = 2;
const int yellowLedPin = 3;
const int blueLedPin = 4;
const int motorEnablePin = 11; // L298N Enable pin
const int motorInput2Pin = 5; // L298N Input 2
const int motorInput3Pin = 6; // L298N Input 3
const int motorInput1Pin = 12; // L298N Input 1
const int motorInput4Pin = 13; // L298N Input 4
const int buzzerPin = 7;
const int trigPin = 8;
const int echoPin = 9;
const int startStopSwitchPin = 10;
// Variables for duration, distance, and program state
long duration;
int distance;
bool programRunning = false;
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(motorInput1Pin, OUTPUT);
pinMode(motorInput2Pin, OUTPUT);
pinMode(motorInput3Pin, OUTPUT);
pinMode(motorInput4Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(startStopSwitchPin, INPUT_PULLUP); // Set the button pin as an input with internal pull-up resistor
}
void loop() {
int buttonState = digitalRead(startStopSwitchPin);
if (buttonState == LOW && !programRunning) {
programRunning = true;
startProgram1();
startProgram2();
}
if (buttonState == HIGH) {
stopProgram();
}
// Add any common logic here
delay(1000); // Adjust the delay based on your application
}
void startProgram1() // for pumping water into the container
{
programRunning = true;
// Main program loop
while (programRunning) {
// Read water level distance from ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
// Control LEDs based on water level
// Control LEDs and buzzer based on water level
if (distance <= 15 && distance >= 10) {
digitalWrite(redLedPin, HIGH); // Red LED on
digitalWrite(motorInput1Pin, HIGH);
}
if (distance < 10 && distance >= 5) {
digitalWrite(yellowLedPin, HIGH); // Yellow LED on
}
if (distance < 5 && distance >= 3) {
digitalWrite(blueLedPin, HIGH); // Blue LED on
}
if (distance < 3 && distance >= 0) {
delay(3000);
digitalWrite(motorInput1Pin, LOW);
digitalWrite(buzzerPin, HIGH); // Buzzer on
delay(3000); // Buzzer on for 3 seconds
digitalWrite(buzzerPin, LOW); // Buzzer off
}
delay(1000); // Adjust the delay as needed
}
}
void startProgram2() {
// Add any additional setup steps for starting the program if needed
// Turn on L298N motor driver
digitalWrite(motorEnablePin, HIGH);
// Main program loop
while (programRunning) {
// Read water level distance from ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
// Control LEDs based on water level
// Control LEDs and buzzer based on water level
if (distance <= 2 && distance >= 0) {
digitalWrite(blueLedPin, LOW); // Red LED on
digitalWrite(motorInput4Pin, HIGH);
}
if (distance < 5 && distance >= 3) {
digitalWrite(yellowLedPin, LOW); // Yellow LED on
}
if (distance < 13 && distance >= 6) {
digitalWrite(redLedPin, LOW); // Blue LED on
}
if (distance < 15 && distance >= 13) {
delay(3000);
digitalWrite(motorInput4Pin,LOW);
}
delay(1000); // Adjust the delay as needed
}
}
void stopProgram() {
// Add any cleanup steps for stopping the program if needed
programRunning = false;
// Turn off all components
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(blueLedPin, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(motorEnablePin, LOW);
digitalWrite(motorInput1Pin, LOW);
digitalWrite(motorInput2Pin, LOW);
// Wait for any ongoing actions to complete
delay(5000); // Adjust the delay as needed
}