I have a question, maybe it's not as difficult as I think, but I don't seem to manage.
I have used the Charlie Bear code to create an RFID music player. And it works, hurray!
The only thing that is missing for my project, is that the music has to stop when the tag is removed from the reader. Right now, the song will stop when it has reached the end. That's great! Now I only need to add an extra option that the song can be interrupted when the tag is no longer being read.
So: Tag --> Play Song --> Remove Tag --> Stop Song
Otherwise: Tag --> Play Song --> End of Song
I cannot find a way to do it but perhaps one of you has done something like this before.
I really appreciate your help, it means my project will finally be finished! (And working! )
Okay, I noticed that when the Arduino and Wave Shield are plugged into the laptop and the tag lies on the reader, it will play the song and just repeats it as long as the tag is above the reader.
However, I just connected 6 AA batteries (as it needs to be a stand-alone artifact) and it stutters playing the song as long as the tag is above the reader.. when I "scan" the tag and remove it, it will play the song normally.
So adding a battery created a new problem :-/ as for the users it will be more logical to lay it onto the reader to hear the complete song. HELP :o
Never really tried this, but just throwing out an idea. How about using wave.isplaying as a flag, you check to see if a card is present, if a wave is playing do not actually read the card, just verify that it is present, if it is not present you call silence() to stop the wave. Something like this...
void loop() {
if (Serial.available() > 0) { // if data available from reader
if (!(wave.isplaying) { //If wave is not playing read the card
if ((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while (bytesread < 10) { // read 10 digit code
if ( Serial.available() > 0) {
val = Serial.read();
if ((val == 10) || (val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if (bytesread == 10) { // if 10 digit read is complete
playsound(code);
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
Serial.flush(); // Flush the serial buffer before trying to read a new code
}
bytesread = 0;
}
}
}
else //no card present call silence()
{
silence();
}
}
If it works when connected to a computer but does not work when running off battries then you have forgotten to wire up the ground on something.
Please post a schematic of how you have wired it.
Grumpy_Mike:
If it works when connected to a computer but does not work when running off battries then you have forgotten to wire up the ground on something.
Please post a schematic of how you have wired it.
Well I'm still having the "stop music when there's no tag" problem, but here is the schematic you asked for.
So I connect 6 AA batteries with a DC plug to the jack on the Arduino. I have a Parallax RFID Read Serial that has 4 pins, going to 5V, pin 7, pin 0 and GND.
These wires are not soldered yet, as I need to be sure it's working. Not sure if this could be the problem, however it would be strange it works better on USB than on battery.
I have an ID12 RFID reader as well. Yesterday I did a test with the same 6 AA batteries and it works with the ID12 reader. So the battery is not working properly with the Parallax reader, but works fine with the ID12 reader.
However, the problem with that code is that I would still like the music to stop when the tag is removed.. :o
So I don't mind using the ID12 or the Parallax, as long as it works on battery and stops the music when there's no tag.
BH72:
Never really tried this, but just throwing out an idea. How about using wave.isplaying as a flag, you check to see if a card is present, if a wave is playing do not actually read the card, just verify that it is present, if it is not present you call silence() to stop the wave. Something like this...
It's not working. It is not stuttering anymore when I plug the battery in, but it doesn't stop the music when the tag is removed. The song keeps repeating for as long as the tag was on the reader.
However, the problem with that code is that I would still like the music to stop when the tag is removed
So can you get a message from the RFID reader that says the tag is removed?
Or is it that you only get a message when a tag is presented?
Typically an RFID reader can not tell you when a tag is removed only when one is presented.
What happens when you toggle the enable pin?
You could try a trick of powering down the reader and then powering it up at regular intervals to see if it still returns a tag ID. To do this you would require a PNP transistor to switch the power.
I have an ID12 RFID reader as well. Yesterday I did a test with the same 6 AA batteries and it works with the ID12 reader.
When you connect your non working reader have you tried connecting the Arduino ground to the mains ground?
I found this code, which (according to the description) is doing what I am trying to do. Video
The problem I am facing with this particular code is that the Serial monitor is endlessly printing: tag removed?
So, it does not even read a tag I hold in front, it seems to be skipping that whole reading part.
(The annoying thing is that I have exactly the same equipment.. so really confused about it not working)
I am not sure what you mean with 'main ground'? Is that the ground next to pin 13? In that case, it is not changing anything.
No mains ground. The ground from your AC supply connector on the wall.
This will only report a tag removal while the sound is playing.
// check to see when the last time we had a tag was
if (millis() - currentMillis > 2000 && wave.isplaying)
{
Serial.println ("tag removed?");
currentTerrarium=0;
if (wave.isplaying)
{// already playing something, so stop it!
wave.stop(); // stop it
}
}
Ooh I see. No I haven't tried that.. I will search for one to see if it will make a difference. (Although, it cannot really plugged into the wall as well)
The annoying thing is that it keeps reporting a removed tag while there is not even music playing.
Inge:
It's not working. It is not stuttering anymore when I plug the battery in, but it doesn't stop the music when the tag is removed. The song keeps repeating for as long as the tag was on the reader.
Oh right you are, it seems the card is being read every time through loop, the way it is above the serial buffer does not get flushed except when the card is initially read, so once the music is playing if (Serial.available() > 0) will be true so the "else" will never take place. Maybe a serial flush if serial.available > 0?
void loop() {
if (Serial.available() > 0) { // if data available from reader
if (!(wave.isplaying) { //If wave is not playing read the card
if ((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while (bytesread < 10) { // read 10 digit code
if ( Serial.available() > 0) {
val = Serial.read();
if ((val == 10) || (val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if (bytesread == 10) { // if 10 digit read is complete
playsound(code);
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
}
bytesread = 0;
}
}
Serial.flush(); // Flush the serial buffer before trying to read a new code
}
else //no card present call silence()
{
silence();
}
Sorry about the lousy code paste, typing in Wordpad so no "Auto Format"..
BH72:
Oh right you are, it seems the card is being read every time through loop, the way it is above the serial buffer does not get flushed except when the card is initially read, so once the music is playing if (Serial.available() > 0) will be true so the "else" will never take place. Maybe a serial flush if serial.available > 0?
I see what you mean, but it still won't stop the song when I remove the tag. It just finishes the song and that keeps repeating it, as the tag was on the reader for X amount of seconds.
I replaced the previous tag-checking code with your code. So it's in the same position as the previous one. Maybe it is not reading the tags, because the code never was?
Update: So I uncommented (is that a word?) your section and it is still not reading the tags. So the problem must be somewhere else. I compared it with the Charlie_Bear code, but I cannot find the reason why it's not detecting the tags.
The word is "commented out" but you can't do that, you would have had to remove just part of the line.
In all I changed was:-
if (millis() - currentMillis > 2000)
to
if (millis() - currentMillis > 2000 && wave.isplaying)
So that it would not keep on printing "tag removed?" when there was no tag. It would only print it if there was no tag AND ( the && bit ) the sound is playing.
The problem with that code is it is badly written. The loop function is way too long and should be split up into a number of functions to make things much easier to follow and to fault find.
Hm, I understand, so that script is useless than as it is not working at all.
So, than I guess I would need to go with the ID12 reader as it is able to work on my 6 AA batteries.
In that case I am still looking for a way to keep the music playing as long as there is a tag. I'm wondering if it's even possible? It's driving me nuts by now :-/
Yes it is possible but you have to insert the code in the correct place and that place is obscufcated by the poor code structure.
Basically you need to understand the code and seprate out the mechanics of reading in a token and playing a wave file from the higher control code of what you want these things to do.