Pages: [1]   Go Down
Author Topic: Help in programming Up / Down Counter  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good Day Everyone;

I am working one making up / down counter using two RFIDs (https://www.sparkfun.com/products/8419) and LED Matrix  (https://www.adafruit.com/product/555) I did successfuly program the first RFID and Led matrix and I am stick in connecting the second one and make the down counter could someone guide me on that. The code as below:

Code:
#include "HT1632.h"

#define DATA 2
#define WR   3
#define CS   4
#define CS2  5

int scrwrt =0;
int newtag =0;
// use this line for single matrix
HT1632LEDMatrix matrix = HT1632LEDMatrix(DATA, WR, CS);

int RFIDResetPin = 13;

//Register your RFID tags here
char tag1[16] = "50008FF398B4";
char tag2[16] = "5000B98D6206";

void setup(){
  Serial.begin(9600);
//Serial1.begin(9600);

  matrix.begin(HT1632_COMMON_16NMOS); 
  matrix.fillScreen();
  delay(500);
  matrix.clearScreen();


  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  matrix.print("on..");
  matrix.writeScreen();

}

void loop(){
  char tagString[13];
  int index = 0;
  boolean reading = false;

  if (scrwrt > 0)
  {
  matrix.clearScreen();
  matrix.setTextSize(1);    // size 1 == 8 pixels high
  matrix.setTextColor(1);   // 'lit' LEDs
  matrix.setCursor(0, 0);   
 
  matrix.writeScreen();
  if (scrwrt==1)
  {
  matrix.print("no");
  matrix.writeScreen();
  matrix.setCursor(0, 8);   // next line, 8 pixels down
  matrix.writeScreen();
  matrix.print("ident");
  matrix.writeScreen();
  }
  if (scrwrt==2)
  {
  matrix.print("emp:");
  matrix.writeScreen();
  matrix.setCursor(0, 8);   // next line, 8 pixels down
  matrix.writeScreen();
  matrix.print(tagString);
  matrix.writeScreen();
  }
  if (scrwrt==3)
  {
  matrix.print("stu:");
  matrix.writeScreen();
  matrix.setCursor(0, 8);   // next line, 8 pixels down
  matrix.writeScreen();
  matrix.print(tagString);
  matrix.writeScreen();
  }
 
  scrwrt=0;
  }

clearTag(tagString); //Clear the char of all value

  while(Serial.available()){

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
    scrwrt=1;
  }

  checkTag(tagString); //Check if it is a match
 
  resetReader(); //eset the RFID reader
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1)){ // if matched tag1, do this
    lightLED(2);
Serial.print("studant : ");
scrwrt =2;
  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    lightLED(3);
Serial.print("employee : ");
scrwrt=3;
  }

else{
  Serial.print("unknown : ");
  }
Serial.println(tag);
}

 
void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
  Serial.println(pin);

  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You have done quite some good work, still some remarks to improve coding style.

remove the bulk of code from loop by defining 2 functions  e.g. updateDisplay()  and  checkSerial(). loop could become something like:
Code:
#define WAINTING 1
#define READING 2
etc

int state
void setup()
{
  ...
  state = WAITING
}
void loop()
{
  switch(state)
  {
    case WAITING:
      if (cardDetected()) state = READING;
      break;
    case READING:
      readSerial();
      if (strlen(input) == 12) state = READ;
      break;
    case READ:
      if (checkTag(input, tag1)) state = EMP;
      if (checkTag(input, tag2)) state = STU;
      break;
    case EMP:
      increase(var);
      state = WAITING;
      break;     
    case STU:
      decrease(var);
      state = WAITING;
      break;
  }   
  updateDisplay(state);
}

other functions
in the code above the loop is mostly controlling the state machine.
 
 

 
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: