Overview: I am trying to use a switch statement to run different tests. A main menu is displayed on the serial monitor, and then the user is given the option to enter their menu choice in the serial monitor. The menu choice entered in the serial monitor should subsequently run the appropriate case statement.
Issues: 1. When I enter a choice the case statement for that choice does not run 2. When I enter a choice my the serial monitor scrolls through my menu 3 times displaying 49, then 13, then 10.
Questions:
- Why are my case statements not executing?
Why does the serial monitor scroll three times displaying these random numbers?
//////////////PREPROCESSOR DIRECTIVES///////// #include <Wire.h> /////////////GLOBAL VARIABLES//////////////// int ADDRESS_SLAVE = 0x08; int ADDRESS_VERSION = 0x00; int ADDRESS_STATUS = 0x01; int ADDRESS_ENABLE = 0x02; int ADDRESS_CONFIG = 0x03; int ADDRESS_RESTART = 0x04; int ADDRESS_STARTUP_STATE = 0X05; int ADDRESS_COMPENDISREG1 = 0X07; int ADDRESS_COMPENDISREG2 = 0X08; int ADDRESS_COMPENDISREG3 = 0X09; int ADDRESS_COMPENDISREG4 = 0X0A; int ADDRESS_FAULT = 0X0B; int ADDRESS_FAULTMASK = 0X0C; int ADDRESS_TEST_MODE = 0X0D; ////////////SETUP CONDITIONS///////////////// void setup() { Wire.setClock(100000); Wire.beginTransmission(byte(ADDRESS_SLAVE)); Serial.begin(9600); } ////////////MAIN FUNCTION/////////////////// void loop() { int choice = 0; int const //Clock 100 CLOCK_FREQ_P = 101, CLOCK_FREQ_DIV4 = 102, CLOCK_FREQ_DIV8 = 103, CLOCK_FREQ_DIV16 = 104, CLOCK_FREQ_DIV32 = 105, CLOCK_INT_SUPPLY_IBIAS = 106, CLK_OK = 107, //Bandgap 200 BG_2p5V = 200, BG_MAX_LOAD = 201, BG_VBE = 202, BG_VP = 203, BG_VGATE = 204, mainMenu(); choice = menuChoice(); switch(choice) { case CLOCK_FREQ_P : { Serial.println(F("CLOCK_FREQ_P Not yet built out")); while(Serial.available() > 0) { int choice = Serial.read(); } delay(10000); } break; case CLOCK_FREQ_DIV4: { Serial.println(F("CLOCK_FREQ_DIV4 Not yet built out")); } break; case CLOCK_FREQ_DIV8: { Serial.println(F("FREQ_DIV8 Not yet built out")); } break; case CLOCK_FREQ_DIV16: { Serial.println(F("CLOCK_FREQ_DIV16 Not yet built out")); } break; case CLOCK_FREQ_DIV32: { Serial.println(F("CLOCK_FREQ_DIV32 Not yet built out")); } break; case CLOCK_INT_SUPPLY_IBIAS: { Serial.println(F("CLOCK_INT_SUPPLY_IBIAS Not yet built out")); } break; case CLK_OK: { Serial.println(F("CLK_OK Not yet built out")); } break; case BG_2p5V: { Serial.println(F("BG_2p5V Not yet built out")); } break; case BG_MAX_LOAD: { Serial.println(F("BG_MAX_LOAD Not yet built out")); } break; case BG_VBE: { Serial.println(F("BG_VBE Not yet built out")); } break; case BG_VP: { Serial.println(F("BG_VP Not yet built out")); } break; case BG_VGATE: { Serial.println(F("BG_VGATE Not yet built out")); } break; delay(500); } ////////////////FUNCTIONS/////FUNCTIONS/////FUNCTIONS/////FUNCTIONS/////////////////////////// //******************************************* //** Function: Main Menu Display //** Passes: nothing //** Returns: nothing //******************************************* void mainMenu() { //Prints main menu to the serial monitor Serial.println(); Serial.println(F("\t\t\t\t\t\t\t\t\t\t ******************************")); Serial.println(F("\t\t\t\t\t\t\t\t\t\t ******************************")); Serial.println(F("\t\t\t\t\t\t\t\t\t\t ** MAIN MENU **")); Serial.println(F("\t\t\t\t\t\t\t\t\t\t ******************************")); Serial.println(F("\t\t\t\t\t\t\t\t\t\t ******************************")); Serial.println(); Serial.println(F("\t\t\t\t\t\t ********************************************************************************************************************")); Serial.println(F("\t\t\t\t\t\t ** Please type in the number of the test you want to run in the command line window at the top of the screen. **")); Serial.println(F("\t\t\t\t\t\t ********************************************************************************************************************")); Serial.println(); Serial.println(); // ****************** // *** TOP MENU *** // ****************** Serial.print(F("\t\t\t CLOCKGEN \t\t\t\t BANDGAP \t\t\t GPIO ")); Serial.println(); Serial.print(F("\t\t\t (1) CLK_FREQ_P \t\t\t (8) BG_2p5V \t\t\t (13) GPIO0_PD \t (21) GP1_N_Buff \t (29) GP2_Buff_Max \t ")); Serial.println(); Serial.print(F("\t\t\t (2) CLK_FREQ_Div_4 \t\t\t (9) BG_MAX_LOAD \t\t (14) GPIO0_PU \t (22) GP1_W_Buff \t (30) GP2_Buff_Min ")); Serial.println(); Serial.print(F("\t\t\t (3) CLK_FREQ_Div_8 \t\t\t (10) BG_VBE \t\t\t (15) GP0_N_Buff \t (23) GP1_Buff_Max \t (31) GPIO3_PD ") ); Serial.println(); Serial.print(F("\t\t\t (4) CLK_FREQ_Div_16 \t\t\t (11) BG_VP \t\t\t (16) GP0_W_Buff \t (24) GP1_Buff_Min \t (32) GPIO3_PU ")); Serial.println(); Serial.print(F("\t\t\t (5) CLK_FREQ_Div_32 \t\t\t (12) BG_VGATE \t\t (17) GP0_Buff_Max \t (25) GPIO2_PD \t\t (33) GP3_N_BUFF ")); Serial.println(); Serial.print(F("\t\t\t (6) CLK_INT_Supply_(IBias)\t\t\t\t\t\t (18) GP0_Buff_Min \t (26) GPIO2_PU \t\t (34) GPIO3_W_BUFF ")); Serial.println(); Serial.print(F("\t\t\t (7) CLK_OK \t\t\t\t\t\t\t\t (19) GPIO1_PD \t (27) GP2_N_Buff \t (35) GP3_BUFF_Max ")); Serial.println(); Serial.print(F("\t\t\t\t\t\t\t\t\t\t\t\t (20) GPIO1_PU \t (28) GP2_W_Buff \t (36) GP3_BUFF_Min ")); Serial.println(); Serial.println(); // ********************* // *** CENTER MENU *** // ********************* Serial.print(F("\t\t\t VBUS COMPARITOR \t\t\t\t\t\t\t VAC COMPARITOR \t\t ISNS COMPARITOR \t\t TSNS COMPARITOR")); Serial.println(); Serial.print(F("\t\t\t (37) CLK_FREQ_P \t(47) BG_2p5V \t(57) GPIO0_PD \t\t\t (61) GP1_N_Buff \t\t (68) GP2_Buff_Max \t\t (74) VB 2.715V Comparitor Test")); Serial.println(); Serial.print(F("\t\t\t (38) CLK_FREQ_P \t(48) BG_2p5V \t(58) GPIO0_PD \t\t\t (62) GP1_N_Buff \t\t (69) GP2_Buff_Max \t\t (75) VB 2.715V Comparitor Test")); Serial.println(); Serial.print(F("\t\t\t (39) CLK_FREQ_P \t(49) BG_2p5V \t(59) GPIO0_PD \t\t\t (63) GP1_N_Buff \t\t (70) GP2_Buff_Max \t\t (76) VB 2.715V Comparitor Test")); Serial.println(); Serial.print(F("\t\t\t (40) CLK_FREQ_P \t(50) BG_2p5V \t(60) GPIO0_PD \t\t\t (64) GP1_N_Buff \t\t (71) GP2_Buff_Max \t\t (77) VB 2.715V Comparitor Test")); Serial.println(); Serial.print(F("\t\t\t (41) CLK_FREQ_P \t(51) BG_2p5V \t\t\t\t\t (65) GPIO0_PD \t\t (72) GP1_N_Buff \t\t\t\t")); Serial.println(); Serial.print(F("\t\t\t (42) CLK_FREQ_P \t(52) BG_2p5V \t\t\t\t\t (66) GPIO0_PD \t\t (73) GP1_N_Buff \t\t\t\t")); Serial.println(); Serial.print(F("\t\t\t (43) CLK_FREQ_P \t(53) BG_2p5V \t\t\t\t\t (67) GPIO0_PD \t\t\t\t\t")); Serial.println(); Serial.print(F("\t\t\t (44) CLK_FREQ_P \t(54) BG_2p5V \t\t\t\t\t\t\t\t\t\t\t\t")); Serial.println(); Serial.print(F("\t\t\t (45) CLK_FREQ_P \t(55) BG_2p5V \t\t\t\t\t\t\t\t\t\t\t\t")); Serial.println(); Serial.print(F("\t\t\t (46) CLK_FREQ_P \t(56) BG_2p5V \t\t\t\t\t\t\t\t\t\t\t\t")); Serial.println(); Serial.println(); Serial.println(); // ********************* // *** BOTTOM MENU *** // ********************* Serial.print(F("\t\t\t QR COMPARITOR \t\t\t ADC \t\t\t MS Driver \t\t\t AC Driver \t\t\t Active Clamp FET \t\t V10")); Serial.println(); Serial.print(F("\t\t\t (78) CLK_FREQ_P \t\t (84) BG_2p5V \t\t (87) GPIO0_PD \t\t (96) GP1_N_Buff \t\t (100) AC_FET_LEAKAGE \t\t (1400) V10MIN_V10_OK_FAL")); Serial.println(); Serial.print(F("\t\t\t (79) CLK_FREQ_P \t\t (85) BG_2p5V \t\t (88) GPIO0_PD \t\t (97) GP1_N_Buff \t\t (101) AC_FET_LEAKAGE_SWEEP")); Serial.println(); Serial.print(F("\t\t\t (80) CLK_FREQ_P \t\t (86) BG_2p5V \t\t (89) GPIO0_PD \t\t (98) GP1_N_Buff \t\t (102) AC_FET_RDS_ON")); Serial.println(); Serial.print(F("\t\t\t (81) CLK_FREQ_P \t\t\t\t\t (90) BG_2p5V \t\t\t (99) GPIO0_PD")); Serial.println(); Serial.print(F("\t\t\t (82) CLK_FREQ_P \t\t\t\t\t (91) BG_2p5V")); Serial.println(); Serial.print(F("\t\t\t (83) CLK_FREQ_P \t\t\t\t\t (92) BG_2p5V")); Serial.println(); Serial.print(F("\t\t\t\t\t\t\t\t\t\t (93) CLK_FREQ_P")); Serial.println(); Serial.print(F("\t\t\t\t\t\t\t\t\t\t (94) CLK_FREQ_P")); Serial.println(); Serial.print(F("\t\t\t\t\t\t\t\t\t\t (95) CLK_FREQ_P")); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); } //******************************************* //** Function: menuChoice //** Passes: nothing //** Returns: integer for menu choice //******************************************* int menuChoice() { int selection = 0; while (Serial.available() == 0); { selection = Serial.read(); Serial.print("You entered: "); (int)Serial.println(selection, DEC); } return selection; }
choice
right afterchoice = menuChoice();
? .... that is a simple way of debugging the program – jsotola May 16 at 20:36