RFID code for SLVM

Dependencies:   ID12RFID mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ID12RFID.h"
00003 
00004 ID12RFID rfid(p27); // uart rx
00005 Serial pc(USBTX,USBRX);
00006 
00007 DigitalOut interrupt(p23);
00008 DigitalOut MSB(p21);
00009 DigitalOut LSB(p22);
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut led3(LED3);
00013 
00014 float speedModifiers[] = {
00015     1.0, 0.7, 0.5, 0.3
00016 };
00017 
00018 int speedTags[] = {
00019     5452129,
00020     5449063,
00021     36902518
00022 };
00023 
00024 volatile int tag = 0;
00025 volatile bool tagRead = false;
00026 
00027 int intCorrection;
00028 
00029 void readCard();
00030 void checkTag();
00031 
00032 void readCard() {
00033     // First, disable interrupts to allow critical sections to run through
00034     NVIC_DisableIRQ(UART2_IRQn);
00035     //led3 = !led3;
00036     if(rfid.readable()) {
00037         tag = rfid.read();
00038         tagRead = true;
00039         bool match = false;
00040         int length = (sizeof(speedTags)/sizeof(speedTags[0]) < sizeof(speedModifiers)/sizeof(speedModifiers[0])) ? (sizeof(speedTags)/sizeof(speedTags[0])) : (sizeof(speedModifiers)/(sizeof(speedModifiers[0])));
00041         for (int i=0; i<length; i++) {
00042             if (speedTags[i] == tag) {
00043                 printf("RFID Tag number : %d\nSpeed now at %d\n", tag, ((int)(speedModifiers[i]*100)));
00044                 match = true;
00045                 intCorrection++;
00046             }
00047         }
00048     
00049         // For some reason, this function is entered twice each time an RFID tag is read.
00050         // This bit here is to detect that case and just ignore it, while still catching
00051         // other cases and displaying a warning
00052         if (!match and intCorrection == 0) {
00053             printf("[ WARNING ] Tag number %d not recognized\n", tag);
00054         }
00055         else if (!match == 1) {
00056             intCorrection = 0;
00057         }
00058     }
00059     
00060     NVIC_EnableIRQ(UART2_IRQn);
00061 }
00062 
00063 void checkTag() {
00064 }
00065 
00066 int main() {
00067     interrupt=0;
00068     rfid.setInterrupt(&readCard);
00069     printf("Entering main loop\n");
00070     while(1) {
00071         if (tagRead) {
00072             checkTag(); 
00073         if(tag==5452129 || tag==5449063) LSB=1;
00074         else LSB=0;
00075         if(tag==5449063 || tag==36902518) MSB=1;
00076         else MSB=0;
00077         interrupt=1;
00078         led1=MSB;
00079         led2=LSB;
00080         led3=interrupt;
00081         wait(.5);
00082         interrupt=0;  
00083         led3=interrupt;      
00084         tagRead = false;}
00085         //led1 = 1; wait(0.5);
00086         //led1 = 0; wait(0.5);
00087         wait(.1);
00088     }
00089 }