//[email protected] 2024-09-01
#include <Stepper.h>
#include <EEPROM.h>
/////////////SET THE BELOW PINS FOR YOUR HARDWARE SETUP//////////////
const int increaseButton = 5; //PIN # BUTTON CONNECTED TO
const int decreaseButton = 6; //PIN # BUTTON CONNECTED TO
const int enableButton = 7; //PIN # BUTTON CONNECTED TO
const int buttonA = A4; //PIN # BUTTON CONNECTED TO
const int buttonB = A3; //PIN # BUTTON CONNECTED TO
const int sonoff = A2; //PIN # BUTTON CONNECTED TO
const int redLed = 2; //PIN # LED CONNECTED TO
const int greenLed = 3; //PIN # LED CONNECTED TO
const int enableDriver = 11;
const int disablemotor = HIGH; //SET TO AUTOMATICALLY DISABLE MOTOR TO SAVE POWER
const int stepsPerRevolution = 800; // change this to fit the number of steps per revolution
Stepper myStepper = Stepper(stepsPerRevolution, 9, 10); // Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
//////////////////NO NEED TO CHANGE ANYTHING BELOW///////////////////
long posA = 0; //position A in steps
long posB = 0; //position B in steps
long currentPos = 0; //current position
long rotationAmount = 800; // steps to jump during a move
int buttonAState = HIGH;
int buttonBState = HIGH;
int enabledState = HIGH; // A variable for saving the enabled state
int triggered = LOW; // a trigger for a mode
int alreadymovedtopositiona = LOW;
int alreadymovedtopositionb = LOW;
int alreadyDisabledTheMotor = LOW;
int waitForSonoff = 2000;
void setup() {
myStepper.setSpeed(800);
Serial.begin(9600);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(decreaseButton, INPUT_PULLUP);
pinMode(enableButton, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonA , INPUT_PULLUP);
pinMode(buttonB , INPUT_PULLUP);
pinMode(sonoff , INPUT_PULLUP);
pinMode(enableDriver, OUTPUT);
// GET SAVED EEPROM POSITION A AND POSITION B
EEPROM.get(0, posA); // this value will be big, set the second byte far away
EEPROM.get(50, posB); // use the 10th byte far away from the first "0" byte
//CHECK THE SONOFF FOR ITS STATE AND SET THE CURRENT POSITION ACCORDINGLY
delay (waitForSonoff); //wait a while for the sonoff to remember its last state
int sonoffState = digitalRead(sonoff);
if(sonoffState == 1) {
currentPos = posA;
}
else {
currentPos = posB;
}
}
void loop() {
// READ STATUS OF THE BUTTONS
int increasingState = digitalRead(increaseButton);
int decreasingState = digitalRead(decreaseButton);
int triggered = digitalRead(enableButton);
int buttonAState = digitalRead(buttonA);
int buttonBState = digitalRead(buttonB);
int sonoffState = digitalRead(sonoff);
//RESET EEPROM POSITION A AND POSITION B TO 0
if(buttonAState == LOW && buttonBState == LOW){
EEPROM.put(0, 0);
EEPROM.put(50, 0);
}
//TRIGGER ENABLED STATE PERMANENTLY
if(enabledState == HIGH) {
if(triggered == LOW || posA >= 10 || posA <= -10 || posB >= 10 || posB <= -10){
enabledState = LOW;
}
}
//CHECK IF ENABLED FOR LIGHTS
if(enabledState == LOW){
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
}
if (enabledState == HIGH){
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
}
//CHECK FOR ENABLED STATE BEFORE DOING ANYTHING
if (enabledState == LOW){
//INCREASING BUTTON
if(increasingState == LOW){
digitalWrite(enableDriver,LOW);
delay(10);
myStepper.step(rotationAmount*4);
currentPos = currentPos + rotationAmount;
}
//DECREASING BUTTON
if(decreasingState == LOW){
digitalWrite(enableDriver,LOW);
delay(10);
myStepper.step(-rotationAmount*4);
currentPos = currentPos - rotationAmount;
}
//SAVE POSITION A
if(triggered == LOW && buttonAState == LOW) {
posA = currentPos;
EEPROM.put(0, posA);
Serial.println("Position A Saved to EEPROM");
Serial.println(currentPos);
delay(1000);
}
//MOVE TO SAVED POSITION A
if(triggered == HIGH && sonoffState == HIGH){
digitalWrite(enableDriver,LOW);
if(currentPos - posA > 0){
myStepper.step(-rotationAmount*4);
currentPos = currentPos - rotationAmount;
if (currentPos - posA == 0){
alreadyDisabledTheMotor = LOW;
}
}
if(currentPos - posA < 0){
myStepper.step(rotationAmount*4);
currentPos = currentPos + rotationAmount;
if (currentPos - posA == 0){
alreadyDisabledTheMotor = LOW;
}
}
}
//SAVE POSITION B
if(triggered == LOW && buttonBState == LOW) {
posB = currentPos;
EEPROM.put(50, posB);
Serial.println("Position B Saved to EEPROM");
Serial.println(currentPos);
delay(1000);
}
//MOVE TO SAVED POSITION B
if(triggered == HIGH && sonoffState == LOW){
digitalWrite(enableDriver,LOW);
if (currentPos - posB > 0){
myStepper.step(-rotationAmount*4);
currentPos = currentPos - rotationAmount;
if (currentPos - posB == 0){
alreadyDisabledTheMotor = LOW;
}
}
if (currentPos - posB < 0){
myStepper.step(rotationAmount*4);
currentPos = currentPos + rotationAmount;
if (currentPos - posB == 0){
alreadyDisabledTheMotor = LOW;
}
}
}
//CODE TO DISABLE THE MOTOR FOR POWER SAVING
if (disablemotor == HIGH && alreadyDisabledTheMotor == LOW){
digitalWrite(enableDriver,HIGH);
Serial.println("POWER SAVING - MOTOR DE-ENERGIZED") ;
alreadyDisabledTheMotor = HIGH;
}
} //END OF ENABLE STATE
} //END OF VOID LOOP
INSTRUCTIONS.
1. Slide PROGRAM switch to the right for programming mode.
A
B
PROGRAM MODE
2. Use the MANUAL buttons to move the motor to desired position.
3. press A or B to save the preset.
5. You can now the POSITION toggle switch to move to position A or B.
4. Once presets are saved, slide the PROGRAM switch to the left to enable preset mode.
POSITION A/B SWITCH