8 years, 11 months ago.

How to make a menu with switch?

Hello everyone I am currently conducting a project to display various items for generator engines. I écrti you because I have finished making all of my programs and now want to make a menu in order to select each item that I programmed. But I do not know how. The selection with the menu must be done apputant on the push buttons (I have already done a PCB). -the first program I did used to display the frequency:

#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
Ticker tick;
InterruptIn in(p10);
Timer t;

int t_period = 0;                   // This is the period between interrupts in microseconds
float t_freq = 0;

void flip();                  // Renvoie la frequence


int main() {
    in.mode(PullDown);              // Set the pin to Pull Down mode.
    in.rise(&flip);                 // Set up the interrupt for rising edge
    t.start();                     // start the timer

    while (1) {
        wait_ms(100);
        lcd.printf(" \n %f Hz\r \n", t_freq);
    }

}

void flip() {
    t_period = t.read_us();                // Get time since last interrupt
    t_freq = (1/(float)t_period)*1000000;   // Convert period (in us) to frequency (Hz)
    t.reset();                             // Reset timer and wait for next interrupt
}

-I then made a program to display the number of rev / min, the dwell and duty cycle.

#include "mbed.h"
#include "TextLCD.h"
 
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7  //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p10); // déclaration de p10 dans in
Timer t;
Timer t_on;
Timer t_off;
 
int t_period = 0; // This is the period between interrupts in microseconds i
float t_freq = 0;  // <-- Set in an interrupt but read in main. Should be volatile or the compiler could optimise it out.
 
void flip(); //Renvoie la frequence
 
int main() { 
int trmin;
float alpha=0;
in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); //Set up the interrupt for rising edge
 
t.start(); //start the timer
 
while (1) {
 wait_ms(100);
 trmin=(t_freq*4*30)/4; 

 if (in==1) {  // <--- the input that you have created not the pin number.
    t_on.start();
    t_off.stop();
  } else {
    t_on.stop();
    t_off.start();
    alpha=t_on/(t_on+t_off);
  }
 
   lcd.printf(" rc = %f trmin = %d \n ",alpha,trmin);
 
}
 
}
 
void flip() {
  t_period = t.read_us();// Get time since last interrupt
  t_freq = (1/(float)t_period)*1000000; // Convert period (in us) to frequency (Hz)
   t.reset(); // Reset timer and wait for next interrupt  <--- Timing would be more accurate if this was one line earlier
}

-Finally, I made a program to make the identification of cylinders, to cut a cylinder when I want.

#include "mbed.h"
#include "TextLCD.h"
 
TextLCD lcd(p15, p14, p17, p18, p19, p20); // rs, e, d4-d7  //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p10); // déclaration de p10 dans in
DigitalIn  bougie(p7); //commande l'info bougie; sert à identifier chaque cylindre
DigitalIn rupt(p6);//Commande le rupteur
DigitalOut cc(p5);//commande le court-circuit; sert aussi à enlevé un des cylindres

// Déclaration des bp
DigitalIn  bp1(p21);
DigitalIn  bp2(p22);
DigitalIn  bp3(p23);
DigitalIn  bp4(p24);
DigitalIn  bp5(p25);
DigitalIn  bp6(p26);
// Déclaration des led

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);


int main() { 
short int cyl1,cyl2,cyl3,cyl4,cyl5,cyl6;
int ref_cyl2; // déclaration de cette entrée qui va servir de référence dans l'identification des cylindres
if(bp1==0){ //condition pour que le reperage de bougie fonctionne
if ((bougie and rupt)==1)
 {cyl1=1; // allumer la led1
 myled1=1;}
  

 
 if ((rupt and cyl1)==1)
 {   cyl1=0;
     cyl3=1;
 myled3=1;
 myled1=0;}
 
 if ((rupt and cyl3)==1)
 {   cyl3=0;
     cyl4=1;
     myled4=1;
     myled3=0;}
    
 if ((rupt and cyl4)==1)
 {   
     cyl4=0;
     cyl2=1;
     myled4=0;
     myled2=1;}

}
 //mise à la masse
 if((bp2 and cyl2)==1)
 {
     cc=1;
    if (cyl1==1)cc=0;
    }
    
  if((bp3 and cyl3)==1)
  {cc=1;
  if (cyl4==1) cc=0;
  }
  if((bp4 and cyl3)==1)
  {cc=1;
  if (cyl2==1) cc=0;
  }
 /*Problème
  if((bp1 and cyl1)==1)
  {cc=1;
  if (cyl3==1) cc=0;
  }
  */
  
  if((ref_cyl2)==1)
  {cyl1=1;
  cyl2=0;
  cyl3=0;
  cyl4=0;}
  
 } 
 

Now I wish to make a menu that will allow me to display a program when I press my buttons. Do you know how I do them?

I had the following program:

switch(menu1)
   {
       case bp1==1:
       lcd.printf("\n bp1: angle\r \n");
       break;
       case bp2==1:
       lcd.printf("\n bp2: dwell\r \n");
       break;
       default:
       lcd.printf("\n bp1 ou bp2\r \n"); 
       break;
       }
       
    switch(menu2)

{ case bp3:
 lcd.printf(" %d trmin\r \n \n", trmin);
 break;
    }

1 Answer

8 years, 11 months ago.

A switch statement can only look at once variable

int menuChoice = ....

switch (menuChoice) {
  case 0: // this code is run if menuChoice == 0
    lcd.printf("Option0\r \n");
    break;
  case 1:
    lcd.printf("Option1\r \n");
    break;
  default: // any value other than 0 or 1
    lcd.printf("No valid option\r \n");
    break;
}

If you have a lot of different variables you need to check (e.g. bp1, bp2, bp3) then it's best to use if rather than switch

if (bp1) {   }
else if (pb2) {   }
else if (pb3) {   }

The other option which is probably far too complicated for this but gives you easier control over what to do if multiple buttons are pressed is to combine them into one variable and then do a switch:

int menuChoice = 0;
if (bp1)
  menuChoice |= 1;
if (bp2)
  menuChoice |= 1 << 1;
if (bp3)
  menuChoice |= 1 << 2;

switch (menuChoice) {
  case 0:
      // no buttons
  break;
  case 1:
     // bp1 only
  break;
  case 2:
     // bp2 only
  break;
  case 3:
     // bp1 & bp2
  break;
  case 4:      // bp3 only
  case 5:      // bp1 and bp3
    // code for button3 only or 3 & 1
  break;
  case 6:   // bp2 and bp3
  case 7:   // bp1, bp2 and bp3
    // code for 2 & 3 or 1, 2 & 3
  break;
  default:
    // should never happen
  break
}