Binary Punch Card Reader Code Issue

Hi guys, i’m not a massively experienced Arduino user and have been having some problems with a project. I’m hoping someone could point me in the right direction for this.

I’ve been trying to build a binary punch card reader similar to the one seen here: Binary Punched Card Reader | Hackaday.io

However I can’t quite get it to work correctly. The one that i have built only has two pins for the moment but i will add more once i get these two to work.

Here’s an image of my punch card reader and a punch card: Punch Card Reader Punch Card

On my Arduino, pins 8 and 9 are connected to the paperclip switches with pull up resistors activated, and the block on the other side is connected to ground. The blue LED powers on when the switches are closed, and if either of the switches stop making contact then the green LED turns on.

I’ll put up a link to a short youtube clip that shows you what i am trying to recreate as it shows it better than i could ever explain. Binary Punched Card Reader test - YouTube

With the code, I am trying to get it to wait for the card to be inserted (paperclip switches are open) and then wait for one of the switches to close, signalling the first reading. Both switches will then open again as the punch card is pulled through to the next set of holes. It should add a 1 to the appropriate array (pin8 array or pin9 array) if there is a hole and add a 0 if there is no hole. The plan is then to convert each 8 bit array to a letter;

int code8[8];
int code9[8];
void setup() { 
  pinMode(8, INPUT);
  pinMode(9, INPUT);  
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);            
  digitalWrite(8, HIGH);    // Connect Pull-Up resistor.
  digitalWrite(9, HIGH);     // Connect Pull-Up resistor.
  Serial.begin(9600);  
}

void loop() {
  for (int i = 0; i < 8; i++) {
    code8[i] = 0;
    code9[i] = 0;
  }
  for (int i = 0; i < 8; i++) {
    while ((digitalRead(8) == LOW) and (digitalRead(9) == LOW)) {
      digitalWrite(12, LOW); //blue off
      digitalWrite(13, HIGH); // green on
    } //wait for card to be inserted
    while ((digitalRead(8) == LOW) or (digitalRead(9) == LOW)) {
      digitalWrite(13, LOW); //green off
      digitalWrite(12, HIGH); //blue on
      code8[i] = digitalRead(8);
      code9[i] = digitalRead(9);
    }
  }
  for (int i = 0; i < 8; i++) {
    Serial.print(code8[i]);
  }
  Serial.println("");
  for (int i = 0; i < 8; i++) {
    Serial.print(code9[i]);
  }
  Serial.println("");
}

I can't quite get it to work correctly.

So what does it do ?

You need to debounce your inputs. Look at File->Examples->02Digital->Debounce. As your paper clips transistion from open to closed, you will get several state changes which your code may pick up on, by accident.

You can also have a look at the File->Examples->02Digital->StateChangeDetection sketch since you really only need to react to state changes on your pins