Arduino Nano - RFID - Stopwatch

Thread Starter

khaledelrady

Joined Feb 5, 2009
4
Hi legends,

I'm working on a project that runs a stopwatch based on the below conditions

  • Inputs
    • Day start pushbutton: To start the program
    • Reset pushbutton: To reset the stopwatch counting
    • 0 – 10v Analog input: To start/pause the stopwatch
    • RFID: To authorize the Day start & Reset
  • Outputs
    • Max7219: to drive four common cathode 7 segment for hours and minutes.


The targeted scenario:

  • The operator will scan the RFID and within 5 seconds will press the Day start pushbutton to start the program.
  • The stopwatch will be waiting for the analog input to be less than 10 to start counting.
  • If the value of the analog input is greater than 10, the counting will pause.
  • To reset the count, the operator must scan again the RFID and within 5 seconds will press Reset push button.


I have finished pretty much most of the work, but I need help with the last point to reset the count.
 

Attachments

Thread Starter

khaledelrady

Joined Feb 5, 2009
4
I did some modifications here and the code now seems to work fine, except I have to keep the card on the reader all the time.
Any suggestions?



#include <SPI.h>
#include <MFRC522.h>
#include "DigitLedDisplay.h"
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define bt_start_day A4
#define bt_reset A5
#define DIN 5
#define CS 6
#define CLK 7
DigitLedDisplay ld = DigitLedDisplay(DIN, CS, CLK);
bool stopwatch = false;
bool reset = false;
void writevalue(int, int, int);
int hh=0, mm=0, ss=0, ms=0;
int start_stop = 0;
bool timerStart = false;
const int sec_led = 4;
//int sec_led = 0;
void setup() {
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
/* Set the brightness min:1, max:15 */
ld.setBright(14);
/* Set the digit count */
ld.setDigitLimit(4);
pinMode(sec_led, OUTPUT);
pinMode(bt_start_day, INPUT_PULLUP);
pinMode(bt_reset, INPUT_PULLUP);
if(ss<10){ld.write(2, B01111110);}
if(mm<10){ld.write(5, B01111110);}
if(hh<10){ld.write(8, B01111110);}
noInterrupts(); // disable all interrupts
TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // set timer count for 1khz increments
OCR1A = 1999; // = (16*10^6) / (1000*8) - 1
//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
// turn on CTC mode
TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11); // enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable
}
//==========================================================================
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte, HEX));
}
content.toUpperCase();
if (content.substring(1) == "93 27 0F 1F" || content.substring(1) == "63 7B C9 1B") //change here the UID of the card/cards that you want to give access
{
if(digitalRead(bt_start_day) == 0) stopwatch = true;
if(digitalRead(bt_reset) == 0) reset = true;
else reset = false;
}



//==========================================================================

start_stop = analogRead(A0);
Serial.println(start_stop);
if (start_stop < 10 && stopwatch == true) timerStart = true;
else timerStart = false; // Stop stopwatch
//==========================================================================
if(reset == true){
ms=0, ss=0, mm=0, hh=0; // Reset stopwatch
ld.write(2, B01111110);
ld.write(4, B01111110);
delay(100);
}
ld.printDigit(hh, 2);
ld.printDigit(mm, 0);
if ( (ss % 2) == 0) {digitalWrite(sec_led, 1);}
else digitalWrite(sec_led, 0);

if(timerStart == true){

if(mm<10){ld.write(2, B01111110);}
if(hh<10){ld.write(4, B01111110);}
}
}


//=======================================================================

ISR(TIMER1_COMPA_vect){
if(timerStart == true){ms=ms+1;
if(ms>999){ms=0;ss=ss+1;
if(ss>59){ss=0; mm=mm+1;}
if(mm>59){mm=0; hh=hh+1;}
}
}
}
 
Top