How do I switch a led on and off with same RFID TAG??

This is the code I am using but the led will only switch the led on when I set the val to read '1' ,and not the whole tag. It is something that seemed quite simple but turned out to be very hard for me as I'm new to the Arduino world. please help guys I know you are all very smart.

int ledon = 13; char val = 0; // variable to store the data from the serial port void setup() { Serial.begin(9600); // connect to the serial port

pinMode(ledon, OUTPUT);

} void loop () {

// read the serial port if(Serial.available() > 0) { val = Serial.read();

Serial.write(val); if (val == '1200134123'){ //this is an example tag but only works if i change it to 1 digitalWrite(13, HIGH);}

} }

Thanks so much in advance

Keenbop

if (val == '1200134123'){ //this is an example tag but only works if i change it to 1

The single char in val is NEVER going to match the multi-byte constant you have defined.

What is sending the serial data to the Arduino?

ID 12 inno rfid reader is sending the data through the serial port being picked up on the 0rx pin. digitalWrite(13, HIGH) then lights up the led. if I could use processing to do it would be even better. but I am a novice and don't understand the code very well but I need to figure it out for my honours project. How would I define the tag properly?

thanks for your reply

but I am a novice and don’t understand the code very well but I need to figure it out for my honours project.

Even a novice can use a search field. There has been plenty of code posted to read from a ID 12 reader.

You need to read a number of characters from the reader, and store them in an array. You need to properly NULL terminate the array, and then use strcmp() to see if that array matches the string that is the good tag id. Note that strings are delimited with ", not '.

Do you know how to do it? I have spent about 8 - 12 hours searching back and forward for the answer and I have tried a number of different pieces of code with no luck and I'm sure it will only take another few lines to make it work. Even if I could use one tag to turn led on digitalwrite, high and another to turn it off digitalwrite low, or, else digitalwrite low. I have spent so long on something that seems so so simple. Thanks

There is code on the playground to read from the ID12. http://arduino.cc/playground/Code/ID12

Have you tried this code without alteration? What problems did you have?

Once you can get the tag read, turning on an LED if the tag matches and the LED is not on, or turning off an LED if the tag matches and is on is trivial.

I’m really sorry i just don’t understand what I am supposed to do. I have used that code and I get an output of:

5-byte code: 28 00 18 B4 06
Checksum: 82 – passed.

5-byte code: 28 00 27 A4 A8
Checksum: 3 – passed.

What am I supposed to do with that to switch on a light.

Should say

if(val == ‘82’) {digitalWrite (13, HIGH);}
if(val == ‘3’) {digitalWrite (13, LOW);},

I really can’t make enough sense out of the coding yet to make the necessary alterations. But I’m getting there slowly but surely. I will be taking it on as a hobby when university is not such a chore and a demand. But now I just need a quick fix, Take a quick look at my site and you will see why I need it to work. I have all the other things like doorlock, led dimmer and media control, but I can’t get a simple light to turn on. www.homescan.org. If you want to login just go to homescan.org/login.html . it is just a dummy login function but it will give you an idea why i need the light function. Thanks for taking the time to chat with me I appreciate it.

I have used that code and I get an output of:

5-byte code: 28 00 18 B4 06 Checksum: 82 -- passed.

5-byte code: 28 00 27 A4 A8 Checksum: 3 -- passed.

This tells you several things. First, and most important, it tells you that the hardware works, and that you have a starting point for code that does what you want it to do.

Those values are stored in an array called code. You could create another array, called good, of the same size and type as code. The values to store in that array are 0x28, 0x00, 0x18, 0xB4, and 0x06.

Then create a function that takes two arrays - the scanned tag's data (code) and the tag to match it to (good). Use a for loop to compare the value in the nth position of the scanned array and the good array. If they don't match, return -1. If they do, continue. If you get to the end of the for loop, return +1.

Then, call that function, if the return code is +1, turn the led on. If it is -1, turn the led off.

That way, you can make the LED come on with a "good" tag and go off with a "bad" tag.

It is easy to change the code then to ignore the bad tag, and turn the LED on if the tag is good, and the LED is off, or off if it is on.

Thanks to a guy called Tony Two Eyes plus you and Zoomcat I finally got it working and working well. this is a fantastic piece of code and should be recommended for anybody new to RFID. It makes live so easy and works really well with a servo too. So happy it it done. Now I need to dim them using processing or max msp, I can do it with a piece of code and it works great with max and a slider, but putting it into this code would be a nightmare. Can it be done. Thanks Paul you are a master of the art. This was my final code (I wish everybody posted their final results):

#include <icrmacros.h>
#include <NewSoftSerial.h>


NewSoftSerial rfidSerial =  NewSoftSerial(8,7);  


char code[20];

int val = 0;
int bytesread = 0;
char ledOn1[]  = "1F001AAC16";//1 //Led 1 On
char ledOff1[]  ="1F001A9DB6";//2 //Led 1 Off
char ledOn2[]  = "2800189B40";//3 //Led 2 on
char ledOff2[] = "280018B406";//4 //Led 2 Off


void setup()
{
  Serial.begin(9600); //open serial
  rfidSerial.begin(9600); //open serial
  pinMode ((9), OUTPUT);
  pinMode ((13), OUTPUT);
}

void loop()
{


  val = 0;
  if(rfidSerial.available() > 0) {
    if((val = rfidSerial.read()) == 2) {                  // check for header
      bytesread = 0;
      while(bytesread < 10)
        if( rfidSerial.available() > 0) {

          // read 12 digit code
          val =  rfidSerial.read();
          if(val == 3)
          { // if header or stop bytes before the 10 digit reading
            break; // stop reading
          }

          if(val != 2)
          {
            code[bytesread] = val; // add the digit
            bytesread++; // ready to read next digit
            code[bytesread] = '\0'; // add the NULL
          }
        }

      if(bytesread >= 10)
      {    
        for(int i=0; i<bytesread; i++)
        {
          //remove cause problem with tag
        }

        int tag = FindValidTag(code);
        rfidSerial.print("");//if no text reading is not always right add delay?
        Serial.println(tag);


      }
      while (Serial.available() > 0)
      {
        //pos=map(val,0,12,0,179);
        //myservo.write(val);
        //value *= 10;
        //value = (Serial.read());//
        //delay(100);
      }

      
    }
  }
}

int FindValidTag(char *code)
{
  if(strcmp(code, ledOn1) == 0){
    digitalWrite(9, HIGH);
    return 1;
  }
  else if(strcmp(code, ledOff1) == 0){
    digitalWrite(9, LOW);
    return 2;
  }
  else if(strcmp(code, ledOn2) == 0){
   digitalWrite(13, HIGH);
    return 3;
  }
  else if(strcmp(code, ledOff2) == 0){
      digitalWrite(13, LOW);
    return 4;
  }

  else{
    return 0;
  }
}

I got the dimmer working too with max/msp I’m on a roll tonight, so now I can switch on a light and use Max as brightness control, amazing. so now I have 4 hours sleep until my lecture at 9 tomorrow, but i will sleep very well : ), thanks guys. Here is the code along with the code you will need with processing. it also works with the max patch on this page http://arduino.cc/en/Tutorial/Dimmer, actually the processing code is there too. add my adaptation and you are good to go. It has taken me two weeks to get this all working together.

#include <NewSoftSerial.h>


NewSoftSerial rfidSerial =  NewSoftSerial(8,7);  


char code[20];
int ledPin = 9; 
int val = 0;
int bytesread = 0;
char ledOn1[]  = "1F001AAC16";//1 //Led 1 On
char ledOff1[]  ="1F001A9DB6";//2 //Led 1 Off
char ledOn2[]  = "2800189B40";//3 //Led 2 on
char ledOff2[] = "280018B406";//4 //Led 2 Off


void setup()
{
  Serial.begin(9600); //open serial
  rfidSerial.begin(9600); //open serial

  pinMode ((13), OUTPUT);
  pinMode(ledPin, OUTPUT);
}

void loop()
{byte brightness;
if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);}



  val = 0;
  if(rfidSerial.available() > 0) {
    if((val = rfidSerial.read()) == 2) {                  // check for header
      bytesread = 0;
      while(bytesread < 10)
        if( rfidSerial.available() > 0) {

          // read 12 digit code
          val =  rfidSerial.read();
          if(val == 3)
          { // if header or stop bytes before the 10 digit reading
            break; // stop reading
          }

          if(val != 2)
          {
            code[bytesread] = val; // add the digit
            bytesread++; // ready to read next digit
            code[bytesread] = '\0'; // add the NULL
          }
        }

      if(bytesread >= 10)
      {    
        for(int i=0; i<bytesread; i++)
        {
          //remove cause problem with tag
        }

        int tag = FindValidTag(code);
        rfidSerial.print("");//if no text reading is not always right add delay?
        Serial.println(tag);


      }
      while (Serial.available() > 0)
      {
        //pos=map(val,0,12,0,179);
        //myservo.write(val);
        //value *= 10;
        //value = (Serial.read());//
        //delay(100);
        brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
      }

      
    }
  }
}

int FindValidTag(char *code)
{
  if(strcmp(code, ledOn1) == 0){
    digitalWrite(9, HIGH);
    return 1;
  }
  else if(strcmp(code, ledOff1) == 0){
    digitalWrite(9, LOW);
    return 2;
  }
  else if(strcmp(code, ledOn2) == 0){
   digitalWrite(13, HIGH);
    return 3;
  }
  else if(strcmp(code, ledOff2) == 0){
      digitalWrite(13, LOW);
    return 4;
  }
  
  

  else{
    return 0;
  }
}