Mode change buttons with enum

MorryStu

Member
Hi All

I am working on a lathe controller project.

What I need is for 3 buttons to be able to switch into 3 different modes
1 MM - Metric Thread pitch in mm
2 TPI - Imperial thread pitch in Teeth Per Inch
3 FEED - feed rate for cutting

I have momentary push switches switches with LED' indication that lights up when button pushed ( indication is derived from the Teensy ). There are further buttons for FWD, REV, STOP, which will also indicate when pushed, but are used for functions with "MM" "TPI" and "FEED" modes

I have previously got mode changes to work with a single button ( with display working with RotEnc ), but now I seem to be going in circles and need some help

Code:
/*
MM Pin0 = MMButt
   Pin1 = MMLed
TPI Pin2 = TPIButt
    Pin3 = TPILed
Feed Pin4 = FeedButt
     Pin5 = FeedLED
STOP Pin6 = STOPButt
     Pin7 = STOPLed
FWD Pin8 = FWDButt
    Pin9 = FWDLED
REV Pin10 = REVButt
    Pin11 = REVLED
DISP1 Pin16 = DISP1Clk
      Pin17 = DISP1Dat
DISP2 Pin18 = DISP2Clk
      Pin19 = DISP2Dat
ENC Pin20 = ENCClk
    PIN21 = ENCDat
SPINDLE Pin22 = SPINDLE_A
        Pin23 = SPINDLE_b
        */
#include <RotaryEncoder.h>  //Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h https://github.com/ownprox/Rotary_Encoder_KY-040_Fixed.....NOTE. this installs as RotaryEncoder.h, so make sure to remove any existing library
#include <Bounce2.h>
#include <SPI.h>
#include <Wire.h>
#include <TM1637Display.h>

#define MMButt_PIN 0  // WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define MMLed_PIN 1   // DEFINE THE PIN FOR THE LED :
#define TPIButt_PIN 2
#define TPILed_PIN 3
#define FEEDButt_PIN 4
#define FEEDLed_PIN 5
#define STOPButt_PIN 6
#define STOPLed_PIN 7
#define FWDButt_PIN 8
#define FWDLed_PIN 9
#define REVButt_PIN 10
#define REVLed_PIN 11
#define CLK1 16  //Display 1 Clk
#define DIO1 17  // Display 1 data
#define CLK2 18  // Disaply 2 Clk
#define DIO2 19  // Display 2 data

TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);

Bounce MMdebouncer = Bounce();
Bounce TPIdebouncer = Bounce();
Bounce FEEDdebouncer = Bounce();
Bounce STOPdebouncer = Bounce();
Bounce FWDdebouncer = Bounce();
Bounce REVdebouncer = Bounce();

int ledState = LOW;  // SET A VARIABLE TO STORE THE LED STATE
int Counter = 0, LastCount = 0;

float Pitchmm;  // used for metric leadscrew calculation and stepper config
float Pitchtpi;

enum MODE  // available modes
{
  MM,
  TPI,
  FEED,
};
MODE Mode = MM;

void RotaryChanged();
RotaryEncoder Rotary(&RotaryChanged, 20, 21, 28);  // Pins 20 (DT), 21 (CLK), 28 (null) (SW)

void RotaryChanged() {
  const unsigned int state = Rotary.GetState();
  if (state & DIR_CW)
    Counter++;
  if (state & DIR_CCW)
    Counter--;
}

void setup() {
  Serial.begin(9600);
  Serial.println("T.I.S.M");  // Death to ART tour, featuring Great Trucking Songs of The Renaissance

  display1.setBrightness(0x0f);
  display2.setBrightness(0x0f);
  delay(500);

  display1.clear();
  display2.clear();
  display1.showNumberDecEx(8888, 0b01000000, false, 4, 4);  //Displays 10.11
  display2.showNumberDec(1234, false, 4, 0);                // shows 10 on far left

  MMdebouncer.attach(MMButt_PIN, INPUT_PULLUP);  // Attach the debouncer to a pin with INPUT_PULLUP mode
  MMdebouncer.interval(25);                      // Use a debounce interval of 25 milliseconds
  pinMode(MMLed_PIN, OUTPUT);                    // Setup the LED
  digitalWrite(MMLed_PIN, ledState);

  TPIdebouncer.attach(TPIButt_PIN, INPUT_PULLUP);
  TPIdebouncer.interval(25);
  pinMode(TPILed_PIN, OUTPUT);
  digitalWrite(TPILed_PIN, ledState);

  FEEDdebouncer.attach(FEEDButt_PIN, INPUT_PULLUP);
  FEEDdebouncer.interval(25);
  pinMode(FEEDLed_PIN, OUTPUT);
  digitalWrite(FEEDLed_PIN, ledState);

  STOPdebouncer.attach(STOPButt_PIN, INPUT_PULLUP);
  STOPdebouncer.interval(25);
  pinMode(STOPLed_PIN, OUTPUT);
  digitalWrite(STOPLed_PIN, ledState);

  FWDdebouncer.attach(FWDButt_PIN, INPUT_PULLUP);
  FWDdebouncer.interval(25);
  pinMode(FWDLed_PIN, OUTPUT);
  digitalWrite(FWDLed_PIN, ledState);

  REVdebouncer.attach(REVButt_PIN, INPUT_PULLUP);
  REVdebouncer.interval(25);
  pinMode(REVLed_PIN, OUTPUT);
  digitalWrite(REVLed_PIN, ledState);
}
void loop() {
  readButton();  // read button and update mode
  switch (Mode) {
    case MM:
      mm();
      break;
  }
  switch (Mode) {
    case TPI:
      tpi();
      break;
  }
  switch (Mode) {
    case FEED:
      feed();
      break;
  }
}

/*MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
   
  } else

    TPIdebouncer.update();
  if (TPIdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(TPILed_PIN, ledState);
   

  } else

    FEEDdebouncer.update();
  if (FEEDdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(FEEDLed_PIN, ledState);
  } else

    STOPdebouncer.update();
  if (STOPdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(STOPLed_PIN, ledState);
  } else

    FWDdebouncer.update();
  if (FWDdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(FWDLed_PIN, ledState);
  } else

    REVdebouncer.update();
  if (REVdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(REVLed_PIN, ledState);
  }
}
*/
// read button and update mode

void readButton() {

  MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
    switch (Mode)
      mm();
    //break;
  } else

    TPIdebouncer.update();
  if (TPIdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(TPILed_PIN, ledState);
    switch (Mode)
      tpi();
  }
}



void mm() {
  Serial.println("void mm");
  if (LastCount != Counter) {
    //Serial.println(Counter);
    Pitchmm = Counter / 100.00;  //Metric threads have dec point values. i.e 1.25mm
    display1.clear();
    display1.showNumberDecEx(Counter, 0b10000000, true, 3, 1);
    Serial.println(Pitchmm);
  }
}

void tpi() {
  Serial.println("void tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display1.clear();
    display1.showNumberDec(Pitchtpi, false, 2, 1);
    tpi();
  }
}

void feed() {
  Serial.println("feed tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display1.clear();
    display1.showNumberDec(Pitchtpi, false, 2, 1);
    tpi();
  }
}
 
ERROR CODE

Code:
In file included from /home/stu/Arduino/Lathe_controler/Lathe_controler.ino:23:
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h: In constructor 'RotaryEncoder::RotaryEncoder(CallbackFunc, byte, byte, byte)':
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:36:28: warning: 'RotaryEncoder::RotaryCallback' will be initialized after [-Wreorder]
   36 |         const CallbackFunc RotaryCallback;
      |                            ^~~~~~~~~~~~~~
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:33:16: warning:   'const byte RotaryEncoder::DT_Pin' [-Wreorder]
   33 |     const byte DT_Pin, CLK_Pin, SW_Pin;
      |                ^~~~~~
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:39:5: warning:   when initialized here [-Wreorder]
   39 |     RotaryEncoder(const CallbackFunc RotaryCallback, const byte DT_Pin = 2, const byte CLK_Pin = 3, const byte SW_Pin = 4) : RotaryCallback(RotaryCallback), DT_Pin(DT_Pin), CLK_Pin(CLK_Pin), SW_Pin(SW_Pin)
      |     ^~~~~~~~~~~~~
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino: In function 'void loop()':
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:126:10: warning: enumeration value 'TPI' not handled in switch [-Wswitch]
  126 |   switch (Mode) {
      |          ^
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:126:10: warning: enumeration value 'FEED' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:131:10: warning: enumeration value 'MM' not handled in switch [-Wswitch]
  131 |   switch (Mode) {
      |          ^
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:131:10: warning: enumeration value 'FEED' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:136:10: warning: enumeration value 'MM' not handled in switch [-Wswitch]
  136 |   switch (Mode) {
      |          ^
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:136:10: warning: enumeration value 'TPI' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino: In function 'void readButton()':
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:191:12: warning: enumeration value 'MM' not handled in switch [-Wswitch]
  191 |     switch (Mode) mm();
      |            ^
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:191:12: warning: enumeration value 'TPI' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:191:12: warning: enumeration value 'FEED' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:199:12: warning: enumeration value 'MM' not handled in switch [-Wswitch]
  199 |     switch (Mode) tpi();
      |            ^
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:199:12: warning: enumeration value 'TPI' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:199:12: warning: enumeration value 'FEED' not handled in switch [-Wswitch]
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:191:21: warning: statement will never be executed [-Wswitch-unreachable]
  191 |     switch (Mode) mm();
      |                   ~~^~
/home/stu/Arduino/Lathe_controler/Lathe_controler.ino:199:22: warning: statement will never be executed [-Wswitch-unreachable]
  199 |     switch (Mode) tpi();
      |                   ~~~^~
Memory Usage on Teensy 4.0:
  FLASH: code:13000, data:4040, headers:8556   free for files:2006020
   RAM1: variables:4704, code:11216, padding:21552   free for local variables:486816
   RAM2: variables:12416  free for malloc/new:511872
 
Your switch code should be:

Code:
  switch (Mode) {
    case MM:
      mm();
      break;
    case TPI:
      tpi();
      break;
    case FEED:
      feed();
      break;
  }
 
Well so far it is almost doing what I want.
Currently it does not change mode when the respective button is pressed, just stays in "mm" mode and I am not sure why.
The button light does display when the respective button is pressed, but no mode change and the button will stay on when a new mode is selected
I would actually like it to cancel previous mode light button to cancel when a new mode is selected
LED disp2 shows the counter number for "mm" only, as does serial output

Code:
/*
MM Pin0 = MMButt
   Pin1 = MMLed
TPI Pin2 = TPIButt
    Pin3 = TPILed
Feed Pin4 = FeedButt
     Pin5 = FeedLED
STOP Pin6 = STOPButt
     Pin7 = STOPLed
FWD Pin8 = FWDButt
    Pin9 = FWDLED
REV Pin10 = REVButt
    Pin11 = REVLED
DISP1 Pin16 = DISP1Clk
      Pin17 = DISP1Dat
DISP2 Pin18 = DISP2Clk
      Pin19 = DISP2Dat
ENC Pin20 = ENCClk
    PIN21 = ENCDat
SPINDLE Pin22 = SPINDLE_A
        Pin23 = SPINDLE_b
        */
#include <RotaryEncoder.h>  //Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h https://github.com/ownprox/Rotary_Encoder_KY-040_Fixed.....NOTE. this installs as RotaryEncoder.h, so make sure to remove any existing library
#include <Bounce2.h>
#include <SPI.h>
#include <Wire.h>
#include <TM1637Display.h>

#define MMButt_PIN 0  // WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define MMLed_PIN 1   // DEFINE THE PIN FOR THE LED :
#define TPIButt_PIN 2
#define TPILed_PIN 3
#define FEEDButt_PIN 4
#define FEEDLed_PIN 5
#define STOPButt_PIN 6
#define STOPLed_PIN 7
#define FWDButt_PIN 8
#define FWDLed_PIN 9
#define REVButt_PIN 10
#define REVLed_PIN 11
#define CLK1 16  //Display 1 Clk
#define DIO1 17  // Display 1 data
#define CLK2 18  // Display 2 Clk
#define DIO2 19  // Display 2 data

TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);

Bounce MMdebouncer = Bounce();
Bounce TPIdebouncer = Bounce();
Bounce FEEDdebouncer = Bounce();
Bounce STOPdebouncer = Bounce();
Bounce FWDdebouncer = Bounce();
Bounce REVdebouncer = Bounce();

int ledState = LOW;  // SET A VARIABLE TO STORE THE LED STATE
int Counter = 0, LastCount = 0;

float Pitchmm;  // used for metric leadscrew calculation and stepper config
float Pitchtpi;

enum MODE  // available modes
{
  MM,
  TPI,
  FEED,
};
MODE mode = MM;

void RotaryChanged();
RotaryEncoder Rotary(&RotaryChanged, 20, 21, 28);  // Pins 20 (DT), 21 (CLK), 28 (SW) - 28 is just a null number as the (SW) is not used

void RotaryChanged() {
  const unsigned int state = Rotary.GetState();
  if (state & DIR_CW)
    Counter++;
  if (state & DIR_CCW)
    Counter--;
}
/*
void mm() {
  Serial.println("void mm");
  if (LastCount != Counter) {
    //Serial.println(Counter);
    Pitchmm = Counter / 100.00;  //Metric threads have dec point values. i.e 1.25mm
    display1.clear();
    display1.showNumberDecEx(Counter, 0b10000000, true, 3, 1);
    Serial.println(Pitchmm);
  }
}

void tpi() {
  Serial.println("void tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display1.clear();
    display1.showNumberDec(Pitchtpi, false, 2, 1);
    //tpi();
  }
}

void feed() {
  Serial.println("feed tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display1.clear();
    display1.showNumberDec(Pitchtpi, false, 2, 1);
    //feed();
  }
}
*/
void setup() {
  Serial.begin(9600);
  Serial.println("T.I.S.M");  // Death to ART tour, featuring Great Trucking Songs of The Renaissance

  display1.setBrightness(0x0f);
  display2.setBrightness(0x0f);
  delay(500);

  display1.clear();
  display2.clear();
  display1.showNumberDecEx(8888, 0b01000000, false, 4, 4);  //Displays 10.11
  display2.showNumberDec(1234, false, 4, 0);                // shows 10 on far left

  MMdebouncer.attach(MMButt_PIN, INPUT_PULLUP);  // Attach the debouncer to a pin with INPUT_PULLUP mode
  MMdebouncer.interval(25);                      // Use a debounce interval of 25 milliseconds
  pinMode(MMLed_PIN, OUTPUT);                    // Setup the LED
  digitalWrite(MMLed_PIN, ledState);

  TPIdebouncer.attach(TPIButt_PIN, INPUT_PULLUP);
  TPIdebouncer.interval(25);
  pinMode(TPILed_PIN, OUTPUT);
  digitalWrite(TPILed_PIN, ledState);

  FEEDdebouncer.attach(FEEDButt_PIN, INPUT_PULLUP);
  FEEDdebouncer.interval(25);
  pinMode(FEEDLed_PIN, OUTPUT);
  digitalWrite(FEEDLed_PIN, ledState);

  STOPdebouncer.attach(STOPButt_PIN, INPUT_PULLUP);
  STOPdebouncer.interval(25);
  pinMode(STOPLed_PIN, OUTPUT);
  digitalWrite(STOPLed_PIN, ledState);

  FWDdebouncer.attach(FWDButt_PIN, INPUT_PULLUP);
  FWDdebouncer.interval(25);
  pinMode(FWDLed_PIN, OUTPUT);
  digitalWrite(FWDLed_PIN, ledState);

  REVdebouncer.attach(REVButt_PIN, INPUT_PULLUP);
  REVdebouncer.interval(25);
  pinMode(REVLed_PIN, OUTPUT);
  digitalWrite(REVLed_PIN, ledState);
}
void loop() {
  readButton();  // read button and update mode
  switch (mode) {
    case MM:
      mm();
      break;
    case TPI:
      tpi();
      break;
    case FEED:
      feed();
      break;
  }
}

/*MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
   
  } else

    TPIdebouncer.update();
  if (TPIdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(TPILed_PIN, ledState);
   

  } else

    FEEDdebouncer.update();
  if (FEEDdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(FEEDLed_PIN, ledState);
  } else

    STOPdebouncer.update();
  if (STOPdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(STOPLed_PIN, ledState);
  } else

    FWDdebouncer.update();
  if (FWDdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(FWDLed_PIN, ledState);
  } else

    REVdebouncer.update();
  if (REVdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(REVLed_PIN, ledState);
  }
}
*/
// read button and update mode

void readButton() {

  MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
    switch (mode)
    case MM:
    default:
      break;
  } else

    TPIdebouncer.update();
  if (TPIdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(TPILed_PIN, ledState);
    switch (mode)
    case TPI:
    default:
      break;
  } else
    FEEDdebouncer.update();
  if (FEEDdebouncer.fell()) {
    ledState = !ledState;
    digitalWrite(FEEDLed_PIN, ledState);
    switch (mode)
    case FEED:
    default:
      break;
  }
}
void mm() {
  Serial.println("void mm");
  if (LastCount != Counter) {
    //Serial.println(Counter);
    Pitchmm = Counter / 100.00;  //Metric threads have dec point values. i.e 1.25mm
    display2.clear();
    display2.showNumberDecEx(Counter, 0b10000000, true, 3, 1);
    Serial.println(Pitchmm);
  }
}

void tpi() {
  Serial.println("void tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display2.clear();
    display2.showNumberDec(Pitchtpi, false, 2, 1);
    //tpi();
  }
}

void feed() {
  Serial.println("feed tpi");
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchtpi = Counter;  // value in TPI
    display2.clear();
    display2.showNumberDec(Pitchtpi, false, 2, 1);
    //feed();
  }
}



Code:
In file included from /home/stu/Arduino/Lathe_controler/Lathe_controler.ino:23:
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h: In constructor 'RotaryEncoder::RotaryEncoder(CallbackFunc, byte, byte, byte)':
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:36:28: warning: 'RotaryEncoder::RotaryCallback' will be initialized after [-Wreorder]
   36 |         const CallbackFunc RotaryCallback;
      |                            ^~~~~~~~~~~~~~
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:33:16: warning:   'const byte RotaryEncoder::DT_Pin' [-Wreorder]
   33 |     const byte DT_Pin, CLK_Pin, SW_Pin;
      |                ^~~~~~
/home/stu/Arduino/libraries/Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h:39:5: warning:   when initialized here [-Wreorder]
   39 |     RotaryEncoder(const CallbackFunc RotaryCallback, const byte DT_Pin = 2, const byte CLK_Pin = 3, const byte SW_Pin = 4) : RotaryCallback(RotaryCallback), DT_Pin(DT_Pin), CLK_Pin(CLK_Pin), SW_Pin(SW_Pin)
      |     ^~~~~~~~~~~~~
Memory Usage on Teensy 4.0:
  FLASH: code:13016, data:4040, headers:8540   free for files:2006020
   RAM1: variables:4704, code:11232, padding:21536   free for local variables:486816
   RAM2: variables:12416  free for malloc/new:511872
 
Well so far it is almost doing what I want.
Currently it does not change mode when the respective button is pressed, just stays in "mm" mode and I am not sure why.
The button light does display when the respective button is pressed, but no mode change and the button will stay on when a new mode is selected
I would actually like it to cancel previous mode light button to cancel when a new mode is selected
LED disp2 shows the counter number for "mm" only, as does serial output

In your readButton() function, the code below is probably not doing what I think that you intended (if I am discerning your intention correctly . . . the "switch", the "case", and the "default" statements have no effect & are not making any changes that affect which button is active and/or processed . . . again, I hope that I am discerning your intention correctly !!):

Code:
  MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
    switch (mode)
    case MM:
    default:
      break;
  } else

I think what you really intended is as follows:

Code:
  MMdebouncer.update();      // Update the Bounce instance
  if (MMdebouncer.fell()) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState;    // Toggle LED state
    digitalWrite(MMLed_PIN, ledState);
    mode = MM:  // change mode to MM
  } else

Try making that equivalent change to the processing for each of the three buttons & see if that gets you any closer to what you intended. And please feel free to ask any other questions that you may have.

You can use the "indentation" hotkey (CTRL-T) in the Arduino IDE to cause the indentation to all be standardized. This is very beneficial to help in making sure that sections of code are inside the intended sets of brackets enclosing a block, that an "else" or an ending bracket lines up with the intended "if" block and/or starting bracket, that your "case" statements are in fact part of the intended "switch" block, etc.

Hope that helps . . .

Mark J Culross
KD5RXT
 
Hi Mark

Thank you for that. I do use indentation, but for some reason, when I pasted it it did its own thing.

I added your changes to readButton() it it worked as intended, as you original thought. The switch,case,break etc was me just trying to see what would work and how it (didnt) work

I did find I had to make some changes to the respective mm,tpi,feed functions to control the indicator lights as they would remain on after changing modes

Stuart
ex VK2USR

Code:
void mm() {
  Serial.println("void mm");
  digitalWrite(MMLed_PIN, HIGH);  // set value to respect void
  digitalWrite(TPILed_PIN, LOW);  //
  digitalWrite(FEEDLed_PIN, LOW);  //
  if (LastCount != Counter) {
    Serial.println(Counter);
    Pitchmm = Counter / 100.00;  //Metric threads have dec point values. i.e 1.25mm
    display2.clear();
    display2.showNumberDecEx(Counter, 0b10000000, true, 3, 1);  // Use Counter value for decimal point placement on LED disp
    Serial.println(Pitchmm);
  }
}
 
Back
Top