c++ - 收到错误 'setLocked' cannot be used as function,初学者,不确定如何解决此错误

标签 c++ c arduino tinkercad

我是 Arduino 和 tinkercad 的初学者,我不太确定问题出在哪里,但我正在尝试创建一个键盘和 LCD 锁。起初,它说 setlock 没有在范围内声明,但后来我添加了“boolean setLocked”,但后来错误变成了说 setLocked 不能用作函数。任何帮助将不胜感激,请与我交谈,就好像我很愚蠢一样,哈哈,我刚刚开始这门类(class),没有任何先验知识。谢谢!

boolean setLocked;
    #include <LiquidCrystal.h>
    #include <Keypad.h>

    #define redLED 10
    #define greenLED 11

    char* password="1212";
    int positions = 0;

    const byte rows = 4;
    const byte columns = 4;

    char keyMap [rows] [columns] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}  
    };

    byte rowPins [rows] = {1, 2, 3, 4};
    byte columnsPins [columns] = {5, 6, 7, 8};

    Keypad myKeypad = Keypad (makeKeymap(keyMap), rowPins, columnsPins, rows, columns);

    LiquidCrystal lcd (A0, A1, A2, A3, A4, A5);

    void setup() {

      lcd.begin(16, 2);
      pinMode(redLED, OUTPUT);
      pinMode(greenLED, OUTPUT);
      setLocked(true);
    }

    void loop() {

      char whichKey = myKeypad.getKey();

      lcd.setCursor (0,0);
      lcd.print ("Hello");
      lcd.setCursor(0,1);
      lcd.print("Please enter the password");

      if(whichKey == '*' || whichKey =='#'|| whichKey =='A' || whichKey =='B'|| whichKey =='C' || whichKey =='D') {
        positions=0;
        setLocked(true);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("The password is incorrect");
        delay(500);
        lcd.clear();    
        }

        if(whichKey == password [positions]) {
          positions ++;
          }

          if(positions == 4){
            setLocked(false);
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("!Correct!");
            delay(3000);
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("End Arduino");
            lcd.setCursor(0, 1);
            lcd.print("Thank you for trying me out")
            delay(5000);
            lcd.clear();     
            }
            delay(100);
    }        
    void setLocked(int locked)
      if(locked){
        digitalWrite(redLED, HIGH);
        digitalWrite(greenLED, LOW);
        }     
        else{
        digitalWrite(redLED, LOW);
        digitalWrite(greenLED, HIGH);
          }
    }

最佳答案

你的编译器是对的。 setLocked未声明。如果你在你的草图中添加一个函数(从上到下)之后 你调用它,你必须在顶部为函数添加一个原型(prototype):

void setLocked(int locked);

现在您可以在草图中的任何地方使用该功能。

关于c++ - 收到错误 'setLocked' cannot be used as function,初学者,不确定如何解决此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62317015/

相关文章:

c++ - vector 中唯一元素的数量

c++ - MFC 单文档应用程序用户输入

c++ - atmega328P+ENC28J60运行12小时卡死

java - 在安卓中设置进度

c++ - 调用常量复杂度 O(1) 5 行函数会显着影响性能,为什么?

C++ 读取文件

c++ - TextureCache::addImageAsync 选择器类型转换问题

c - 为什么两个整数相除得不到 float ?

c - 时域重叠相加如何实现时间拉伸(stretch)?

c - C中错误的多维数组变量增加