magnetic_card_emulator.pde
int leftPin = 10;
int rightPin = 11;
int clockSpeed = 300; 
int clockHalf = 0;
 
void setup() {
  // The interrupts are key to reliable
  // reading of the clock and data feed
  pinMode(leftPin, OUTPUT);
  pinMode(rightPin, OUTPUT); 
  pinMode(13, OUTPUT); // LED
 
}
 
void writeLow()
{
  digitalWrite(leftPin, LOW);
  digitalWrite(rightPin, LOW);
}
 
void writeBit(int lowOrHigh)
{
  if(lowOrHigh == 1)
  {
    if(clockHalf == 0)
    {
      digitalWrite(rightPin, LOW);
      digitalWrite(leftPin, HIGH);
      delayMicroseconds(clockSpeed); 
      digitalWrite(leftPin, LOW);
      digitalWrite(rightPin, HIGH);
      delayMicroseconds(clockSpeed);
      //clockHalf = 1;
    }
    else
    {
      digitalWrite(leftPin, LOW);
      digitalWrite(rightPin, HIGH);
      delayMicroseconds(clockSpeed);
      digitalWrite(rightPin, LOW);
      digitalWrite(leftPin, HIGH);
      delayMicroseconds(clockSpeed);
      //clockHalf = 0;
    }
  }
  else
  {
    if(clockHalf == 0)
    {
      digitalWrite(rightPin, LOW);
      digitalWrite(leftPin, HIGH);
      delayMicroseconds(clockSpeed * 2);
      clockHalf = 1;
    }
    else
    {
      digitalWrite(leftPin, LOW);
      digitalWrite(rightPin, HIGH);
      delayMicroseconds(clockSpeed * 2);
      clockHalf = 0;
    }
  }
}
 
void writeNumber(int numToWrite)
{
  if(numToWrite == 0)
  {
     writeBit(0);
     writeBit(0);
     writeBit(0);
     writeBit(0);
     writeBit(1);
   }
  else if(numToWrite == 1)
  {
     writeBit(1);
     writeBit(0);
     writeBit(0);
     writeBit(0);
     writeBit(0);
  }
  else if(numToWrite == 2)
  {
     writeBit(0);
     writeBit(1);
     writeBit(0);
     writeBit(0);
     writeBit(0);
  }
  else if(numToWrite == 3)
  {
     writeBit(1);
     writeBit(1);
     writeBit(0);
     writeBit(0);
     writeBit(1);
  }
  else if(numToWrite == 4)
  {
     writeBit(0);
     writeBit(0);
     writeBit(1);
     writeBit(0);
     writeBit(0);
  }
  else if(numToWrite == 5)
  {
     writeBit(1);
     writeBit(0);
     writeBit(1);
     writeBit(0);
     writeBit(1);
  }
  else if(numToWrite == 6)
  {
     writeBit(0);
     writeBit(1);
     writeBit(1);
     writeBit(0);
     writeBit(1);
  }
  else if(numToWrite == 7)
  {
     writeBit(1);
     writeBit(1);
     writeBit(1);
     writeBit(0);
     writeBit(0);
  }
  else if(numToWrite == 8)
  {
     writeBit(0);
     writeBit(0);
     writeBit(0);
     writeBit(1);
     writeBit(0);
  }
  else if(numToWrite == 9)
  {
     writeBit(1);
     writeBit(0);
     writeBit(0);
     writeBit(1);
     writeBit(1);
  }
  else if(numToWrite == 10)
  {
     writeBit(0);
     writeBit(1);
     writeBit(0);
     writeBit(1);
     writeBit(1);
  }
  else if(numToWrite == 11)
  {
     writeBit(1);
     writeBit(1);
     writeBit(0);
     writeBit(1);
     writeBit(0);
  }
  else if(numToWrite == 12)
  {
     writeBit(0);
     writeBit(0);
     writeBit(1);
     writeBit(1);
     writeBit(1);
  }
  else if(numToWrite == 13)
  {
     writeBit(1);
     writeBit(0);
     writeBit(1);
     writeBit(1);
     writeBit(0);
  }
  else if(numToWrite == 14)
  {
     writeBit(0);
     writeBit(1);
     writeBit(1);
     writeBit(1);
     writeBit(0);
  }
  else if(numToWrite == 15)
  {
     writeBit(1);
     writeBit(1);
     writeBit(1);
     writeBit(1);
     writeBit(1); 
  }
}
 
void loop(){
  digitalWrite(13, HIGH);
  for(int i = 0; i < 60; i ++)
  {
    writeBit(0);
  }
  writeNumber(11); // start sentinel
  // card data starts here
  writeNumber(1);
  writeNumber(2);
  writeNumber(3);
  writeNumber(4);
  writeNumber(5);
  writeNumber(6);
  writeNumber(7);
  writeNumber(8);
  writeNumber(9);  
  writeNumber(0);
  // card data ends here
  writeNumber(15); // end sentinel
  for(int i = 0; i < 80; i ++)
  {
    writeBit(0);
  }
 
  writeLow();
  digitalWrite(13, LOW);
  delay(300);
}
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki