未在此范围内声明“ updateMenu”

时间:2019-07-05 10:34:45

标签: c++ arduino

  1. 我从youtube引用此代码,当我运行它时,定义了updateMenu。但是当我更改它时,突然没有定义
  2. 我从此链接https://www.youtube.com/watch?v=DuAG98P9Seo中获取了代码
  3. 实际的人只使用带按钮的LCD显示屏,我想添加一个键盘来修改代码
  4. 可以看到错误说
      在此范围内未声明

    'updateMenu'   enter image description here


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

int upButton = 10;
int downButton = 11;
int selectButton = 12;
int menu = 1;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

void loop() {
  char key = keypad.getKey();

  if (key != No_Key){
    Serial.print(key);
  }
  {
  if (!digitalRead(downButton)){
    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(">Find Existing File");
      lcd.setCursor(0, 1);
      lcd.print(" Select File");
      break;
    case 2:
      lcd.clear();
      lcd.print(">Custom Setting");
      lcd.setCursor(0, 1);
      lcd.print("Create/Delete File");
      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;
  }
}

void action1() {
  lcd.clear();
  lcd.print(">Select File 0 to 99");
  delay(1500);
}
void action2() {
  lcd.clear();
  lcd.print(">Create File");
  delay(1500);
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

编译器不了解updateMenu()函数,因此您应该通过简单声明updateMenu()函数来告诉他有关此函数的信息。

要执行此操作,您需要将其放置在以下行:

void updateMenu();

在定义setup()函数之前:

...
void updateMenu();

void setup() {
  Serial.begin(9600);
...

P.S默认情况下,CPP程序员在其代码顶部(紧随#include行之后)声明其功能(如果需要)。