I am trying my first project in Arduino. I am trying to use my Membrane Switch Module to answer simple math prob. like 1 1. in general, I am trying to make my Arduino do the following:
ask for an answer to 1 1 if the answer is correct turn the light on for 30 sec else blink 3 times and ask again...
the result for my code is that it is running endlessly without getting the input of the keypad.
thank you for the help.
I am new to Arduino and for c coding :)
#include <Keypad.h>
char pad[4][4]={
{1,2,3,'A'},
{4,5,6,'B'},
{7,8,9,'C'},
{'*',0,'#','D'}
};
byte rowPins[4]={9,8,7,6};
byte colPins[4]={5,4,3,2};
Keypad customKeypad=Keypad (makeKeymap(pad),rowPins,colPins,4,4);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(10,OUTPUT);
digitalWrite(10,LOW);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("how much is 1 1?");
char answer=customKeypad.getKey();
Serial.println(answer);
delay(10000);
if (answer==2){
digitalWrite(10,HIGH);
delay(30000);
digitalWrite(10,LOW);
delay(1000);}
else {
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
delay(1000);
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
delay(1000);
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
delay(1000);
}
}
CodePudding user response:
See the documentation: https://playground.arduino.cc/Code/Keypad/
getKey
returns the currently pressed key, as you wait 10 seconds between calls to getKey
it's likely that you'll miss your key presses unless you hold the key down. You should use waitForKey
instead.