// 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
// V_4.02 Update Manual Roller Step to Move Down & Restart Position (0)
const int stepPin = 11;
const int dirPin = 10;
const int enPin = 12;
const int stepsSegment = 200; // Segment size (100 steps per segment)
const int totalStepsLimit = 1000; // Total steps before returning to step 0
int stepCounter = 0; // Counter for total steps
// ========
#define ForLed 7
#define RevLed 6
#define SolonoidLed 2
#define LimitBtn 9
#define RollerLimit 4
#define StartBtn 3
#define MoveDownBtn 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 lastMoveDownBtnState = 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 lastTimeMoveDownBtnStateChanged = 0;
unsigned long lastTimePauseBtnStateChanged = 0; // Debounce for pause/resume button
int counter = 0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
//digitalWrite(enPin, LOW); // Enable the driver (assuming active low)
pinMode(ForLed, OUTPUT);
pinMode(RevLed, OUTPUT);
pinMode(SolonoidLed, OUTPUT);
pinMode(RollerLimit, INPUT_PULLUP);
pinMode(LimitBtn, INPUT_PULLUP);
pinMode(StartBtn, INPUT_PULLUP);
pinMode(MoveDownBtn, 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);
// ===========================================
Serial.begin(9600);
int stepCounter = 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)
digitalWrite(dirPin, HIGH); // Set direction to forward
moveMotor(+1);// Move motor in segments of 100 steps
stepCounter ++ ; // Decreased step counte
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...");
digitalWrite(dirPin, LOW); // Set direction to reverse
moveMotor(+stepCounter); // Move back the total steps
stepCounter = 0; // Reset the step counter to 0
Serial.println("Returned to original position step 0.");
//============================================
}
void loop() {
int RollerDistantValue = (counter * stepsSegment)-stepsSegment;
int inputValue = totalStepsLimit; // Example input value for
// 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(stepCounter > 0 && counter >=1 )
{
digitalWrite(ForLed,LOW);
digitalWrite(dirPin, LOW); // Set direction to reverse
moveMotor(stepCounter); // Move back the total steps
stepCounter = 0; // Reset the step counter to 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);
}
}
}
}
// Manual Roller Move Down By Step Segment
if (millis() - lastTimeMoveDownBtnStateChanged > debounceDuration) {
byte MoveDownBtnState = digitalRead(MoveDownBtn);
if (MoveDownBtnState != lastMoveDownBtnState) {
lastTimeMoveDownBtnStateChanged = millis();
lastMoveDownBtnState = MoveDownBtnState;
if (MoveDownBtnState == HIGH ) { // Trigger on release
conveyorRunning = HIGH;
paused = HIGH; // Reset paused state on stop
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
Serial.print("Manual Roller Step To MoveDown---");
if(counter >= 1 )
{
digitalWrite(dirPin, HIGH); // Set direction to forward
moveMotor(stepsSegment); // Move motor in segments of 100 steps
stepCounter += stepsSegment; // Update step counter
Serial.print("TotalSteps:");
Serial.print(stepCounter-200);
counter++;
Serial.print(" Counter:");
Serial.println(counter);
}
if(counter == 0 )
{
counter ++;
Serial.print(" 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);
}
//================================//
int stepsToMove = inputValue; // Total steps to move
while (stepsToMove >= 1 && counter >= 1) {
digitalWrite(dirPin, HIGH); // Set direction to forward
moveMotor(stepsSegment); // Move motor in segments of 100 steps
stepCounter += stepsSegment; // Update step counter
Serial.print("TotalSteps:");
Serial.println(stepCounter);
digitalWrite(SolonoidLed,LOW);
delay(1000);
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);
//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.print("Finish & Restart Stepper Roller Distant");
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
digitalWrite(dirPin, LOW); // Set direction to reverse
moveMotor(totalStepsLimit-stepsSegment); // Move back the total steps
stepCounter = 0; // Reset the step counter to 0
counter = 0;
}
}
}
}
}
// Function to move motor in specified segment size
void moveMotor(int steps) {
for (int x = 0; x < steps; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
}
Restart