[SOLVED] Quick code debug

Hello,

I have two codes for a one-wire analog keypad input (4x4 matrix turned into 1 input). The code that came along with it works just fine in the serial monitor. It inputs exactly the keys I have pressed without skipping a beat. A fellow member helped me implement it into my project. The issue is that it throws 1’s a lot all on its own. Like I said, the original code works fine, so I don’t believe there is with that, but he even said that the code might be buggy. Any help would be greatly appreciated. The project will not work if i cannot enter the right passcode due ti random 1’s being thrown into the mix. Thanks in advance.

Here is the original code:

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 here is the code I need it to stop throwing 1’s in:

#include <LiquidCrystal.h>
#include <Servo.h>
String keys="123A456B789C*0#D";
int key;
boolean key_lockout=false;
int button1 = 7;
int press1 = 0;
int pos = 90;
int led = 8;
int led2 = 9;

Servo servo1;
void setup()
{
  Serial.begin(9600);



  pinMode(button1, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led, OUTPUT);
  servo1.attach(13);
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

char password[]={"1234"};
char enteredString[5]={""};
int stringPos=0;


void loop(){

  key=getKeypad();
  if( (key>-1) && (keys[key]!='*') && (keys[key]!='#') )
  {
   //save current char
   enteredString[stringPos++]=keys[key];
   //terminate string
   enteredString[stringPos]=0;
   //prevent writing past end of string
   stringPos -= (stringPos > 3);
   Serial.print(keys[key]);

   delay(10);
  }
  
 
 
 
  if (keys[key] =='#')
  {//reset ready for next attempt
    Serial.println("");
    stringPos=0;
    if ( strcmp(enteredString, password)==0) 
      {digitalWrite(led, HIGH);
        Serial.println("Quick, it's open");
       servo1.write(0);
       delay(5000);
       servo1.write(90);
       digitalWrite(led, LOW);
       digitalWrite(led2, HIGH);
    delay(1000);
    digitalWrite(led2, LOW);
      }
    else
    digitalWrite(led2, HIGH);
      Serial.println("GO AWAY!");
      delay(1000);
      digitalWrite(led2, LOW);
    }
    press1 = digitalRead(button1);
    if (press1==LOW)
    {
    digitalWrite(led, HIGH);
    servo1.write(0);
    delay(5000);
    servo1.write(90);
    digitalWrite(led, LOW);
    digitalWrite(led2, HIGH);
    delay(1000);
    digitalWrite(led2, LOW);
  }
      
}

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

Thanks again

I would suspect some noise on the analog input. Try changing:

if(analogRead(A0)==0)

to

if(analogRead(A0)<2)

If that helps but does not fix the problem, try changing ‘2’ to a higher number until the problem goes away. If that number gets too high you won’t be able to use the 1 key and the problem is more likely a physical problem with the 1 key on the keypad.

johnwasser:
I would suspect some noise on the analog input. Try changing:

if(analogRead(A0)==0)

to

if(analogRead(A0)<2)

If that helps but does not fix the problem, try changing ‘2’ to a higher number until the problem goes away. If that number gets too high you won’t be able to use the 1 key and the problem is more likely a physical problem with the 1 key on the keypad.

Thank you so very much! Turns out 7 was the lucky number, I truly appreciate the help!!

Is there something in the air today? Why all the cross-posts? It just makes work while we deal with them, when we could be answering your questions.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Other thread deleted.

  • Moderator

Nick, I think you deleted the wrong post, so now I have to repost it. Which post were you talking about?

This one:

Hello,

I am trying to print out my keypad presses onto my lcd screen. I have everything else working properly, but this part. When I press a key, it prints (both on the serial monitor and lcd), but the next keypad just changes the number on the lcd (but not the serial monitor). I want to view the whole string of keys before I press the open button (# in my case). Any suggestions?

#include <LiquidCrystal.h>

#include <Servo.h>
String keys=“123A456B789C*0#D”;
int key;
boolean key_lockout=false;

Looks like you have another thread about your LCD:

forum.arduino.cc/index.php?topic=283620

Hello, can anyone help me with my LCD printing issue? The LCD is a SainSmart LCD2004. I have it working ALMOST the way I want to. Basically, I wish to display EVERY keypad press on the LCD. Currently, it is only displaying the character I press, and then overwriting it when I press another. Any help would be greatly appreciated. Thank you.

Is that not a cross post?