ввод данных с матричной клавиатуры и моргание светодиодом
- Войдите на сайт для отправки комментариев
Чт, 16/12/2021 - 12:10
Добрый день
Хочу заставить моргать светодиод и одновременно и параллельно с этим работать с клавиатурой (в данном случае вводить пароль)
плата wemos d1r1
перепробовал разные варианты-никак не идет,последний вариант был такой
#include <Keypad.h>
const int ledPin = D13;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {D2, D3, D4, D5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {D6, D7, D8}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const String password = "1234"; // change your password here
String input_password;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
input_password.reserve(32); // maximum input characters is 33, change if needed
}
void loop(){
}
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
if(customKey == '*') {
input_password = ""; // clear input password
} else if(customKey == '#') {
if(password == input_password) {
Serial.println("password is correct");
} else {
Serial.println("password is incorrect, try again");
}
input_password = ""; // clear input password
} else {
input_password += customKey; // append new character to input password string
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}я подозреваю, что проблема кроется в функции работы с клавиатурой, но из кучи различных вариантов,к сожалению,заработал нормально только этот,и хотелось бы с ним и заставить заработать светодиод
Читай про время жизни переменных.
В строке 49 переменной нужен атрибут static:
static unsigned long currentMillis = millis();
Ну ещё можно объявить переменную глобально