// V_3.04 is update add Ready Led and modified StartBtn Restart Setting
// Update Solonoid Led for Roller gear lock
// V_4.00 Update Find Home Position in Void Setup
#include <Stepper.h>
const int stepsPerRevolution = 200; // Number of steps per revolution for your motor
const int stepSegment = 20; // Segment size (20 steps per segment)
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);
int inputValue = 200; // Example input value for steps
#define ForLed 7
#define RevLed 6
#define SolonoidLed 2
#define LimitBtn 9
#define RollerLimit 4
#define StartBtn 3
#define StopBtn 8
#define PauseBtn 5 // New Pause/Resume button
byte lastLimitBtnState = HIGH; // Set initial state to HIGH for INPUT_PULLUP
byte lastStartBtnState = HIGH; // Set initial state to HIGH for INPUT_PULLUP
byte lastStopBtnState = HIGH; // Set initial state to HIGH for INPUT_PULLUP
byte lastPauseBtnState = HIGH; // Set initial state to HIGH for INPUT_PULLUP
byte ledState = LOW;
byte conveyorRunning = LOW;
byte paused = LOW; // Paused state
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeLimitBtnStateChanged = 0;
unsigned long lastTimeStartBtnStateChanged = 0;
unsigned long lastTimeStopBtnStateChanged = 0;
unsigned long lastTimePauseBtnStateChanged = 0; // Debounce for pause/resume button
int counter = 0;
int FirstTimeWithoutRollerMove = 0;
void setup() {
pinMode(ForLed, OUTPUT);
pinMode(RevLed, OUTPUT);
pinMode(SolonoidLed, OUTPUT);
pinMode(RollerLimit, INPUT_PULLUP);
pinMode(LimitBtn, INPUT_PULLUP);
pinMode(StartBtn, INPUT_PULLUP);
pinMode(StopBtn, INPUT_PULLUP);
pinMode(PauseBtn, INPUT_PULLUP); // New button input
digitalWrite(ForLed,LOW); // Initial State Led Off
digitalWrite(RevLed,LOW); // Initial State Led Off
digitalWrite(SolonoidLed,LOW);
// ===========================================
myStepper.setSpeed(10); // Set motor speed to 10 RPM
Serial.begin(9600);
int stepCount = 0; // Initialize step count
// Find home position at startup
Serial.println("Finding home position...");
while (digitalRead(RollerLimit) == HIGH) {
// Move until the limit switch is touched (LOW)
myStepper.step(-1); // Move step by step in a direction to find home
stepCount--; // Decrease step count for each step taken
delay(10); // Small delay between steps
}
// Stop motor once home position is found
Serial.println("Home position found!");
// If you want to move the motor back to the original position, do this:
Serial.println("Returning to original position...");
myStepper.step(-stepCount); // Move back the same number of steps
Serial.println("Returned to original position.");
//============================================
}
void loop() {
int RollerDistantValue = counter * 20;
// Handle Start Button ( Restart)
if (millis() - lastTimeStartBtnStateChanged > debounceDuration) {
byte startBtnState = digitalRead(StartBtn);
if (startBtnState != lastStartBtnState) {
lastTimeStartBtnStateChanged = millis();
lastStartBtnState = startBtnState;
if (startBtnState == HIGH && counter >= 0) { // Trigger on release
Serial.println("Running for ");
Serial.print(inputValue);
Serial.println(" steps clockwise in segments.");
conveyorRunning = HIGH;
paused = LOW; // Ensure it starts unpaused
digitalWrite(ForLed, HIGH);
digitalWrite(RevLed,LOW);
delay(2000);
digitalWrite(ForLed, LOW);
digitalWrite(SolonoidLed,LOW);
Serial.println("Conveyor Started -----Ready-----");
if(RollerDistantValue > 0 && counter >=1 )
{
digitalWrite(ForLed,LOW);
myStepper.step(-(RollerDistantValue - stepSegment)); // Move the motor back by the input steps (resetting)
counter = 0;
conveyorRunning = HIGH;
paused = LOW; // Reset paused state on stop
ledState = LOW;
digitalWrite(ForLed,HIGH);
delay(2000);
digitalWrite(ForLed,LOW);
digitalWrite(RevLed,LOW);
Serial.println("Resetting to original state (step 0) counterclockwise and stopping motor.");
Serial.println("Conveyor Started -----Ready-----");
digitalWrite(SolonoidLed,LOW);
}
}
}
}
// Handle Stop Button
if (millis() - lastTimeStopBtnStateChanged > debounceDuration) {
byte stopBtnState = digitalRead(StopBtn);
if (stopBtnState != lastStopBtnState) {
lastTimeStopBtnStateChanged = millis();
lastStopBtnState = stopBtnState;
int stepsToMove = inputValue; // Total steps to move
if (stopBtnState == HIGH) { // Trigger on release
conveyorRunning = HIGH;
paused = HIGH; // Reset paused state on stop
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
Serial.println("Conveyor Stopped ----------");
//Serial.println("Resetting to original state (step 0) counterclockwise and stopping motor.");
//myStepper.step(-(RollerDistantValue - stepSegment)); // Move the motor back by the input steps (resetting)
//counter = 0;
int stepCount = min(stepSegment, stepsToMove); // Get steps for the current segment
myStepper.step(stepCount); // Move the motor clockwise
counter++;
Serial.print("Move Down Roller Counter:");
Serial.println(counter);
delay(1000); // Delay to ensure the motor resets before any other action
}
}
}
// Handle Pause/Resume Button
if (conveyorRunning && millis() - lastTimePauseBtnStateChanged > debounceDuration) {
byte pauseBtnState = digitalRead(PauseBtn);
if (pauseBtnState != lastPauseBtnState) {
lastTimePauseBtnStateChanged = millis();
lastPauseBtnState = pauseBtnState;
if (pauseBtnState == HIGH ) { // Trigger on release
paused = !paused; // Toggle paused state
if (paused) {
Serial.println("Conveyor Paused ----------");
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
digitalWrite(SolonoidLed,LOW);
} else {
Serial.println("Conveyor Resumed ---------- ");
digitalWrite(SolonoidLed,LOW);
digitalWrite(ForLed, HIGH);
delay(1000);
digitalWrite(ForLed,LOW);
}
}
}
}
// Handle Limit Button
if (conveyorRunning && !paused && millis() - lastTimeLimitBtnStateChanged > debounceDuration) {
byte limitBtnState = digitalRead(LimitBtn);
if (limitBtnState != lastLimitBtnState) {
lastTimeLimitBtnStateChanged = millis();
lastLimitBtnState = limitBtnState;
if (limitBtnState == HIGH) { // Trigger on release
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(RevLed, LOW);
digitalWrite(ForLed, LOW);
// First Time Move Without Stepper
if (counter == 0)
{
digitalWrite(SolonoidLed, HIGH);
delay(2000);
digitalWrite(ForLed,ledState);
FirstTimeWithoutRollerMove = 20;
}
//================================//
int stepsToMove = inputValue; // Total steps to move
while (stepsToMove >= 1 && counter >= 1) {
int stepCount = min(stepSegment, stepsToMove); // Get steps for the current segment
digitalWrite(SolonoidLed,LOW);
delay(1000);
myStepper.step(stepCount); // Move the motor clockwise
digitalWrite(SolonoidLed,HIGH);
delay(1000);
digitalWrite(RevLed, LOW); // Reverse Led Pre Off delay
delay(1000); // Reverse Led Pre Off delay
digitalWrite(ForLed, ledState);
digitalWrite(RevLed,!ledState);
stepsToMove -= stepCount; // Decrease remaining steps
//digitalWrite(RevLed, !ledState);
Serial.print((ledState == HIGH) ? " Forward:": " Reverse:");
// Break condition when all steps are completed
if (stepsToMove >= 1) {
Serial.print("Movement complete.");
break; // Exit the loop once all steps are completed
}
}
counter++;
Serial.print("Counter:");
Serial.println(counter);
if (counter >= 5) {
int RollerDistantValue = (counter * 20);
digitalWrite(SolonoidLed,LOW);
Serial.println("Finish & Restart Stepper Roller Distant");
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
myStepper.step(-RollerDistantValue + FirstTimeWithoutRollerMove);
counter = 0;
}
}
}
}
}