Arduino Uno,4x4键盘继电器

时间:2017-03-15 07:21:22

标签: arduino arduino-uno

#include <Keypad.h>

const byte numRows = 4;
const byte numCols = 4;

int relePin = 10;

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

byte rowPins[numRows] = {9, 8, 7, 6};
byte colPins[numCols] = {5, 4, 3, 2};

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);


void setup() {

  Serial.begin(9600);

  pinMode(relePin, OUTPUT);
}

void loop() {

  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY);
  {
    Serial.print(keypressed);
  }
}
{
  char key = keypad.getKey();
  if (int(key) == 1) {
    digitalWrite(relePin, LOW);
    delay(1000);
    digitalWrite(relePin, HIGH);
    delay(1000);
  }
}

keypad_til_rele:37:错误:在&#39; {&#39;之前预期的不合格ID令牌

{

^

退出状态1 在{&#39;之前预期的不合格身份令牌

错误在哪里? :/ 我不知道它在哪里? 我是ardiuno的新手

1 个答案:

答案 0 :(得分:0)

你有两个额外的大括号}{过早地关闭你的主循环。您还没有在任何地方声明keypad。我在下面评论过它们:

#include <Keypad.h>

const byte numRows = 4;
const byte numCols = 4;

int relePin = 10;

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

byte rowPins[numRows] = {9, 8, 7, 6};
byte colPins[numCols] = {5, 4, 3, 2};

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);


void setup() {

  Serial.begin(9600);

  pinMode(relePin, OUTPUT);
}

void loop() {

  char keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY);
  {
    Serial.print(keypressed);
  }
  //} These two line are your problem
  //{
  //char key = keypad.getKey(); // keypad was not declared anywhere, I assume it was meant to be this:
  char key = myKeypad.getKey();
  if (int(key) == 1) {
    digitalWrite(relePin, LOW);
    delay(1000);
    digitalWrite(relePin, HIGH);
    delay(1000);
  }
}