3 wire 4x4 analogue keypad button latching and led flash

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);
}

Hi sharky,

welcome to the arduino-forum.

Your description is too unprecise to be understandable
there are two keywords "latch" and "blink" they mean opposite things.

What do you really want? an LED that is "latched" = stays in a certain mode
or
an LED than blinks = changing its mode all the time

You should post a detailed description of the wanted functionality.
you press a button then what should happen?
You press the same button what should happen?
you press a different button what should happen?

best regards Stefan

Which Arduino board do you use ? The Arduino Uno does not have pin 40.

Your sketch has three things:

  • analog keypad
  • detect when the button is pressed and released
  • blink a led

You could make three sketches for each part or make one sketch and try to separate them as much as possible in the code.

Could you make a sketch that does the first two things ? And print the result to the serial monitor ?

The buttonState and lastButtonState are for a single digital input.
I suggest to store the pressed button (1...16), and compare a new key with that.

get the analog value from the keypad
check which button is pressed, button 1...16
check if the button is a new one or was already pressed
If new one, show message to serial monitor

By the way, I don't like multiple keys connected to a single analog input. If the contacts make less contact or if there is electronic noise, then the wrong key is read. If multiple buttons are pressed, the sketch does not know which key is pressed.

Hello sharkbait-oo-haha
Check your sketch. The sketch didn´t switch the LED off again.
Have a nice day and enjoy coding in C++.

So I've made some progress.
Now when I press the button the LED turns on and stays on, but wont blink. It flashes briefly, if i change the 20ms delay too 200 i can see that is the line that's causing it to flash. I cant get the delay (1000); to work.

What do you really want? an LED that is "latched" = stays in a certain mode
or
an LED than blinks = changing its mode all the time
You should post a detailed description of the wanted functionality.
you press a button then what should happen?
You press the same button what should happen?
you press a different button what should happen?

When the button is pressed I want the LED attached to pin 40 to turn 1 for 1 second and off for 1 second repeatedly.
When I press the same button again I want it to all be off.
When a different button is pressed I want a different LED to flash and to turn off the LED on pin 40

so far I'm stuck at part 1.

I'm using an arduino mega.

Check your sketch. The sketch didn´t switch the LED off again.

Thanks for pointing that out, I've changed

  digitalWrite(ledPin, HIGH);
  delay (1000);
  digitalWrite(ledPin, HIGH);}

To

  digitalWrite(ledPin, HIGH);
  delay (1000);
  digitalWrite(ledPin, LOW);}

Could you make a sketch that does the first two things ? And print the result to the serial monitor ?

Sorry, I'm not sure how to do that.

The buttonState and lastButtonState are for a single digital input.
I suggest to store the pressed button (1...16), and compare a new key with that.

So your saying the buttonState and LastButtonState wont work for a analogue input? I should do something like change the number of the button state instead.

updated code.

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){
  buttonState = 1; {
    
    // 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, LOW);}
  
  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);
}

don't you need a delay both to see the LED on as well as off?

an analog input can be used to read a button using digitalRead() but the pin is typically configured as INPUT_PULLUP and the button connected between the pin and ground

I made a library years ago for this very thing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.