//Include libraries
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <EEPROM.h>
//Set up 16x2 LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
int menuCounter = 0;
//Define variables
int FRONT_Value = 1;
int REAR_Value = 1;
int FSuspValue = 0;
int RSuspValue = 0;
//Defines the number of steps for each click
int SetSteps = 5;
//Define EEPROM direcctions
int FRONT_Value_direcction = 0;
int REAR_Value_direcction = 1;
//Statuses (1/true: selected, 0/false: unselected)
bool FRONT_selected = false;
bool REAR_selected = false;
//Define rotary encoder pins
const int RotaryCLK = 2;
const int RotaryDT = 4;
const int PushButton = 3;
//Define Stepper motors pins
const int stepFL = 6;
const int dirFL = 5;
const int stepFR = 8;
const int dirFR = 7;
const int stepRL = 9;
const int dirRL = 10;
const int stepRR = 11;
const int dirRR = 12;
//Set up Stepper motors
AccelStepper stepperFL(1, stepFL, dirFL);
AccelStepper stepperFR(1, stepFR, dirFR);
AccelStepper stepperRL(1, stepRL, dirRL);
AccelStepper stepperRR(1, stepRR, dirRR);
//Statuses
int CLKNow;
int CLKPrevious;
int DTNow;
int DTPrevious;
bool refreshLCD = true;
bool refreshSelection = false;
float TimeNow1;
float TimeNow2;
void setup()
{
Serial.begin(9600);
pinMode(RotaryCLK, INPUT_PULLUP);
pinMode(RotaryDT, INPUT_PULLUP);
pinMode(PushButton, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.clear();
printLCD();
CLKPrevious = digitalRead(RotaryCLK);
DTPrevious = digitalRead(RotaryDT);
attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE);
attachInterrupt(digitalPinToInterrupt(PushButton), pushButton, FALLING);
stepperFL.setMaxSpeed(1000);
stepperFL.setAcceleration(5000);
stepperFR.setMaxSpeed(1000);
stepperFR.setAcceleration(5000);
stepperRL.setMaxSpeed(1000);
stepperRL.setAcceleration(5000);
stepperRR.setMaxSpeed(1000);
stepperRR.setAcceleration(5000);
EEPROM.get(0, FRONT_Value);
EEPROM.get(10, REAR_Value);
}
void loop(){
if(refreshLCD == true)
{
updateLCD();
if(FRONT_selected == true || REAR_selected == true)
{
}
else
{
refreshLCD = false;
updateCursorPosition();
}
//refreshLCD = false;
}
if(refreshSelection == true)
{
updateSelection();
refreshSelection = false;
}
RunTheMotorFRONT();
RunTheMotorREAR();
EEPROM.put(0, FRONT_Value);
EEPROM.put(10, REAR_Value);
}
void rotate() {
//--------------------------FRONT-----------------------------
if(FRONT_selected == true)
{
CLKNow = digitalRead(RotaryCLK);
if(CLKNow != CLKPrevious && CLKNow == 1)
{
refreshLCD = true;
if(digitalRead(RotaryDT) != CLKNow)
{
if(FRONT_Value < 30)
{
FSuspValue += SetSteps;
FRONT_Value++;
}
else
{
FRONT_Value = 30;
}
}
else
{
if(FRONT_Value <= 1)
{
FRONT_Value = 1;
}
else
{
FSuspValue -= SetSteps;
FRONT_Value--;
}
}
}
else
{
refreshLCD = false;
}
CLKPrevious = CLKNow;
}
//--------------------------REAR-----------------------------
if(REAR_selected == true)
{
CLKNow = digitalRead(RotaryCLK);
if(CLKNow != CLKPrevious && CLKNow == 1)
{
refreshLCD = true;
if(digitalRead(RotaryDT) !=CLKNow)
{
if(REAR_Value < 30)
{
RSuspValue += SetSteps;
REAR_Value++;
}
else
{
REAR_Value = 30;
}
}
else
{
if(REAR_Value <= 1)
{
REAR_Value = 1;
}
else
{
RSuspValue -= SetSteps;
REAR_Value--;
}
}
}
else
{
refreshLCD = false;
}
CLKPrevious = CLKNow;
}
//--------------------------COUNTER-----------------------------
else
{
CLKNow = digitalRead(RotaryCLK);
if (CLKNow !=CLKPrevious && CLKNow == 1)
{
if (digitalRead(RotaryDT) !=CLKNow)
{
//refreshLCD = true;
if(menuCounter < 1)
{
menuCounter++;
}
else
{
menuCounter = 0;
}
}
else
{
if(menuCounter < 1)
{
menuCounter = 1;
}
else {
menuCounter--;
}
}
}
CLKPrevious = CLKNow;
}
refreshLCD = true;
}
void pushButton()
{
TimeNow2 = millis();
if(TimeNow1 - TimeNow2 <1000);
switch(menuCounter)
{
case 0:
FRONT_selected = !FRONT_selected;
break;
case 1:
REAR_selected = !REAR_selected;
break;
}
TimeNow1 = millis();
refreshLCD = true;
refreshSelection = true;
}
void printLCD()
{
lcd.setCursor(5, 0);
lcd.print("FRONT");
lcd.setCursor(5, 1);
lcd.print("REAR");
}
void updateLCD()
{
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(FRONT_Value);
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(REAR_Value);
}
void updateCursorPosition()
{
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(" ");
switch(menuCounter)
{
case 0:
lcd.setCursor (4, 0);
lcd.print(">");
break;
case 1:
lcd.setCursor (4, 1);
lcd.print(">");
break;
}
}
void updateSelection()
{
if(FRONT_selected == true)
{
REAR_selected = false;
lcd.setCursor(4, 0);
lcd.print("X");
}
if(REAR_selected == true)
{
FRONT_selected = false;
lcd.setCursor(4, 1);
lcd.print("X");
}
}
void RunTheMotorFRONT()
{
stepperFL.enableOutputs();
stepperFL.moveTo(FSuspValue);
while(stepperFL.distanceToGo() != 0)
{
stepperFL.runToNewPosition(FSuspValue);
}
stepperFR.enableOutputs();
stepperFR.moveTo(FSuspValue);
while(stepperFR.distanceToGo() != 0)
{
stepperFR.runToNewPosition(FSuspValue);
}
}
void RunTheMotorREAR()
{
stepperRL.enableOutputs();
stepperRL.moveTo(RSuspValue);
while(stepperRL.distanceToGo() != 0)
{
stepperRL.runToNewPosition(RSuspValue);
}
stepperRR.enableOutputs();
stepperRR.moveTo(RSuspValue);
while(stepperRR.distanceToGo() != 0)
{
stepperRR.runToNewPosition(RSuspValue);
}
}