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