purtroppo il collega(nonchè mio guru) che mi aiuta di solito è attualmente molto indaffarato e non ha tempo da dedicarmi, sono quindi qui a chiedervi aiuto di nuovo, sto facendo qualche esperimento con l'RFID ho trovato 2 sketch on line uno per la lettura dei tag e l'altro per il confronto, ho letto i 2 tag e inserito questi 2 valori nello sketch sotto ( sostituiti con xxx perchè sono i transponder del mio allarme) ho il seguente problema:
- ogni volta che metto un tag, sia giusto che sbagliato, mi esegue 2 volte il programma ( ho fatto un video ma non saprei come postarlo)
il secondo problema(marginale) sarebbe quello di fargli comporre una melodia mentre il relay e il servo sono azionati, non saprei dove/come inserire la funzione musica, per il momento l'ho inserita nel loop per vedere cosa suonasse , emette qualche suono ma non è sicuramente per elisa infatti mi sto studiando sta cosa http://www.arduino.cc/en/Tutorial/melody
ciao e grazie in anticipo
//-------------------------------------------------------------------------------------------------------------
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial RFID(2, 3); // RX and TX
int data1 = 0;
int TApertura = 3000; // tempo che il raly deve essere eccitato int ok = -1; int yes = 13; int no = 12; int relay = 8; int pinServo = 11; // use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers int tag1[14] = {XXX};
int tag2[14] = {XXX };
int newtag[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons Servo myServo;
unsigned long time= millis();
unsigned long prevtime = time;
int nota;
int i=0;
int DO=1046;
int DOd=1109;
int RE=1175;
int REd=1245;
int MI=1319;
int FA=1397;
int FAd=1480;
int SOL=1568;
int SOLd=1661;
int LA=1760;
int LAd=1865;
int SI=1976;
char perElisa[]={'MI','REd','MI','REd', 'MI', 'SI', 'RE', 'DO', 'LA', 'DO', 'MI', 'LA', 'SI', 'MI', 'SOL', 'SI', 'DO', 'MI', 'REd', 'MI', 'REd', 'MI', 'SI', 'RE', 'DO', 'LA', 'DO', 'MI', 'LA', 'SI', 'MI', 'DO', 'SI', 'LA', 'SI', 'DO', 'RE', 'MI'};//, 'SOL', 'FA', 'MI', 'RE', 'FA', 'MI', 'RE','DO', 'MI', 'RE', 'DO', 'SI', 'MI', 'REd', 'MI' 'REd', 'MI','SI', 'RE', 'DO', 'LA', 'DO', 'MI', 'LA', 'SI', 'MI', 'DO', 'SI', 'LA'};
void setup()
{
myServo.write(0); // imposta lo stato iniziale del servo all'avvio
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
pinMode(yes, OUTPUT); // for status LEDs
pinMode(no, OUTPUT);
myServo.attach(pinServo);
}
boolean comparetag(int aa[14], int bb[14]) {
boolean ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++)
{
if (aa[cc] == bb[cc])
{
fg++;
}
}
if (fg == 14)
{
ff = true;
}
return ff;
}
void checkmytags() // compares each tag against the tag just read {
ok = 0; // this variable helps decision-making,
// if it is 1 we have a match, zero is a read but no match,
// -1 is no read attempt made
if (comparetag(newtag, tag1) == true)
{
ok++;
}
if (comparetag(newtag, tag2) == true)
{
ok++;
}
}
void readTags()
{
ok = -1;
if (RFID.available() > 0)
{
// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.
for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
{
data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();
}
// now do something based on tag type
if (ok > 0) // if we had a match
{
Serial.println("Accepted");
digitalWrite(yes, HIGH);
digitalWrite(relay, HIGH);
myServo.write(180);
delay(TApertura);
digitalWrite(yes, LOW);
digitalWrite(relay, HIGH);
myServo.write(0);
ok = -1;
}
else if (ok == 0) // if we didn't have a match
{
Serial.println("Rejected");
digitalWrite(no, HIGH);
delay(1000);
digitalWrite(no, LOW);
ok = -1;
}
}
void musica(){
//i=0;
if (time-prevtime <TApertura){
nota=perElisa[i];
if (i>=20){
i=0;
noTone(3);
delay (2000);
}
if (i<20){
tone(3,nota,1000);
delay(250);
noTone(3);
i++;
}
}
}
void loop()
{
readTags();
musica();
}