//Sheeter V_1.02 Standard Code
//Sheeter V_2.00 Update. Wirhout Roller do'not Move in First Time Conveyor Movement
#include <Stepper.h>
const int stepsPerRevolution = 100; // 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 = 100; // Example input value for steps
#define ForLed 7
#define RevLed 6
#define RollerMled 2
#define LimitBtn 9
#define StartBtn 3
#define StopBtn 8
#define LeftSs 4
#define RightSs 3
byte lastLimitBtnState = LOW;
byte lastStartBtnState = LOW;
byte lastStopBtnState = LOW;
byte ledState = LOW;
byte conveyorRunning = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeLimitBtnStateChanged = 0;
unsigned long lastTimeStartBtnStateChanged = 0;
unsigned long lastTimeStopBtnStateChanged = 0;
int counter = 0;
void setup() {
pinMode(ForLed, OUTPUT);
pinMode(RevLed, OUTPUT);
pinMode(RollerMled, OUTPUT);
pinMode(LimitBtn, INPUT);
pinMode(StartBtn, INPUT);
pinMode(StopBtn, 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;
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;
digitalWrite(ForLed, LOW);
digitalWrite(RevLed, LOW);
Serial.println("Conveyor Stopped");
Serial.println("Resetting to original state (step 0) counterclockwise and stopping motor.");
//myStepper.step(-inputValue); // Move the motor back by the input steps (resetting)
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 Limit Button
if (conveyorRunning && 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);
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
digitalWrite(ForLed, ledState);
digitalWrite(RevLed,LOW);
stepsToMove -= stepCount; // Decrease remaining steps
digitalWrite(RevLed, !ledState);
delay(2000);
// 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;
Serial.println("Finish & Restart Stepper Roller Distant");
digitalWrite(ForLed,LOW);
digitalWrite(RevLed,LOW);
myStepper.step(-RollerDistantValue+stepSegment);
counter = 0;
}
} //<<<<
}
}
}