// Shetter Version 2.01 ...First Time Convery Movement Without Stepper do not move
// Update Paused/Resume Function Push Button By AI Chat Gpt
#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 RollerMled 2
#define LimitBtn 9
#define StartBtn 3
#define StopBtn 8
#define PauseBtn 5 // New Pause/Resume button
#define LeftSs 4
#define RightSs 3
byte lastLimitBtnState = LOW;
byte lastStartBtnState = LOW;
byte lastStopBtnState = LOW;
byte lastPauseBtnState = LOW; // State for pause/resume button
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;
void setup() {
pinMode(ForLed, OUTPUT);
pinMode(RevLed, OUTPUT);
pinMode(RollerMled, OUTPUT);
pinMode(LimitBtn, INPUT);
pinMode(StartBtn, INPUT);
pinMode(StopBtn, INPUT);
pinMode(PauseBtn, INPUT); // New button input
pinMode(LeftSs, INPUT);
pinMode(RightSs, INPUT);
myStepper.setSpeed(10); // Set motor speed to 10 RPM
Serial.begin(9600);
}
void loop() {
int RollerDistantValue = counter * 20;
// Handle Start Button
if (millis() - lastTimeStartBtnStateChanged > debounceDuration) {
byte startBtnState = digitalRead(StartBtn);
if (startBtnState != lastStartBtnState) {
lastTimeStartBtnStateChanged = millis();
lastStartBtnState = startBtnState;
if (startBtnState == LOW) {
Serial.print("Running for ");
Serial.print(inputValue);
Serial.println(" steps clockwise in segments.");
counter = 0;
conveyorRunning = HIGH;
paused = LOW; // Ensure it starts unpaused
digitalWrite(ForLed, HIGH);
delay(2000);
digitalWrite(ForLed, LOW);
Serial.println("Conveyor Started");
}
}
}
// Handle Stop Button
if (millis() - lastTimeStopBtnStateChanged > debounceDuration) {
byte stopBtnState = digitalRead(StopBtn);
if (stopBtnState != lastStopBtnState) {
lastTimeStopBtnStateChanged = millis();
lastStopBtnState = stopBtnState;
if (stopBtnState == LOW) {
conveyorRunning = LOW;
paused = LOW; // 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;
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 == LOW) {
paused = !paused; // Toggle paused state
if (paused) {
Serial.println("Conveyor Paused");
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
} else {
Serial.println("Conveyor Resumed");
digitalWrite(ForLed,HIGH);
}
}
}
}
// Handle Limit Button
if (conveyorRunning && !paused && millis() - lastTimeLimitBtnStateChanged > debounceDuration) {
byte limitBtnState = digitalRead(LimitBtn);
if (limitBtnState != lastLimitBtnState) {
lastTimeLimitBtnStateChanged = millis();
lastLimitBtnState = limitBtnState;
if (limitBtnState == LOW) {
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(RevLed, LOW);
digitalWrite(ForLed, LOW);
// First Time Move Without Stepper
digitalWrite(ForLed, ledState);
//================================//
int stepsToMove = inputValue; // Total steps to move
while (stepsToMove > 1 && counter >= 1) {
int stepCount = min(stepSegment, stepsToMove); // Get steps for the current segment
myStepper.step(stepCount); // Move the motor clockwise
delay(1000);
digitalWrite(ForLed, ledState);
digitalWrite(RevLed, LOW);
stepsToMove -= stepCount; // Decrease remaining steps
digitalWrite(RevLed, !ledState);
delay(1000);
// 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 >= 10) {
int RollerDistantValue = counter * 20;
Serial.println("Finish & Restart Stepper Roller Distant");
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
myStepper.step(-RollerDistantValue + stepSegment);
counter = 0;
}
}
}
}
}