-1

I want to control the 3x3 reed switch at the same time. This my sketch.

enter image description here

and my Arduino code is like this;

const byte rows = 3;
const byte cols = 3;

byte rowPins[rows] = {3, 4, 5};
byte colPins[cols] = {7, 8, 9};

char keys[rows][cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
};


int result[rows][cols] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);


  pinMode(13, OUTPUT);
  for (int i = 0 ; i < rows; i++) {
    pinMode(rowPins[i], OUTPUT); //3,4,5 에서 OUTPUT 을 차례대로 준다음에 7,8,9 에서 읽으면됨 ㅎㅎ 외부 풀다운 구현
    digitalWrite(rowPins[i], LOW); //처음에 0v 를 준다.
  }
  for (int i = 0; i < cols; i++) {
    pinMode(colPins[i], INPUT);   }

}

void loop() {
  char val = '\0';
  // put your main code here, to run repeatedly:

  digitalWrite(13, HIGH);
  for (int i = 0; i < rows ; i++) { //0,1,2 차례대로 output 을 준다.
    digitalWrite(rowPins[i], HIGH); // 처음에 3번핀에서만 5V를 준다

    for (int j = 0 ; j < cols ; j++) {
      if (!digitalRead(colPins[j])) {
        result[i][j] = 0;
      }
      else {
        val = keys[i][j];
        Serial.println(val);
        result[i][j] = 1;
      }
    }
    digitalWrite(rowPins[i], LOW); //다시 잠가줌

  }


  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3 ; j++) {
      Serial.print(result[i][j] );
    }
    Serial.println("");
  }
  Serial.println("-----------------------------------");
  delay(500);

}

it seems like working very well. But I figured out that when I placed my magnetic in a columne like this 1 0 0 1 0 0 0 0 0 originally I want to my computer shows me like that. but it only shows me all 0.

if I placed it like this M 0 0 0 M 0 0 0 0 It shows me 1 0 0 0 1 0 0 0 0 it works perfectly. If you know the problem plz let me know

Daniel
  • 23
  • 8
  • The schematic is not understandable. I do not see if there could be a short circuit when you close 2 switches. Could you draw something like that : https://www.google.fr/search?q=arduino+key+matrix+schematic&rlz=1C1GGRV_frFR776FR776&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi57IK_rqPaAhVRKlAKHdz8ClMQ_AUICigB&biw=2133&bih=1054#imgrc=eCM-ohVPsSHj7M: – Julien Apr 05 '18 at 14:40
  • @Julien Okay I will post is ASAP thank you ! – Daniel Apr 06 '18 at 00:08

1 Answers1

0

Based on the schematic, you have the resistors on the columns instead of the rows. You need to pull each row low and monitor for a high if the switch is closed. In addition, you can't keep the non-active columns as outputs; instead of switching them from high to low, you need to change them to inputs. If you don't, you can easily create short circuits. Lastly, by setting them to inputs, you can now detect multiple closed switches in both the rows and columns.

Joe Thomas
  • 325
  • 1
  • 7