Hi.
I bought my first Arduino Duemilanove earlier this week and I am really impressed of it. I have no experience of microprocessorprogramming, I usually program Vb.NET applications and i know some c/c++.
My first project is to make Arduino work with a MagstripeReader and a Matrix KeyPad. I found two sketchfiles on this forum but I can't make them work together. I have tested them separatly and they work perefect. I put them together so I don't have double of setup() and loop().
The Magstripe Sketch:
/*
* Magnetic Stripe Reader
* by Stephan King http://www.kingsdesign.com
*
* Reads a magnetic stripe.
*
*/
int cld1Pin = 5; // Card status pin
int rdtPin = 2; // Data pin
int reading = 0; // Reading status
volatile int buffer[400]; // Buffer for data
volatile int i = 0; // Buffer counter
volatile int bit = 0; // global bit
char cardData[40]; // holds card info
int charCount = 0; // counter for info
int DEBUG = 0;
int Play = 0;
void setup() {
pinMode(8, OUTPUT); // set a pin for buzzer output
Serial.begin(9600);
// The interrupts are key to reliable
// reading of the clock and data feed
attachInterrupt(0, changeBit, CHANGE);
attachInterrupt(1, writeBit, FALLING);
buzz(8, 5100, 100); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
buzz(8, 5300, 100); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
buzz(8, 5500, 80); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
Play = analogRead(2); // read the value from the sensor
}
void loop(){
// Active when card present
// Active when card present
while(digitalRead(cld1Pin) == LOW){
reading = 1;
}
// Active when read is complete
// Reset the buffer
if(reading == 1) {
if (DEBUG == 1) {
printBuffer();
}
decode();
reading = 0;
i = 0;
int l;
for (l = 0; l < 40; l = l + 1) {
cardData[l] = '\n';
}
charCount = 0;
}
noInterrupts();
interrupts();
}
// Flips the global bit
void changeBit(){
if (bit == 0) {
bit = 1;
} else {
bit = 0;
}
}
// Writes the bit to the buffer
void writeBit(){
buffer[i] = bit;
i++;
}
// prints the buffer
void printBuffer(){
int j;
for (j = 0; j < 200; j = j + 1) {
Serial.println(buffer[j]);
}
}
int getStartSentinal(){
int j;
int queue[5];
int sentinal = 0;
for (j = 0; j < 400; j = j + 1) {
queue[4] = queue[3];
queue[3] = queue[2];
queue[2] = queue[1];
queue[1] = queue[0];
queue[0] = buffer[j];
if (DEBUG == 1) {
Serial.print(queue[0]);
Serial.print(queue[1]);
Serial.print(queue[2]);
Serial.print(queue[3]);
Serial.println(queue[4]);
}
if (queue[0] == 0 & queue[1] == 1 & queue[2] == 0 & queue[3] == 1 & queue[4] == 1) {
sentinal = j - 4;
break;
}
}
if (DEBUG == 1) {
Serial.print("sentinal:");
Serial.println(sentinal);
Serial.println("");
}
return sentinal;
}
void decode() {
int sentinal = getStartSentinal();
int j;
int i = 0;
int k = 0;
int thisByte[5];
for (j = sentinal; j < 400 - sentinal; j = j + 1) {
thisByte[i] = buffer[j];
i++;
if (i % 5 == 0) {
i = 0;
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0) {
break;
}
printMyByte(thisByte);
}
}
char nummer1[7];
Serial.print("Stripe_Data:");
for (k = 0; k < charCount; k = k + 1) {
Serial.print(cardData[k]);
if (k > 0 & k < 9){
nummer1[k-1] = cardData[k]; }
}
Serial.println("");
if (strcmp(nummer1, "01550001") == 0){ //& cardData[2] == Value2 & cardData[3] == Value3 & cardData[4] == Value4 & cardData[5] == Value5 & cardData[6] == Value6 & cardData[7] == Value7 & cardData[8] == Value8) {
buzz(8, 5500, 100); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
}
else{
//digitalWrite(ledPin, LOW);
buzz(8, 1500, 100); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
delay(50);
buzz(8, 1500, 100); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds
}
}
void printMyByte(int thisByte[]) {
int i;
for (i = 0; i < 5; i = i + 1) {
if (DEBUG == 1) {
Serial.print(thisByte[i]);
}
}
if (DEBUG == 1) {
Serial.print("\t");
Serial.print(decodeByte(thisByte));
Serial.println("");
}
cardData[charCount] = decodeByte(thisByte);
charCount ++;
}
char decodeByte(int thisByte[]) {
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
return '0';
}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
return '1';
}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 0){
return '2';
}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 0 & thisByte[4] == 1){
return '3';
}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
return '4';
}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
return '5';
}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 1){
return '6';
}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 0 & thisByte[4] == 0){
return '7';
}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
return '8';
}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
return '9';
}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 1){
return ':';
}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 0 & thisByte[3] == 1 & thisByte[4] == 0){
return ';';
}
if (thisByte[0] == 0 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
return '<';
}
if (thisByte[0] == 1 & thisByte[1] == 0 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
return '=';
}
if (thisByte[0] == 0 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 0){
return '>';
}
if (thisByte[0] == 1 & thisByte[1] == 1 & thisByte[2] == 1 & thisByte[3] == 1 & thisByte[4] == 1){
return '?';
}
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
And The Matrix KeyPad Sketch:
// keypad v0.1 (C)2007 Mark Stanley <mstanley@technologist.com>
// An example for testing the matrix keypad driver.
#include <keypad.h>
#define ROWS 4
#define COLS 3
char bufferkey;
keypad kpd = keypad(ROWS, COLS);
void setup()
{
kpd.init();
}
void loop()
{
char key = kpd.get_key();
if(key != '\0')
{
}
}
I have organised the PinSettings for the KeyPad so they don't interrupt with the Magstripe pins.
Do any of you have any suggestions of how I can mix these two sketches?
Regards
Daniel Eriksson