@gfvalvo sorry one last question. Is there a simple way to store the last encoder value from each settingIndex so that when returned back to that settingIndex again it returns to the same encoder value instead of the starting position? I was attempting to do that with this code.
#include "Arduino.h"
#include "NewEncoder.h"
int16_t mainMenuValue; //integer to store speed value
int16_t speedValue; //integer to store speed value
int16_t feedValue; //integer to store speed value
int16_t spinValue; //integer to store speed value
int16_t oscillationValue; //integer to store speed value
int16_t oscillationRangeValue; //integer to store speed value
int16_t oscillationSpeedValue; //integer to store speed value
int16_t drillsValue; //integer to store speed value
// Define a struct to store encoder settings (upper and lower limits and starting position)
struct EncoderSettings {
int16_t low; // lower limit of encoder values
int16_t high; // upper limit of encoder values
int16_t start; // starting position of encoder
};
// Define an index to keep track of current encoder setting
uint16_t settingIndex = 0;
// Define variable for encoder value
int16_t currentValue;
// Define an array of encoder settings
const EncoderSettings settings[] = {
{ 1, 7, mainMenuValue }, // Setting 0: Main Menu lower limit 1, upper limit 7, starting position 1
{ 1, 50, speedValue }, // Setting 1: Speed Menu lower limit 1, upper limit 50, starting position 10
{ 0, 30, feedValue }, // Setting 2: Feed Menu lower limit 0, upper limit 30, starting position 3
{ -20, 20, spinValue }, // Setting 3: Spin Menu lower limit -20, upper limit 20, starting position 0
{ 0, 1, oscillationValue }, // Setting 4: Oscillation Menu lower limit 0, upper limit 1, starting position 0
{ 1, 20, oscillationRangeValue }, // Setting 5: Oscillation Range Menu lower limit 1, upper limit 20, starting position 1
{ 1, 15, oscillationSpeedValue }, // Setting 6: Oscillation Speed Menu lower limit 1, upper limit 15, starting postion 3
{ 0, 1, drillsValue } // Setting 7: Drills lower limit 0, upper limit 1, starting position 0
};
//Create a NewEncoder object with pin assignments and initial settings from the settings array
NewEncoder encoder(5, 18, settings[settingIndex].low, settings[settingIndex].high, settings[settingIndex].start, FULL_PULSE);
int16_t prevEncoderValue; //Initialize a variable to store the previous encoder value
const uint8_t button = 19; //Define the button pin and initialize time variables
uint8_t debounceThreshold = 20; //Set the debounce threshold for the button
bool mainMenuSelected = true; // initialize that main menu is selected
bool speedSelected = false; // initialize that speed menu is not selected
bool feedSelected = false; // initialize that feed menu is not selected
bool spinSelected = false; // initialize that spin menu is not selected
bool oscillationSelected = false; // initialize that oscillation menu is not selected.
bool oscillationRangeSelected = false; // initialize that oscillation range menu is not selected
bool oscillationSpeedSelected = false; // initialize that oscillation speed menu is not selected
bool drillsSelected = false; // initialize that drills menu is not selected
bool subMenu = false;
void subMenuSelected() {
mainMenuSelected = false;
subMenu = true;
switch (currentValue) {
case 1:
speedSelected = true;
mainMenuSelected = false;
feedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("Speed is selected");
break;
case 2:
feedSelected = true;
mainMenuSelected = false;
speedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("Feed is selected");
break;
case 3:
spinSelected = true;
mainMenuSelected = false;
speedSelected = false;
feedSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("Spin is selected");
break;
case 4:
oscillationSelected = true;
mainMenuSelected = false;
speedSelected = false;
feedSelected = false;
spinSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("Oscillation is selected");
break;
case 5:
oscillationRangeSelected = true;
mainMenuSelected = false;
speedSelected = false;
feedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("Oscillation Range is selected");
break;
case 6:
oscillationSpeedSelected = true;
mainMenuSelected = false;
speedSelected = false;
feedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
drillsSelected = false;
Serial.println("Oscillation Speed is selected");
break;
case 7:
drillsSelected = true;
mainMenuSelected = false;
speedSelected = false;
feedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
Serial.println("Drills is selected");
break;
}
}
void deSelectSubMenus() { // unSelects all menus
subMenu = false;
mainMenuSelected = true;
speedSelected = false;
feedSelected = false;
spinSelected = false;
oscillationSelected = false;
oscillationRangeSelected = false;
oscillationSpeedSelected = false;
drillsSelected = false;
Serial.println("All Menus Unselected");
}
void setMachineValues() {
if (mainMenuValue == true) {
mainMenuValue = currentValue;
Serial.print("Main Menu Value is ");
Serial.println(currentValue);
}
if (speedSelected == true) {
speedValue = currentValue;
Serial.print("Speed Value is ");
Serial.println(speedValue);
}
if (feedSelected == true) {
feedValue = currentValue;
Serial.print("Feed Value is ");
Serial.println(feedValue);
}
if (spinSelected == true) {
spinValue = currentValue;
Serial.print("Spin Value is ");
Serial.println(spinValue);
}
if (oscillationSelected == true) {
oscillationValue = currentValue;
Serial.print("Oscillation Value is ");
Serial.println(oscillationValue);
}
if (oscillationRangeSelected == true) {
oscillationRangeValue = currentValue;
Serial.print("Oscillation Range is ");
Serial.println(oscillationRangeValue);
}
if (oscillationSpeedSelected == true) {
oscillationSpeedValue = currentValue;
Serial.print("Oscillation Speed is ");
Serial.println(oscillationSpeedValue);
}
if (drillsSelected == true) {
drillsValue = currentValue;
Serial.print("Drills value is ");
Serial.println(drillsValue);
}
}
void storedValues() {
if (mainMenuSelected == true) {
currentValue = mainMenuValue;
}
if (speedSelected == true) {
currentValue = speedValue;
}
if (feedSelected == true) {
currentValue = feedValue;
}
if (spinSelected == true) {
currentValue = spinValue;
}
if (oscillationSelected == true) {
currentValue = oscillationValue;
}
if (oscillationRangeSelected == true) {
currentValue = oscillationRangeValue;
}
if (oscillationSpeedSelected == true) {
currentValue = oscillationSpeedValue;
}
if (drillsSelected == true) {
currentValue = drillsValue;
}
}
void setup() {
NewEncoder::EncoderState state; //Create a NewEncoder::EncoderState object to hold the state of the encoder
Serial.begin(115200); //Start serial communication
delay(2000); //Delay for 2 seconds
Serial.println("Starting");
//If the encoder fails to start, print an error message to the serial monitor and enter an infinite loop
if (!encoder.begin()) {
Serial.println("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting.");
while (1) {
yield();
}
}
//Otherwise, get the current state of the encoder and print the starting value to the serial monitor
else {
encoder.getState(state);
Serial.print("Encoder Successfully Started at value = ");
prevEncoderValue = state.currentValue;
Serial.println(prevEncoderValue);
}
pinMode(button, INPUT_PULLUP); //Set the button pin as an input with internal pull-up resistor
speedValue = 10; //initializes speedValue at 10;
feedValue = 3; //initializes feedValue at 3;
spinValue = 0; //initializes spinValue at 0;
oscillationValue = 0; //initializes oscillationValue at 0;
oscillationRangeValue = 1; //initializes oscillationRangeValue at 1;
oscillationSpeedValue = 3; //initializes oscillationSpeedValue at 1;
drillsValue = 0; //initializes drillsValue at 0;
mainMenuValue = 1; //initializes mainMenuValue at 1;
}
void loop() {
//Declare variables for current encoder value and encoder state
NewEncoder::EncoderState currentEncoderState;
uint32_t TimeSinceLastButtonReading;
int buttonReading;
uint32_t currentMillis = millis();
static uint32_t TimeOfLastButtonReading = currentMillis;
static int buttonState = digitalRead(button);
//Calculate the time since the last button reading
TimeSinceLastButtonReading = currentMillis - TimeOfLastButtonReading; //If enough time has passed for debounce, read the button state
if (TimeSinceLastButtonReading > debounceThreshold) {
buttonReading = digitalRead(button);
if (buttonReading != buttonState) { //If the button state has changed, update the button state variable
buttonState = buttonReading;
if (buttonState == LOW) { //If the button is pressed, toggle through the different encoder settings
encoder.getState(currentEncoderState); //Get the current state of the encoder and set the setting index based on the current value
currentValue = currentEncoderState.currentValue;
if ((currentValue >= 0) && (currentValue < 7) && (subMenu == false)) {
subMenuSelected();
settingIndex = currentValue;
} else {
settingIndex = 0;
deSelectSubMenus();
}
encoder.newSettings(settings[settingIndex].low, settings[settingIndex].high, settings[settingIndex].start, currentEncoderState);
currentValue = currentEncoderState.currentValue;
storedValues();
Serial.print("Encoder Settings Toggled to Index ");
Serial.println(settingIndex);
Serial.print("Updated Encoder Value: ");
Serial.println(currentValue);
prevEncoderValue = currentValue;
}
TimeOfLastButtonReading = currentMillis;
}
}
if (encoder.getState(currentEncoderState)) {
currentValue = currentEncoderState.currentValue;
if (currentValue != prevEncoderValue) {
setMachineValues();
Serial.println(currentValue);
prevEncoderValue = currentValue;
} else {
switch (currentEncoderState.currentClick) {
case NewEncoder::UpClick:
Serial.println("at upper limit.");
break;
case NewEncoder::DownClick:
Serial.println("at lower limit.");
break;
default:
break;
}
}
}
}