How to attachInterrupt in my code

Hey all, i am working in rfid and now here i very confused in my coding coz i wanna make it could be interrupt where if that tag has been scan by reader it will call process, and if that other process has been scan too that first tag process must be stop and process 2 will be called. please your help.
my code of rfid :
int buffer[14];
int *buffer_at;
int *buffer_end = buffer + sizeof(buffer);

String checksum;
boolean tagfound = false;

void setup()
{
Serial.begin(9600);
Serial.println("Serial Ready");

Serial1.begin(9600);
Serial.println("RFID Ready");
}

void loop()
{

if (Serial1.available()){
delay(20);
buffer_at = buffer;
while ( buffer_at < buffer_end )
{
*buffer_at++ = Serial1.read();
}
tagfound = true;
Serial1.end();
Serial1.begin(9600);
}

if (tagfound){
buffer_at = buffer;
uint32_t result = 0;
// Skip the preamble
++buffer_at;
// Accumulate the checksum, starting with the first value
int checksum = rfid_get_next();
// We are looking for 4 more values
int i = 4;
while(i--)
{
// Grab the next value
int value = rfid_get_next();
// Add it into the result
result <<= 8;
//Serial.print("Add =");
//Serial.println(result);
result |= value;
//Serial.print("Xor =");
//Serial.println(result);
// Xor it into the checksum
checksum ^= value;
}

// Pull out the checksum from the data
int data_checksum = rfid_get_next();

// Print the result
Serial.print("Tag: ");
Serial.print(result);
if ( checksum == data_checksum )
Serial.println(" OK");
else
Serial.println(" CHECKSUM FAILED");
// We're done processing, so there is no current value

tagfound = false;

}

}

int rfid_get_next(void)
{
// sscanf needs a 2-byte space to put the result but we only need one byte.
int hexresult;
// Working space to assemble each byte
static char byte_chars[3];
// Pull out one byte from this position in the stream
snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]);
sscanf(byte_chars,"%x",&hexresult);
buffer_at += 2;
return hexresult;
}

so hopefully you could help me :mrgreen:

I'm not sure an interrupt is the correct mechanism for what you want to do.
Please use code tags when posting code.

have you any idea for this case ?

that tag is uniq from that tag

have you any idea for this case ?

No, because it isn't clear to me what you want to do, and you haven't said what the source of interrupts is.

that tag is uniq from that tag

I don't understand.

Please edit your first post so that the code is inside tags.

if (Serial1.available()){
delay(20);
buffer_at = buffer;
while ( buffer_at < buffer_end )
{
*buffer_at++ = Serial1.read();
}
tagfound = true;
Serial1.end();
Serial1.begin(9600);
}

If there is at least one byte available to read, read enough characters to fill the buffer, however many that is. Wrong! Completely wrong.

Then, turn that serial port off and back on again. Completely unnecessary. Stop that.

And, no, interrupts are NOT what you need.

You have not defined anything that happens when a tag is scanned and found to be valid, so it's hard to see what you want to interrupt.

here i attached my coding using by music shield + rfid + arduino mega 2560

#include <Fat16.h>
#include <Fat16Util.h>
#include <NewSPI.h>
#include <arduino.h>
#include <TimerOne.h>

#include "pins_config.h"
#include "vs10xx.h"
#include "newSDLib.h"
#include "MusicPlayer.h"
MusicPlayer myplayer;

char *dtaSocket; // the max leng of command
int dtaLen = 0; // actual leng of command
unsigned char getDta = 0;

//Declare RFID
uint8_t buffer[14];
uint8_t* buffer_at;
uint8_t* buffer_end = buffer + sizeof(buffer);
String checksum;
boolean tagfound = false;

void timerIsr()
{
if(Serial.available())
{
dtaSocket[dtaLen++] = Serial.read();
if(dtaSocket[dtaLen-1]=='\n')
{
playingState = PS_STOP;
getDta = 1;
}
if(dtaLen > 8)
{
getDta = 0;
dtaLen = 0;
}
}

}

void setup()
{
Serial.begin(9600);
myplayer.begin();//will initialize the hardware and set default mode to be normal.
Serial1.begin(9600);
Serial.println("RFID Ready");

Timer1.initialize(10);
Timer1.attachInterrupt( timerIsr );
}

/unsigned char judge(char str1, char str2, int n) //discriminant function
{
for(int i = 0; i<n; i++)
{
if(str1 _!= str2
)
_
_
{_
return 0;
_
}_
_
}_
return 1;
_
}_
_/
void loop()
{
myplayer.setPlayMode(MODE_NORMAL);
/_
if(getDta)
_
{_
getDta = 0;
int tmp = dtaLen;
dtaLen = 0;
if(Serial1.available(dtaSocket, "Name1.mp3",5) && tmp == 6) // input the name of a song,but the length need to be 5 bytes.
_
{_
myplayer.playSong("ENRIQU~1.MP3"); //play the song with the name you give.
_
}_
else if(judge(dtaSocket, "Name2.mp3", 5) && tmp == 6) // input the name of a song,but the length need to be 5 bytes.
_
{_
myplayer.playSong("Name2.mp3"); //play the song with the name you give.
_
}_
else
_
{_
Serial.println("unavailable command"); //only two command is available,others are not
_
}_
_
}_
_/_

if (Serial1.available()){
* delay(20);*
* buffer_at = buffer;*

* while ( buffer_at < buffer_end )
_
{_
buffer_at++ = Serial1.read();
_
}
_
* tagfound = true;*
* Serial1.end();*
* Serial1.begin(9600);*
* }*
* if (tagfound){*
* buffer_at = buffer;
uint32_t result = 0;*

* // Skip the preamble*
* ++buffer_at;
_
// Accumulate the checksum, starting with the first value*_
* uint8_t checksum = rfid_get_next();
_
// We are looking for 4 more values*_
* int i = 4;*
* while(i--)*
* {*
* // Grab the next value*
* uint8_t value = rfid_get_next();
_
// Add it into the result*_
* result <<= 8;*
* result |= value;*
* // Xor it into the checksum*
* checksum ^= value;*
* }*
* // Pull out the checksum from the data*
* uint8_t data_checksum = rfid_get_next();*

* // Print the result*
* Serial.print("Tag: ");*
* //Serial.print(result);*
* if((int)result == (int)4185575){*

* myplayer.playSong("CHRIS_~1.MP3");*

* }*
* else if((int)result == (int)8499115){*
* myplayer.playSong("FUN-WH~1.MP3");*

* }*
* else {*
* Serial.println("");*
* }*

* if ( checksum == data_checksum )
_
Serial.println(" OK");_
_
else*_
* Serial.println(" CHECKSUM FAILED");*
* // We're done processing, so there is no current value*
_ /if(result != "4197386") //bagaimana mau membandingkan data nya_
_
Serial.print("Good");_
_
else*_
* Serial.print("nice");*
_ /_
_
tagfound = false;*_

* }*
}
uint8_t rfid_get_next(void)
{
* // sscanf needs a 2-byte space to put the result but we*
* // only need one byte.*
* uint16_t hexresult;
_
// Working space to assemble each byte*_
* static char byte_chars[3];
_
// Pull out one byte from this position in the stream*_
* snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]);
sscanf(byte_chars,"%x",&hexresult);
buffer_at += 2;
return static_cast<uint8_t>(hexresult);
_
}*_
at that above coding it work when i scan that tag contain uniq 4185575 it will call music CHRIS.mp3, and other conditional it will work too.
here i wanna make this code run like this, when i scan that tag contain uniq 4185575 it will call music CHRIS.mp3 and while that music play, i scan the next tag 8499115, this program should be stop that music CHRIS.mp3 and play that FUN-WH~1.MP3.
until now i just can play that music one process done and will be play with other process.

Now do you see why we ask you to put your code inside tags?

You can go back and edit your posts to do that - click on "modify"

if((int)result == (int)4185575){

4185575 is a literal constant that the compiler will interpret as an int. Casting it to an int is unnecessary, and wrong. It is NOT an int. That is it is not in the range that fits in an int. Casting result to an int is wrong, too.

If the idea is to play a different song when a tag is scanned, and the thing that you are trying to interrupt is the playing of the current song, if any, you simply need to research the methods available in the MusicPlayer class. It should have a method to tell if a song is playing, and it should have a method to stop it, if one is playing.

just_riko11:
here i attached my coding using by music shield + rfid + arduino mega 2560

Please read this:

How to use this forum

Read this before posting a programming question

I trust we won't have to ask for code tags again.

PaulS:

if((int)result == (int)4185575){

4185575 is a literal constant that the compiler will interpret as an int.

No, Paul. The compiler promotes it to a long because it doesn't fit into an int. Consider this code:

void setup ()
  {
  Serial.begin (115200);
  Serial.println (4185575);
  }  // end of setup

void loop () { }

Output:

4185575