arduino uno上的多开关案例问题

时间:2020-04-19 15:24:52

标签: c arduino

我已经使用开关盒制作了一个菜单,所以当用户按下选择按钮时,就会执行功能。

问题: 其中一个功能包含另一个也使用按钮的开关盒,但是当我再次单击按钮时,仅控制菜单,而不控制该功能,即它将加载该功能和第一种情况,但是单击按钮时则什么也没发生。

我想发生的事情: 我希望按钮停止控制菜单并在其他功能中控制开关。仅在调用菜单功能时按钮才应控制菜单(其他功能相同)。

我不确定如何解决这个问题,也不知道是什么问题。

n.b。请不要建议我使用过的那些库以外的任何其他库,而且该功能在菜单外时本身也可以正常工作,所以我知道它可以工作。我也是C语言编程和使用arduino的新手。

n.b。我也尝试过使用嵌套的切换用例无济于事:((尽管我可能一直以一种不好的方式来解决这个问题)。我也愿意使用if语句(或其他适当的方法)(作为最后的选择)但是我更喜欢使用开关盒。

我很高兴能为我提供帮助:O

#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <EEPROM.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();


//variables to decide which buttons are being used when
bool choosing;
bool playing;

//variables to select the users choices, 3 and 2 and the starting numbers displayed
int size_S=4;
int size_M=2;

uint8_t choice=1;
uint8_t oldButtons=0;

char buffer [12];

//function displaying the menu options
void SelectDisplay(int size_S, int size_M){
  lcd.setCursor(0,0);
  sprintf(buffer, "S:%02d M:%02d", size_S, size_M);
  lcd.print(buffer);
  lcd.setCursor (0,1);
  lcd.print((choice==1?"Choose S":"Choose M"));
}

//variables to store user's choice
int choice_S;
int choice_M;

//!!THIS IS THE FUNCTION THAT DOESN'T WORK INSIDE THE MENU
//function for user to select options S and M
int Select(int choice_S, int choice_M){

  uint8_t buttons, changes;

  buttons = lcd.readButtons ();
  changes = oldButtons & (~buttons);

  if (playing==true){

    //calling the menu disply function
    SelectDisplay (size_S,size_M);

      switch (choice){
        case 1: //case S
          if (changes & BUTTON_UP){
            size_S = (size_S == 10 ? 4:size_S+1);
          }else if (changes & BUTTON_DOWN){
            size_S = (size_S == 4 ? 10:size_S-1);
          }else if (changes & BUTTON_RIGHT){
            choice= 2;
          }
          break;
        case 2: //case M
          if (changes & BUTTON_UP){
            size_M = (size_M == 4 ? 2:size_M+1);
          }else if (changes & BUTTON_DOWN){
            size_M = (size_M == 2 ? 4:size_M-1);
          }else if (changes & BUTTON_LEFT){
            choice = 1;
          }
          break;
      }

     if (changes & BUTTON_SELECT){
        choice_S=size_S;
        choice_M=size_M; 
      }

  }
    oldButtons=buttons; 
}

//variable to change the menu page and options
int menu=1;
//function displaying what the menu options
void displayMenu(){

    switch(menu){
      case 0:
        menu=1;
        break;
      case 1:
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(">Play");
        lcd.setCursor(0,1);
        lcd.print(" Characters");
        break;
      case 2:
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" Play");
        lcd.setCursor(0,1);
        lcd.print(">Characters");
        break;
      case 3:
        break;
        //etc..
    }
}

//function for carrying out the user's menu selection
void menuAction (){

    switch (menu){
      case 1://play
        Select(choice_S, choice_M); //!!HERE IS WHERE THE FUNCTION I WANT TO WORK IS CALLED
        break;
      case 2:       
        break;
      case 3:
        break;
      //etc..
    } 
}

//function for navigating the menu
void mainMenu(){
  int navigate = lcd.readButtons ();

  if (playing==true){
    choosing=false;
  }else{
    choosing=true;
  }

  if(choosing==true){

    //if statement to navigate the menu
    if (BUTTON_DOWN & navigate){
      menu++;
      displayMenu();
      delay(200);
    }
    if (BUTTON_UP & navigate){
      menu--;
      displayMenu();
      delay(200);
    }
    if (BUTTON_SELECT & navigate){
      playing=true;
      menuAction();
      delay(200);
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16,2);
  displayMenu();
  choice=1;
  choosing=true;
}

void loop() {
  // put your main code here, to run repeatedly:

  mainMenu();
}

0 个答案:

没有答案