// For Discord channel
// Sketch by G6six
#include <SevSeg.h>
#include <AccelStepper.h>
#include <Arduino.h>
//defining the analog input pins
#define rotaryA A0 // Changed
#define rotaryB A1 // Changed
#define rotaryButton A2 // Changed
#define stepPin A5 // Changed
#define dirPin A4 // Changed
SevSeg sevseg;
AccelStepper stepper(1, stepPin, dirPin);
//sevseg required inputs
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
bool leadingZeros = false;
bool disableDecPoint = false;
//variables
int cableLength = 0;
int currentState;
int aLastState;
int rotaryButtonState;
//function that uses the rotary encoder ouput to modify the variable cableLength
int rotaryEncoder () {
currentState = digitalRead(rotaryA); // Reads the "current" state of the outputA
if (currentState != aLastState){
if (digitalRead(rotaryB) != currentState) {
cableLength = cableLength + 5; //+ and - 5 bcs the length of the cable is going to be selected by half a meter
} else {
cableLength = cableLength - 5;
}
Serial.println(cableLength); //debuggging purpose,
}
aLastState = currentState; // Updates the previous state of the outputA with the current state
return cableLength;
}
void stepperExecute(){
while (stepper.distanceToGo() != 0){
Serial.println("stepperExecute called"); //debugging purpose
stepper.run();
}
}
void setup() {
Serial.begin(9600); //debugging purpose
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(20);
pinMode(rotaryButton, INPUT_PULLUP); // push button is an input pull up (High when not presses)
stepper.setMaxSpeed(800);
stepper.setAcceleration(200);
}
void loop(){
rotaryEncoder();
sevseg.setNumber(cableLength, 1);
sevseg.refreshDisplay();
int rotaryButtonState = digitalRead(rotaryButton);
if (rotaryButtonState == LOW){
delay(1000);
stepper.move(cableLength);
Serial.println("target position changed");
}
stepperExecute();
}