jshnaidman jshnaidman - 4 months ago
232 0

Keypad code that is bugged

C

SImple Keypad

int i,j;
int col[4];
int row[4];
char buffer[50];
int key[4][4] = { //this is the matrix with ascii value for each key. It goes 123A,456B,789D,*0#D
  {49,50,51,65},
  {52,53,54,66},
  {55,56,57,67},
  {42,48,35,68}
};
void setup() {
  Serial.begin(9600); //code will print the character pressed
  for(i=13;i>=10;i--){
    pinMode(i,INPUT); //columns 0 to 3
    col[j++] = i;
  }
  j=0;
  for(i=9;i>=6;i--){
    pinMode(i,OUTPUT); //rows 0 to 3
    digitalWrite(i,LOW); //set all pins to low from the start
    row[j++] = i;
  }
  sprintf(buffer,"Start\n");
  Serial.print(buffer);
}
void loop() { // Set one row to high and check each column to see if it is high. If both column and row are high, print that key in the 2d array
  for(i=0;i<4;i++){
    digitalWrite(row[i],HIGH);
    for(j=0;j<4;j++){
      if(digitalRead(col[j]) == HIGH){ // ( ==HIGH is for clarity)
        sprintf(buffer,"%c\n",key[i][j]);
        Serial.print(buffer);
//        while(digitalRead(col[j]) == HIGH){ optional to prevent serialprint from spamming console and prevent printing until key is released
//          delay(1);
//        }
      }
    }
    digitalWrite(row[i],LOW); //set it back to low and check next row
  }
}
Comments