Go Down

Topic: RFID and LEDs - Help with code? (Read 1 time) previous topic - next topic

Arrch

#5
Jan 11, 2013, 07:27 am Last Edit: Jan 11, 2013, 07:29 am by Arrch Reason: 1
Quote

Thanks Arrch for the reply. How would I go about doing this, if you don't mind talking more in-depth about this?


Example code that takes an ASCII string from a serial device such as "<1234>" and converts it into the numerical 1234.

Code: [Select]
/*
* Example for Serial2Int
* When reading from the serial monitor, there are two important things to note:
* (1) Bytes are read one at a time. So when sending "246", will be read by your
* code as '2', then '4', then '6'. If you want to identify them as related in some
* way, you need a way to determine that. This example uses start and stop bytes.
* (2) Sending a number through the monitor sends it's ASCII representation, not
* the value itself. So typing 3 and hitting enter would send '3' or 51 as per the
* ascii table. To account for this, we will be using atoi(), which takes a null
* terminated array of chars, also known as a string, and produces the int equivalent.
*/

// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';

// Maximum characters in an int + null terminated character
const int maxBuffer = 6;

void setup() {
 Serial.begin(115200);
 Serial.println("[Serial2Int]");
}

void loop() {
 // Stores the characters between the start and stop bytes
 static char buffer[maxBuffer];
 // Keeps track of spot in buffer
 static int index=0;
 
 if (Serial.available() > 0 ) {
   char inChar = Serial.read();
   
   if (inChar==startByte) { // If start byte is received
     index=0; // then reset buffer and start fresh
   } else if (inChar==stopByte) { // If stop byte is received
     buffer[index] = '\0'; // then null terminate
     processData(buffer); // and process the data
     index=0; // this isn't necessary, but helps limit overflow
   } else { // otherwise
     buffer[index] = inChar; // put the character into our array
     index++; // and move to the next key in the array
   }
   
   /* Overflow occurs when there are more than 5 characters in between
    * the start and stop bytes. This has to do with having limited space
    * in our array. We chose to limit our array to 5 (+1 for null terminator)
    * because an int will never be above 5 characters */
   if (index>=maxBuffer) {
     index=0;
     Serial.println("Overflow occured, next value is unreliable");
   }
 }
}

void processData(char buffer[]) {
 unsigned int value = atoi(buffer); // convert string to int
 Serial.print("Value: ");
 Serial.println(value);
}

ch_91

Once again, thanks for the replies.

I've gotten this code which I was able to get working with a single LED but when i'm not sure how to get it working with my piranha RGB LED.

Code: [Select]
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int RFID = 0;

//Register your RFID tags here
char tag1[13] = "67005DC651AD";
char tag2[13] = "67005DB143C8";


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

  pinMode(RFID, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop(){

  char tagString[13];
  int index = 0;
  boolean reading = false;

  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 ++;
    }
  }

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  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
    turnoffLED(2);

  }else if(compareTag(tag, tag2)){ //if matched tag2, do this
    turnoffLED(3);


  }else{
    Serial.println(tag); //read out any unknown tag
  }

}

void turnoffLED(int pin){

//Turn on LED on pin "pin" for 250ms

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

void resetReader(){  // Reset the RFID reader to read again

  digitalWrite(RFID, LOW);
  digitalWrite(RFID, 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
}



So what I would like to happen is both of the tags will be set up to match with each one of the LED colours.

Then one of the two colours (red (RGB 255, 255, 0) or blue (RGB 0, 255, 255) will turn on at a random and then you put the RFID tag on which will turn on and then it will restart and another colour will come at random. For example if the colour red came on and then it was linked to tag 1, then you would need to place that tag on and then the LED will turn.

I'm hoping this does not sound too confusing and would be gratefully for help. also I am new to Arduino, can you tell? lol.

Cheers!

PaulS

That code does something. What does it do? Does it turn the LED on or off?

ash901226

PaulS i think that what the op wanted to do is to let a led light up in random which could me one of 2 which is red or blue, so when the red led light up, one of the rfid will turn the led off while the will not.

PaulS

Quote
PaulS i think that what the op wanted to do is to let a led light up in random which could me one of 2 which is red or blue, so when the red led light up, one of the rfid will turn the led off while the will not.

Probably. But, does that code correctly read the RFID tag? Does it correctly compare the tag read to the other two tags? What problem are we trying to solve?

If all that need to change is what action happens when tag 1 or tag 2 is scanned, then that is a trivial change. If collecting the data from the RFID is not working correctly, then the changes needed to the code are far more extensive. Where do we need to start?

Go Up