#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int stepPin = 4;
const int dirPin = 5;
const int enablePin = 6;
const int irSensorPin = 7;
const int startButtonPin = A0;
const int stopButtonPin = A1;
const int incButtonPin = A2;
const int decButtonPin = A3;
const int resetButtonPin = 3; // Reset button pin
const int relayPin = 8; // Relay control pin
volatile bool motorRunning = false;
volatile bool relayActive = false; // Relay is initially inactive
unsigned long irSensorTimestamp = 0;
unsigned long resumeMotorTime = 0; // Time to resume motor after IR sensor sensing
const int stepsPerRevolution = 6400;
int irSensorCounter = 0;
int maxIrSensorTriggers = 10;
unsigned long startButtonDebounceTime = 0;
unsigned long stopButtonDebounceTime = 0;
unsigned long incButtonDebounceTime = 0;
unsigned long decButtonDebounceTime = 0;
unsigned long resetButtonDebounceTime = 0;
const int debounceDelay = 50; // Adjust this delay as needed for your buttons
const unsigned long resumeDelay = 10000; // Time delay to resume motor after IR sensor sensing (in milliseconds)
bool objectDetected = false;
// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address and dimensions if needed
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(irSensorPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(incButtonPin, INPUT_PULLUP);
pinMode(decButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT); // Set the relay pin as an output
digitalWrite(enablePin, LOW);
digitalWrite(dirPin, LOW);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(9600);
while (!Serial)
;
lcd.setCursor(0, 0);
lcd.print("TABLET LIMIT:10");
lcd.setCursor(0, 1);
lcd.print("TABLET COUNT:0");
digitalWrite(relayPin, LOW); // Set the relay to its initial state (off)
}
void loop()
{
handleButtonPresses();
handleIrSensor();
handleMotorControl();
}
void handleButtonPresses()
{
if (digitalRead(startButtonPin) == LOW && (millis() - startButtonDebounceTime) > debounceDelay)
{
motorRunning = true;
digitalWrite(enablePin, LOW);
digitalWrite(dirPin, LOW);
startButtonDebounceTime = millis();
Serial.println("Start button pressed");
irSensorCounter = 0; // Reset the counter when motor starts
}
if (digitalRead(stopButtonPin) == LOW && (millis() - stopButtonDebounceTime) > debounceDelay)
{
motorRunning = false;
digitalWrite(enablePin, HIGH);
digitalWrite(relayPin, LOW); // Deactivate the relay
stopButtonDebounceTime = millis();
Serial.println("Stop button pressed");
}
if (digitalRead(incButtonPin) == LOW && (millis() - incButtonDebounceTime) > debounceDelay)
{
maxIrSensorTriggers++;
updateMaxIrSensorDisplay();
incButtonDebounceTime = millis();
delay(200);
}
if (digitalRead(decButtonPin) == LOW && (millis() - decButtonDebounceTime) > debounceDelay)
{
if (maxIrSensorTriggers > 0)
{
maxIrSensorTriggers--;
updateMaxIrSensorDisplay();
}
decButtonDebounceTime = millis();
delay(200);
}
if (digitalRead(resetButtonPin) == LOW && (millis() - resetButtonDebounceTime) > debounceDelay)
{
maxIrSensorTriggers = 10;
updateMaxIrSensorDisplay();
irSensorCounter = 0; // Reset the counter value
updateIrSensorCounterDisplay(); // Update the display
resetButtonDebounceTime = millis();
Serial.println("Reset button pressed");
}
}
void handleIrSensor()
{
if (digitalRead(irSensorPin) == LOW) // Check if an object is detected by the IR sensor
{
irSensorTimestamp = millis(); // Update timestamp when the IR sensor detects something
if (!objectDetected && motorRunning) {
irSensorCounter++; // Increment the counter if object is not already detected and motor is running
updateIrSensorCounterDisplay(); // Update the display
objectDetected = true; // Set flag to true to indicate object detection
}
if (!relayActive && motorRunning)
{
relayActive = true; // Relay is active after IR sensor trigger and motor is running
delay(2000);
digitalWrite(relayPin, HIGH); // Turn on the relay
delay(1000);
}
}
else
{
if (relayActive && motorRunning)
{
relayActive = false; // Relay is not active when the IR sensor is not triggered and motor is running
digitalWrite(relayPin, LOW); // Turn off the relay
}
objectDetected = false; // Reset flag when no object is detected
}
// Stop motor if counter value reaches the maximum limit
if (irSensorCounter >= maxIrSensorTriggers && motorRunning)
{
motorRunning = false;
digitalWrite(enablePin, HIGH);
digitalWrite(relayPin, LOW); // Deactivate the relay
updateIrSensorCounterDisplay(); // Update the display
Serial.println("Motor stopped automatically as counter reached the maximum limit.");
}
}
void handleMotorControl()
{
if (motorRunning)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(400);
digitalWrite(stepPin, LOW);
delayMicroseconds(400);
}
}
void updateMaxIrSensorDisplay()
{
lcd.setCursor(13, 0);
lcd.print(" "); // Clear the previous value
lcd.setCursor(13, 0);
lcd.print(maxIrSensorTriggers);
}
void updateIrSensorCounterDisplay()
{
lcd.setCursor(13, 1);
lcd.print(" "); // Clear the previous value
lcd.setCursor(13, 1);
lcd.print(irSensorCounter);
}