#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the input pin for the item counting sensor
const int sensorPin = 6; // Change to your sensor pin
const int maxItemCount = 5; // The number of items to count before stopping the motor
const int motorSpeed = 1000; // Adjust this to your motor's speed
const int motorDirection = 0; // Change direction if needed
boolean motorStopped = false;
AccelStepper stepper;
int itemCount = 0;
void setup() {
stepper.setMaxSpeed(-motorSpeed);
stepper.setSpeed(-motorSpeed);
pinMode(sensorPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("INICIANDO");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("CONTADOR");
delay(1500);
lcd.clear();
delay(300);
lcd.setCursor(0, 0);
lcd.print("CUENTA OBJETOS");
lcd.setCursor(0, 1);
lcd.print("CANTIDAD: ");
stepper.setAcceleration(2000); // Adjust acceleration if needed
stepper.runSpeed();
}
void loop() {
int sensorValue = digitalRead(sensorPin);
stepper.runSpeed();
if (sensorValue != 1) {
itemCount++;
while (digitalRead(sensorPin) != 1) {
delay(80);
}
lcd.setCursor(12, 1);
lcd.print(itemCount);
stepper.runSpeed();
}
if (itemCount >= maxItemCount) {
stepper.stop();
delay(10000);
itemCount = 0;
lcd.setCursor(12, 1);
lcd.print(itemCount);
stepper.runSpeed();
}
}