#include <AccelStepper.h>
#include <ezButton.h>
#include <avr/wdt.h>
#define dirPin 5 //Define pin no. for direction
#define stepPin 6 //Define pin no. for stepper motor
#define motorInterface 1 //Define type of stepper motor
#define POWER_BUTTON A0 //Define pin A0 to power button
#define LIMIT_SWITCH_LEFT A1 //Define pin A1 to limit swithc left side
#define LIMIT_SWITCH_RIGHT A2 //Define pin A2 to limit swtich right side
//Define motor direction
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
//Define toggle switch pin no.
const int switchPinRight = 8;
const int resetPin = 2;
//Define ezbutton name
ezButton powerButton(POWER_BUTTON);
ezButton limitSwitch1(LIMIT_SWITCH_LEFT);
ezButton limitSwitch2(LIMIT_SWITCH_RIGHT);
// Create a new instance of the AccelStepper class;
AccelStepper stepper = AccelStepper(motorInterface, stepPin, dirPin);
long maxPosition = 2000; //Define maximum position
//Define the state variables in the main loop
enum Funcstate{HOME_STATE, CLEAN_STATE};
Funcstate presentState = HOME_STATE;
//Define the state variables in home loop
enum ModeState{NOR_STATE, HEV_STATE};
ModeState initialState = NOR_STATE;
//Define the state variables in the motor control function
enum State{STEP_INITIAL, STEP_CHANGE_CCW, STEP_CHANGE_CW, STEP_MOVE, STEP_MOVING, STEP_STOP};
State currentState = STEP_INITIAL;
void setup() {
Serial.begin(9600);
pinMode(resetPin, INPUT_PULLUP);
pinMode(switchPinRight, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_LEFT, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_RIGHT, INPUT_PULLUP);
//Set debounce time to 50 milliseconds
powerButton.setDebounceTime(50);
limitSwitch1.setDebounceTime(20);
limitSwitch2.setDebounceTime(20);
//Set the count mode of button (Falling, Rising, Both)
powerButton.setCountMode(COUNT_FALLING);
limitSwitch1.setCountMode(COUNT_FALLING);
stepper.setMaxSpeed(1000); //Set maximum speed 1000 step/sec
stepper.setAcceleration(50); //Set motor travelling acceleration 50 step/sec2
stepper.setSpeed(100); //Set motor travelling speed 100 step/sec
}
void loop() {
bool sub_rightState = selectSwitchState();
// Check if the switch position has changed
if (sub_rightState == true) {
initialState = NOR_STATE;
} else {
initialState = HEV_STATE;
}
switch (initialState) {
case NOR_STATE:
powerButton.loop();
unsigned long count1 = powerButton.getCount(); //Store number of pressing power button to "count1"
if (count1 >= 1){
switch (presentState) {
case HOME_STATE:
Serial.println("Homing is processing.................");
homeMode();
Serial.println("Homing has Completed");
delay(3000);
presentState = CLEAN_STATE;
break;
case CLEAN_STATE:
motorControl();
break;
}
}
break;
case HEV_STATE:
break;
}
// Check if the reset button is pressed
if (digitalRead(resetPin) == LOW){
delay(50); // Debounce the button
}
// Check button state again
if (digitalRead(resetPin) == LOW){
resetArduino(); // Button pressed, reset the Arduino board
}
}
// Function to reset the Arduino board
void resetArduino() {
wdt_disable(); // Disable the watchdog timer
wdt_enable(WDTO_15MS); // Enable the watchdog timer with a 15ms timeout
while (true); // Wait for the watchdog timer to reset the board
}
//Function for checking mode of selector switch
bool selectSwitchState(){
bool rightState = false;
// Read the states of the switch pins
int switchStateRight = digitalRead(switchPinRight);
if(switchStateRight == LOW){
rightState = true;
}
return rightState;
}