Pages: [1]   Go Down
Author Topic: How to stop the function of piezo buzzer that is running  (Read 1512 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I realize a project that combines an RFID reader and melodies played by a piezo buzzer that are recorded in the Arduino sketch. Currently, I assign to each of the RFID cards a particular melody and it all works well: when I pass the card the good melody is played, but I'm still compelled that the melody is played in full range before to launch a new melody with a new card on the reader.
Indeed, when the first song was triggered by a card, a card that would pass on the reader does not stop the first song.
I would like to stop the first melody with the second card and the second melody to play.
I tried to interrupt but this does not work with the program below.
Does anyone have an idea for stopping the function that plays the melody to the passage of the RFID card ?
Thank you very much...

// Piezo Sounder Melody Player and RFID reader
#include "pitches.h"
#include "tunes.h"

#define lockPin 7
#define LED_RED_PIN 5
#define LED_GREEN_PIN 6
//#define speaker_pin 9
#define tx 3
#define rx 2
#define unlockLength 2000

#include <SoftwareSerial.h>
SoftwareSerial rfidReader = SoftwareSerial(rx, tx);
int users = 3;
char* cards[] = { // valid cards
"24004DGSE6",
"24004DED7C",
"24004DS17-",
};
char* names[] = { // cardholder names
"JG",
"SG",
"IG",
};

void setup() {
pinMode (lockPin, OUTPUT);
pinMode (speaker_pin, OUTPUT);
pinMode (LED_RED_PIN, OUTPUT);
pinMode (LED_GREEN_PIN, OUTPUT);
digitalWrite(lockPin, LOW);
Serial.begin(9600);
rfidReader.begin(9600);
}
void loop() {
char cardNum[10]; // array to hold card number
byte cardBytes[6]; // byte version of card number + checksum
int index=0; // current digit
byte byteIn=0; // byte read from RFID
byte lastByte=0; // the last byte read
byte checksum = 0; // checksum result stored here
if (rfidReader.read()==2) { // read the RFID reader
while(index<12) { // 12 digits in unique serial number
byteIn = rfidReader.read(); // store value in byteIn
if ((byteIn==1) || (byteIn==2) || (byteIn==10) || (byteIn==13)) {return;}
// if STX, ETX, CR or LF break
if (index<10) {cardNum[index]=byteIn;} // store first 10 HEX digits only (last 2 are checksum)
// convert ascii hex to integer hex value
if ((byteIn>='0') && (byteIn<='9')) {
byteIn -= '0';
}
else if ((byteIn>='A') && (byteIn<='F')) {
byteIn = (byteIn+10)-'A';
}
if ((index & 1) == 1) { // if odd number merge 2 4 bit digits into 8 bit byte
cardBytes[index/2]= (byteIn | (lastByte<<4)); // move the last digit 4 bits left and add new digit
if (index<10) {checksum ^= cardBytes[index/2];} // tot up the checksum value
}
lastByte=byteIn; // store the last byte read
index++; // increment the index
if (index==12) {cardNum[10] = '\0';} // if we have reached the end of all digits add a null terminator
}
Serial.println(cardNum); // print the card number
int cardIndex =checkCard(cardNum); // check if card is valid and return index number
if(cardIndex>=0 && (cardBytes[5]==checksum)) { // if card number and checksum are valid
Serial.println("Card Validated");
Serial.print("User: ");
Serial.println(names[cardIndex]); // print the relevant name
unlock(); // unlock the door
Serial.println();
digitalWrite(LED_GREEN_PIN, HIGH);
delay(1000);
digitalWrite(LED_GREEN_PIN, LOW);

int length = tune_length[cardIndex];
play_song(cardIndex, length, milliseconde[cardIndex]);

}
else {
Serial.println("Card INVALID");
digitalWrite(LED_RED_PIN, HIGH);
tone(speaker_pin, 250, 250);
delay(250);
tone(speaker_pin, 150, 250);
delay(250);
digitalWrite(LED_RED_PIN, LOW);

Serial.println();
}
}
}
int checkCard(char cardNum[10]) {
for (int x=0; x<=users; x++) { // check all valid cards
if(strcmp(cardNum, cards
  • )==0) { // compare with last read card number
return (x); // return index of card number
}
}
return (-1); // negative value indicates no match
}
void unlock() {
tone(speaker_pin, 1000, 500);
digitalWrite(lockPin, HIGH);
delay(unlockLength);
digitalWrite(lockPin, LOW);
}


void play_song (int song, int length, int milliseconde_tempo) {
 
for (int x=0; x<length; x++) {
if ((*(tune[song] + x)) != 0){
  tone(speaker_pin, *(tune[song] + x)); }
    delay(milliseconde_tempo* *(duration[song] + x));
    noTone(speaker_pin);
  }//FIN DU FOR
}



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the IDE, there is a tool called "auto format".
I can't recommend it too highly.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: