? KeyPad 4x4 with Single wire connect

Hoi Guys, i am using this code to read an 4x4 keypad matrix, and this works fine, but trying to control 2 LEDs with that fails. Like to Switch one lED on when pressing 8, and switch it off when pressing 9

If i i.e. press 8, the 8 appears in the serial monitor but has no effect to the LED, the same with the 9...

String keys="123A456B789C*0#D";
int key;
boolean key_lockout=false;

const int LED1 = A1;

void setup(){
   Serial.begin(9600); 
   pinMode(LED1,OUTPUT);
}

void loop(){
  key=getKeypad();
  if(key!=-1)
      Serial.println(keys[key]);
   delay(2);

if(key=8) {
 digitalWrite(LED1,HIGH);
}
if(key=9) {
 digitalWrite(LED1,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;
}

Has anybody a hint what is wrong ?

key=8 ... key=9

You are assigning 8 and 9 to key and not comparing them to key. Use == instead

They're characters, you're treating them like actual numbers.

if(key=8) {
 digitalWrite(LED1,HIGH)
}
if(key=9) {
 digitalWrite(LED1,LOW)
}

should be

if(key=='8') {
 digitalWrite(LED1,HIGH)
}
if(key=='9') {
 digitalWrite(LED1,LOW)
}

Hello, replacing the =8 with ==8 made it work, the =='8' won't work.

So, now there is another problem :fearful:

With this code

if(key==4) {
digitalWrite(LED1,LOW);
}
if(key==5) {
 digitalWrite(LED1,HIGH);
}
if(key==8) {
digitalWrite(LED2,LOW);
}
if(key==9) {
digitalWrite(LED2,HIGH);
}

only the LED1 will switch when ever i press the depending buttons (4 and 5), BUT when i press 9 the LED2 switches, but pressing 9 has no effect....4 and 5 is working

I've just noticed that the string ( keys="123A456B789C*0#D"; ) is never used in your sketch, so it's just there as a red herring.

I suspect your getKeypad() function may not be returning what you expect.

Since you are using a one wire keypad, try my library. OnewireKeypad

edit: Its in the playground too, but for some reason, the playground is taking a long time to load.

Here it is. http://playground.arduino.cc/Code/OneWireKeyPad#.UyozdvldWSo

Thx for the link(s), i know them and tried, but IMHO the different wiring let it not work. Here is the schematic i use:

OK I did a quick Excel Spreadsheet to calculate the analog values expected from each key. Assuming the forward voltage on your diodes as being 0.6v I get these values
901,785,682,620,540,496,453,425,365,344,323,308,281,269,256,246

So then taking the midway point between each of those to use as thresholds I come up with the following as a replacement for your getKeypad
(although it returns a char NOT an integer)

Ideally you should just do a test first to see what your ACTUAL analog readings are for each key and adjust accordingly.

//NOTE THIS RETURNS A CHAR NOT AN INTEGER 
//although it still returns -1 if no key is being pressed.
char getKeypad()
{
int threshold[16]={843,733,651,580,518,474,439,395,354,333,315,294,275,262,251,123};
char chars[17]="123A456B789C*0#D";
int v=analogRead (A0);
for(int n=0;n<16;n++)
  {
   if (v > threshold[n])
     return chars[n];
  }
return -1;
}

You can also do the calculations on the fly too.

Made quite a few edits. i’m done now.

char *_Data ="123A456B789C*0#D";

long 
  _R_row[4] = {0,1000,2200,3300},
  _R_col[4] = {0,220,470,680},
  _PD = 1500;

unsigned long startTime = millis(), debounceTime = 50;
bool _diode = true;

...

char Getkey()
{
  if ( int Reading = analogRead(A0))
  {
    if (millis() - startTime >= debounceTime)
    {
      for ( byte i = 0, R = _Rows - 1, C = _Cols - 1; i < SIZE; i++ )
      {
        float V = ((5.0f * float(_PD)) / (float(_R_row[R]) + float(_R_col[C]) + float(_PD)) ) - (0.7f * byte(_diode));
        float Vfinal = V * (1023.0f / 5.0f);

        if (Reading <= long(Vfinal + 1.9f))
          return _Data[(SIZE - 1) - i];

        if ( C == 0 )
        {
          R--;
          C = _Cols - 1;
        }
        else C--;
      }
      startTime += debounceTime;
    }
  }
}