Hi, I'm pretty new to Arduino. I'm trying to figure out how to use a 3 pin analogue keypad to blink a led. I'm stuck on trying to get the keypad to "latch" when a button is pressed.
The keypad puts out a resistor value that changes depending on which button is pressed.
I'm using this keypad and I've used some code already to determine the correct resistance value for each of the keys. Robotdyn 4x4 analogue keypad
also, right now If I change the ledState from 0 to 1 manually in the code then upload it, the LED stays lit and doesn't blink.
Thanks for any help/explanations you can provide.
const int ledPin = 40; // the pin that the LED is attached to
int analogPin = A1; // the pin that the keypad is attached to
int val = 0;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int ledState = 0; // remember current led state
void setup() {
pinMode(analogPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT); // initialize the button pin as a output
}
void loop() {
val = analogRead (analogPin);
//check resistor value of keypad
if (val >= 845 and val <= 890){
if (buttonState != lastButtonState) {
// change the state of the led when someone pressed the button
if (buttonState == 1) {
if(ledState==1) ledState=0;
else ledState=1;
}
// remember the current state of the button
lastButtonState = buttonState;
}
}
// turns LED on if the ledState=1 or off if the ledState=0
if (ledState == 1){
digitalWrite(ledPin, HIGH);
delay (1000);
digitalWrite(ledPin, HIGH);}
// adding a small delay prevents reading the buttonState to fast
// ( debouncing )
delay(20);
}