Understanding 1X4 Keypad code

Hello,

I’ve been trying to modify a code example for a 4X4 keypad I found here<LINK_TEXT text=“introduction ----> https://www.youtube. … m/7aqbptxp”>introduction ----> https://www.youtube.com/watch?v=URO042VrCKU
Schematic here ----> Imgur: The magic of the Internet Code here ----> String keys="123A456B789C*0#D";int key;boolean key_lockout=false;void se - Pastebin.com</LINK_TEXT> I have managed to modify the 4X4 keypad code and it’s working good to operate my 1X4 keypad but I’m a little puzzled as to how it is working. I have no idea how this or for that matter any strings code work?

This is the orginal code I started with…

String keys="123A456B789C*0#D";
int key;
boolean key_lockout=false;
 
void setup(){
   Serial.begin(9600);
}
 
void loop(){
  key=getKeypad();
  if(key!=-1)
      Serial.println(keys[key]);
   delay(10);
}
 
int getKeypad(){
  int ret=-1;
  boolean reset_lockout=false;
  if(analogRead(A0)==0)
    key_lockout=false;
  else if(!key_lockout){
    delay(20);
    ret=15-(log((analogRead(A0)-183.9)/58.24)/0.1623)+0.5;
    key_lockout=true;
  }
  return ret;
}

Here is my modified code here:

String keys="41*7";
int b;

void setup(){
   Serial.begin(9600);
}

void loop(){

  int keypadValue = analogRead(A0);

  if (keypadValue >= 500 && keypadValue <= 550){ b=0; 
      Serial.print("Key # 1 keypadValue = ");//pin 1 = 527 524
    while  (keypadValue >= 500 && keypadValue <= 550)
    {if (b==0){Serial.print(keypadValue); Serial.println(" print instructions or whatever here or use as a debounce");
    } keypadValue = analogRead(A0);b=b=1;} delay(100);}  
      
  if (keypadValue >= 850 && keypadValue <= 900){ b=0; 
      Serial.print("Key # 2 keypadValue = "); // 2 = 873 866
    while  (keypadValue >= 850 && keypadValue <= 900)
    {if (b==0){Serial.print(keypadValue); Serial.println(" print instructions or whatever here or use as a debounce");
    } keypadValue = analogRead(A0);b=b=1;} delay(100);}      
      
  if (keypadValue >= 250 && keypadValue <= 300){ b=0;  
  	  Serial.print("Key # 3 keypadValue = ");// 3 = 276 275
    while  (keypadValue >= 250 && keypadValue <= 300)
    {if (b==0){Serial.print(keypadValue); Serial.println(" print instructions or whatever here or use as a debounce");
    } keypadValue = analogRead(A0);b=b=1;} delay(100);}  	                        
  	                        
  if (keypadValue >= 325 && keypadValue <= 375){ b=0;   
  	  Serial.print("Key # 4 keypadValue = ");// A = 358 357
    while  (keypadValue >= 325 && keypadValue <= 375)
    {if (b==0){Serial.print(keypadValue); Serial.println(" print instructions or whatever here or use as a debounce");
    } keypadValue = analogRead(A0);b=b=1;} delay(100);}
}

The above code is working great on my 1X4 keypad. Can anyone here give me a breakdown on how it is working? And also I’d be interested in anyone’s opinion if they think it could be reliable as is? I have tested it for at least a couple of hours of pressing buttons and viewing it on serial monitor and it hasn’t had a hiccup yet.

Another question is how do I post pictures on this forum, there doesn’t seem to be a spot here to do that otherwise I’d post the schematics for my 1X4 keypad.

Thanks

jessey

Here is the schematics and circuit board I did for the 4X4 keypad. It turns out this same schematics also works for a 3X4 keypad using only 7 pins without even having to modify the code.

And here is the schematics and circuit board I did for the 1X4 keypad. This is the same circuit board as the one used for the 4X4 keyboard but only using the first 5 pins. I made 4 of these boards to have a few around. You can use the same board for the 1X4 keypad but the code has to be modified because the keypadValues are different (don’t know why though?) and need to be checked, noted and changed in the code.

Thanks

jessey

Can anyone here give me a breakdown on how it is working?

Pressing a key forms a voltage divider, and the resulting voltage is read by analogRead(). The resistive divider, and thus the voltage is different for every key.

Thanks jermington,

I know about the voltages produced and how that works now but as far as I was told it’s not the voltages we should be concerned about but it’s the actual value of the analog read that we want to find. I was using this code below to get the values I needed to address a key press and I found out it was the wrong way. I was using:

  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage); //

And I was using the result of the voltage produced from the above code to put into my if statement to determine which key was being pressed with the code below.

  if (voltage >= 2.25 && voltage <= 2.45){ Serial.print("Key # 5 Volts = ");} // key 5 = 2.35 volts
  if (voltage >= 2.05 && voltage <= 2.25){ Serial.print("Key # 6 Volts = ");} // key 6 = 2.15 volts
  if (voltage >= 1.90 && voltage <= 2.10){ Serial.print("Key # B Volts = ");} // key B = 2.00 volts
  if (voltage >= 1.68 && voltage <= 1.83){ Serial.print("Key # 7 Volts = ");} // key 7 = 1.73 volts
  if (voltage >= 1.60 && voltage <= 1.69){ Serial.print("Key # 8 Volts = ");} // key 8 = 1.64 volts
  if (voltage >= 1.50 && voltage <= 1.58){ Serial.print("Key # 9 Volts = ");} // key 9 = 1.54 volts
  if (voltage >= 1.42 && voltage <= 1.50){ Serial.print("Key # C Volts = ");}

And although the code seemed to be working fine when the buttons were pressed the guys on the arduino forum told me its not the correct way of doing it and I quote:

UKHeliBob

You don’t care what the actual voltage is. What you need to know is what is returned by analogRead()

So now I’m using the code below to find the value of the anolog read on pin A0:

  int keypadValue = analogRead(A0);
   lcd.setCursor(0, 0);//-------------//keypadValue 
   lcd.print(keypadValue);
   lcd.setCursor(3, 0);
   lcd.print(" Anolog pinA0");//---// 
   lcd.setCursor(0, 1);//-------------//

And as far as I know that’s the proper way of doing it, correct me if I’m wrong.

So now what I’m looking for here is to get a bit of a tutor on how the code I modified for the 1X4 keypad works. I’d like to know because I completely don’t understand how the original code below here worked:

String keys="123A456B789C*0#D";
int key;
boolean key_lockout=false;
 
void setup(){
   Serial.begin(9600);
}
 
void loop(){
  key=getKeypad();
  if(key!=-1)
      Serial.println(keys[key]);
   delay(10);
}
 
int getKeypad(){
  int ret=-1;
  boolean reset_lockout=false;
  if(analogRead(A0)==0)
    key_lockout=false;
  else if(!key_lockout){
    delay(20);
    ret=15-(log((analogRead(A0)-183.9)/58.24)/0.1623)+0.5;
    key_lockout=true;
  }
  return ret;
}

And I just started getting rid of bits of the above code and checking to see if it would still function and to my suprise it functions very well with just this code below:```
String keys=“41*7”;

And that one line of code above is all that's left of the original code and combined with the code I wrote it seems to be working good.

What is a string and how is that code working? Can anyone here put it into context that a person with very limited programming skills can understand? If this forum is a place for discussion of code then I'm all ears...

Thanks

jessey

UKHeliBob

You don’t care what the actual voltage is. What you need to know is what is returned by analogRead()

Technically correct, but there is a direct and simple relationship between the voltage and the analog readout. It is easier to just use the analog readout.

I suggest to avoid using Strings with Arduino. They tend to cause hard-to-solve problems because they make very inefficient use of small memory spaces. Everything you do with Strings you can do with character arrays (“C strings” with lower case s) and the built in string utility functions. And the result is faster and takes less program space. For one C string tutorial, see http://www.cplusplus.com/reference/cstring/

Thanks for getting back to me jermington. Like I said I’m a novice with very little programming experience and I was looking for an explanation on how this one line of code is working? The one line I’m talking about is this one below.```
String keys=“41*7”;

I started going through the tutorial you sent me a link to and thanks for that. I'll continue on with the tutorial a bit at a time as time permits. In the meantime I am really interested in hearing how the program I modified works, specifically that one line of code above.

Thanks

jessey

That one line of code defines a variable named keys, of type String, which contains the ASCII characters 4, 1, * and 7.