#include <Stepper.h>
#include <Servo.h>
#include <LiquidCrystal.h>
//Define the pin configurations for your components:
// Stepper motor 1 pins
const int stepper1StepPin = 2;
const int stepper1DirPin = 3;
// Stepper motor 2 pins
const int stepper2StepPin = 4;
const int stepper2DirPin = 5;
// Servo motor pins
const int servo1Pin = 6;
const int servo2Pin = 7;
// Proximity sensor pins
const int proxTrigPin = 8;
const int proxEchoPin = 9;
// IR sensor pin
const int irSensorPin = 10;
// LCD pins
const int lcdRS = 11;
const int lcdEnable = 12;
const int lcdD4 = 13;
const int lcdD5 = 14;
const int lcdD6 = 15;
const int lcdD7 = 16;
// Create instances of objects
Stepper stepper1(200, stepper1StepPin, stepper1DirPin);
Stepper stepper2(200, stepper2StepPin, stepper2DirPin);
Servo servo1;
Servo servo2;
LiquidCrystal lcd(lcdRS, lcdEnable, lcdD4, lcdD5, lcdD6, lcdD7);
//In the setup() function, initialize the components:
void setup() {
stepper1.setSpeed(100); // Set the speed of stepper 1
stepper2.setSpeed(100); // Set the speed of stepper 2
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
pinMode(proxTrigPin, OUTPUT);
pinMode(proxEchoPin, INPUT);
pinMode(irSensorPin, INPUT);
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Count: 0");
}
//In the loop() function, perform the following tasks:
//a. Read the proximity sensor and activate servo2 if an object is detected:
void loop(){
digitalWrite(proxTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(proxTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(proxTrigPin, LOW);
long duration = pulseIn(proxEchoPin, HIGH);
int distance = duration / 58; // Convert to centimeters
if (distance < 10) { // Adjust the threshold as needed
servo2.write(90); // Activate servo2
} else {
servo2.write(0); // Deactivate servo2
}
//b. Read the IR sensor and activate servo1 if it detects an object:
int irSensorValue = digitalRead(irSensorPin);
if (irSensorValue == HIGH) {
servo1.write(90); // Activate servo1
} else {
servo1.write(0); // Deactivate servo1
}
//c. Control the stepper motors:
stepper1.step(200); // Move stepper 1 200 steps forward
delay(1000); // Delay for 1 second
stepper2.step(-200); // Move stepper 2 200 steps backward
delay(1000); // Delay for 1 second
//d. Count and display the count on the LCD:
static int count = 0; // Initialize a count variable
count++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(count);
delay (1000);// Update the display every 1 second
}