So i have this code a 3 by 4 keypad. And i want to change it into a for loop. But i've a hard time understanding how i should do so.
The code is using the buttons 1,2,4 and 5 only.
Just wondering if someone could give a hint as to what i should do!
int col1 = 3;
int col2 = 6;
int row1 = 4;
int row2 = 2;
int reading_row1 = 0;
int reading_row2 = 0;
int pin1 = 8;
int pin2 = 9;
int pin3 = 10;
int pin4 = 11;
void setup()
{
Serial.begin(9600);
pinMode(row1, INPUT_PULLUP);
pinMode(col1, OUTPUT);
pinMode(row2, INPUT_PULLUP);
pinMode(col2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(col1, HIGH);
digitalWrite(col2, HIGH);
//digitalWrite(col3, HIGH);
}
void loop()
{
digitalWrite(col1, HIGH);
digitalWrite(col2, LOW);
reading_row1 = digitalRead(row1);
reading_row2 = digitalRead(row2);
if (reading_row1 == LOW)
{
Serial.println("2");
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
}
else if (reading_row2 == LOW)
{
Serial.println("5");
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
digitalWrite(col1, LOW);
digitalWrite(col2, HIGH);
reading_row1 = digitalRead(row1);
reading_row2 = digitalRead(row2);
if (reading_row1 == LOW)
{
Serial.println("1");
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
else if (reading_row2 == LOW)
{
Serial.println("4");
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}