10 years, 5 months ago.

How to use switch or case in my program

Hello. I started programming with the mbed microcontroller . I work on a project réalsation an adjustment device for the generator. I first connect my microcontroller and my lcd screen. I then programmed to display the frequency on my screen then turns / min , duty cycle and dwell . Now I want to show the cam angle , depending on the number of cylinders of my engine (1 to 6) and using the push buttons. For that I second flight condition "if" but it's not good because I would have to use a switch / case into a menu, depending on the number of cylinders. I do not know how. So I wrote to you to see if some of you would have a Udee . Thank you for voting help. I have attached the code I 've written .

--------------------------------

  1. include "mbed.h"
  2. 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 DigitalIn e1(p21); DigitalIn e2(p22); DigitalIn e3(p23); DigitalIn e4(p24); DigitalIn e5(p25); DigitalIn e6(p26);

Timer t; Timer t_on; Timer t_off;

int t_period = 0; This is the period between interrupts in microseconds 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; int dwell=0; float alpha=0; float angle1=0;float angle2=0;float angle3=0;float angle4=0;float angle5=0;float angle6=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); dwell=alpha*100; }

lcd.printf("nbre de cylindres \n");

switch(s){ case e: if(e1==1) { angle1=(dwell*360)/100; lcd.printf("ac 1cy=%f\r\n",angle1);} break;

case f: else if(e2==1){ angle2=(dwell*360)/200; lcd.printf("ac 2cy=%f\r\n",angle2);} break;

case g: else if(e3==1){ angle3=(dwell*360)/300; lcd.printf("ac 3cy=%f\r\n",angle3); break;} else if(e4==1){ case h: angle4=(dwell*360)/400; lcd.printf("ac 4cy=%f\r\n",angle4);} break;

case i: else if(e5==1){ angle5=(dwell*360)/500; lcd.printf("ac 5cy=%f\r\n",angle5);} break;

case j: else if(e6==1){ angle6=(dwell*360)/600; lcd.printf("ac 6cy=%f\r\n",angle6);} break;

default: lcd.printf("choisir un cylindre\n");

}

}

}

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 }

1 Answer

10 years, 5 months ago.

1) as previously stated please use <<code>> and <</code>> to make your code readable. See the Editing tips link for details.

2) If you are going to copy and paste code from the answers to earlier questions then either also implement the changes indicated or remove the comments telling you what to change. Leaving the comments in but not making the changes will confuse people.

3) Please don't delete your previous questions, it helps if people can see past questions so we don't have to answer the same questions over and over again.

You seem to have the syntax for switch completely wrong and mixed in with if/else. You use one or the other not both mixed together.

int cylinders = [Get some user input]
switch (cylinders) {
  case 1: 
    angle=(dwell*360)/100;  // <- Note - set the same variable every time not angle1 for 1 cylinder, angle2 for 2 cylinders etc...
    lcd.printf("ac 1cy=%f\r\n",angle);
    break;
  case 2:
    angle=(dwell*360)/200; // <- Note - set the same variable every time not angle1 for 1 cylinder, angle2 for 2 cylinders etc...
    lcd.printf("ac 2cy=%f\r\n",angle);
    break;
etc.....
  default:
    angle = 0;
    break;
}

Or you could do

if (button_For_1_cylinder) {
    angle=(dwell*360)/100;  // <- Note - set the same variable every time not angle1 for 1 cylinder, angle2 for 2 cylinders etc...
    lcd.printf("ac 1cy=%f\r\n",angle);
} else if (button_For_2_cylinder2) {
    angle=(dwell*360)/200; // <- Note - set the same variable every time not angle1 for 1 cylinder, angle2 for 2 cylinders etc...
    lcd.printf("ac 2cy=%f\r\n",angle);
} else ....

Accepted Answer

I thank you for your answer. I tested the program taking into account your modifications and it works well. I wanted to ask how I could well écriire the code as you do it. thank you

posted by AGON Juppé 11 May 2015

Quote:

I wanted to ask how I could well écriire the code as you do it.

<<code>>
int x = 1;
<</code>>

will generate

int x = 1;
posted by Andy A 11 May 2015