1

I want to see the voltage data using void volt();, when I go to the "Item2SubItem3" within void menuChanged. Now I can only see the data at that time: how can I refresh the value every second?

void setup()
{
    // setup()...
}

void loop()
{
    //loop()...
}

// DC Voltometer
float vPow = 4.7;
float r1 = 100000;
float r2 = 10000;

void volt()
{
    lcd.clear();
    float v = (analogRead(0) * vPow) / 1024.0;
    float v2 = v / (r2 / (r1 + r2));
    lcd.setCursor(0, 0);
    lcd.print("DC VOLTOMETER");
    lcd.setCursor(0, 1);
    lcd.print(v2);
    delay(1000);
}

void menuChanged(MenuChangeEvent changed)
{
    MenuItem newMenuItem = changed.to; // get the destination menu
    lcd.setCursor(0, 1);            // set start position for lcd
                                 // printing to the second row

    if (newMenuItem.getName() == menu.getRoot())
    {
        lcd.print("Main Menu       ");
    }
    else if (newMenuItem.getName() == "Item1")
    {
        lcd.print("Item1           ");
    }
    else if (newMenuItem.getName() == "Item1SubItem1")
    {
        lcd.print("Item1SubItem1");
    }
    else if (newMenuItem.getName() == "Item1SubItem2")
    {
        lcd.print("Item1SubItem2   ");
    }
    else if (newMenuItem.getName() == "Item2")
    {
        lcd.print("Item2           ");
    }
    else if (newMenuItem.getName() == "Item2SubItem1")
    {
        lcd.print("Item2SubItem1   ");
    }
    else if (newMenuItem.getName() == "Item2SubItem2")
    {
        lcd.print("Item2SubItem2   ");
    }
    else if (newMenuItem.getName() == "Item2SubItem3")
    {
        lcd.print("Item2SubItem3   ");
        delay(2000);
        volt();
    }
    else if (newMenuItem.getName() == "Item3")
    {
        lcd.print("Item3           ");
    }
    else if (newMenuItem.getName() == "Item4")
    {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Item4           ");
    }
}
  • I think it would be easier to answer your question if you try to break down your problem to a smaller piece of code. – user3704293 Aug 17 '15 at 19:01
  • menuChangeEvent is only called if the menu is changed. I.e. you moved inside the menu. I don't this the MenuBackend framework supports what you want it to do. – Gerben Aug 17 '15 at 19:07
  • how can i use a menu in order to monitoring sensors? – kosar Aug 17 '15 at 19:30
0

As Gerben implied in a comment, menuChangeEvent is called only when the menu selection changes. Thus, your voltmeter readout will print only whenever you change the menu selection to Item2SubItem3. The code shown below illustrates how to modify your loop() so that it checks the menu selection once per second. Each second the main loop will do whatever function is currently selected, such as displaying a current voltmeter reading. Note, you can make the menu decoding shorter and less tedious as shown in the answer, and can put variable declarations near where they are used instead of in random places.

void setup() {
}  // setup()...

// DC voltmeter reading
void volt () {
  const float vPow = 4.7, r1 = 100000, r2 = 10000;
  lcd.clear();
  float v = (analogRead(0) * vPow) / 1024.0;
  float v2 = v / (r2 / (r1 + r2));
  lcd.setCursor (0,0);
  lcd.print("DC Voltmeter");
  lcd.setCursor (0,1);
  lcd.print (v2);
  // Don't delay(1000) since loop() takes care of delay.
}

long int second = 0;
uint8_t itemcode = 0; // Will be number nm for Item n, Subitem m
int passnumber = 0;
void menuChanged(MenuChangeEvent changed) {
  MenuItem menuitem = changed.to; //get the destination menu
  String itemname = menuitem.getName();
  lcd.clear();        // Make printing spaces at line ends unnecessary
  lcd.setCursor(0,1);         //set lcd printing position = second row
  if(newMenuItem.getName() == menu.getRoot()){
    lcd.print("Main Menu");
    itemcode = 0;
  } else {
    lcd.print(itemname);
    itemcode = 10*(itemname[4] & 7);
    if (itemname.length() > 5)
      itemcode += itemname[itemname.length()-1] & 7;
  }
  passnumber = 0; // Actions can test passnumber if they like
  second = 0; // Cause actions to take place now
}

void loop() {
  long int now = millis()/1000; // Get elapsed-seconds
  if (now == second) return;    // Only do stuff once per second
  // Update to current second and do chosen menu item
  second = now;
  switch (itemcode) {
  case  0: break;
  case 10: break;
  case 11: break; // Fill in with code for different cases
  case 12: break;
  case 20: break;
  case 21: break;
  case 22: break;
  case 23:    // Item 2, subitem 3, read voltmeter
    volt();
    break;
  case 30: break;
  case 40: break;
  }
  // You can test passnumber to control for cases that only go once
  ++passnumber;
} // end of loop()

Edit: I moved the declaration of second and added second = 0 within menuChanged() to decrease latency between a menu item being selected and its action taking place. Without second being zeroed in menuChanged(), up to a second could pass between an item's selection and its action; latency would average 0.5 seconds, which is undesirable in a user interface.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.