Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

do while loop inside switch case

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,493
Please let me know how exit from do while loop in switch case OR I want to change any variable value inside do while how to do it?


Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

int upButton = 10;
int downButton = 11;
int selectButton = 12;
unsigned int menu = 1, w = 0;

void setup() {
  lcd.begin();
  lcd.backlight();
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(selectButton, INPUT_PULLUP);
  updateMenu();
}

void loop() {
  if (!digitalRead(downButton)) {
    w = 1;
    menu++;
    updateMenu();
    delay(100);
    while (!digitalRead(downButton));
  }
  if (!digitalRead(upButton)) {
    menu--;
    updateMenu();
    delay(100);
    while (!digitalRead(upButton));
  }
  if (!digitalRead(selectButton)) {
    executeAction();
    updateMenu();
    delay(100);
    while (!digitalRead(selectButton));
  }
}

void updateMenu() {
  switch (menu) {
    case 0:
      menu = 1;
      break;
    case 1:
      lcd.clear();
      lcd.print("DO WHILE LOOP");
      do {
        lcd.setCursor(0, 1);
        lcd.print("DW LOOP RUN");
      } while (w != 1);
      w = 0;
      break;
    case 2:
      lcd.clear();
      lcd.print(" MenuItem1");
      lcd.setCursor(0, 1);
      lcd.print(">MenuItem2");
      break;
    case 3:
      lcd.clear();
      lcd.print(">MenuItem3");
      lcd.setCursor(0, 1);
      lcd.print(" MenuItem4");
      break;
    case 4:
      lcd.clear();
      lcd.print(" MenuItem3");
      lcd.setCursor(0, 1);
      lcd.print(">MenuItem4");
      break;
    case 5:
      menu = 4;
      break;
  }
}

void executeAction() {
  switch (menu) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {
  lcd.clear();
  lcd.print(">Executing #1");
  delay(1500);
}
void action2() {
  lcd.clear();
  lcd.print(">Executing #2");
  delay(1500);
}
void action3() {
  lcd.clear();
  lcd.print(">Executing #3");
  delay(1500);
}
void action4() {
  lcd.clear();
  lcd.print(">Executing #4");
  delay(1500);
}

}
 

Hi,

If you want to a do multiple tasks in one loop, then you need to avoid busy waits and blocking routines.

You may use a timer to trigger to run the loop maybe every 10ms.
Then use a simple counter to get a 10 x 10ms = 100ms delay.... for example

Klaus

Btw: you could shorten and simplify your code a lot.
 

Generally speaking you need a flag or event external to the do while construct, like
an interrupt from internal or external event.

Or poll inside the do while a hardware state, or memory value, or flag set by
process outside do while. A flag could be set by HW process, or another thread
as running in an RTOS or a process running in a multi core processor.


Regards, Dana.
 

It's generally a bad idea to wait in main() or any of it's subsidiary functions for a keypress or -release. You also don't want delays longer than a few ms.

Instead consider the wait for keypress or delay expiration as states of multiple state machines periodically called from main(). You usually want a global time counter (with e.g. ms resolution) advanced by a timer interrupt, possibly a timer interrupt driven keyboard handling with debounce, interrupt driven communication with FIFO buffers.
 

By the way some processors have onchip hardware to do key debounce
so that delays no longer needed.

Also when you debounce some coders implement key down and then release,
eg. bounce in and bounce out before acting on a key press. To eliminate spurious
responses.


Regards, Dana.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top