Hi there!
It's my second post on this forum, I hope to post it in the right section.
I'm trying to use a TDA2030A chip to use a 24V buzzer with Adruino. The set up is working when connected directly to the Adruino, but when using the TDA2030A chip, the buzzer is unexpectedly always sounding, not only when it's supposed to do it.
Can someone help me to understand this unexpected behavior? Is there any easier way to use a 24V buzzer with Adruino?
I attach some diagrams and the code of the full project, which is a bike alarm that triggers when someone disconnect or cut the wire.
Thank you a lot for your help!
Note: What in the attached full diagram is a MOSFET, in reality is a TDA2030A like you can see in the following handwritten diagram.
const int button1 = 2; //pushbutton1 on pin 3
const int button2 = 3; //pushbutton2 on pin 2
const int connector = 4; //pushbutton3 on pin 4 (this is going to be a connector in the future)
const int Red = 11; //red LED is on pin 11
const int greenLed = 10; //green LED is pin 10
const int buzzer = 13; //buzzer pin is 13
void checkEntered1(int button);
void activateAlarm();
int code[] = {1, 2, 2, 1, 1, 1}; //the desired code is entered in this array,
//separated by commas
int entered[7]; //create a new empty array for the code entered by
//the user (has 4 elements)
int alarmActive = 0; //variable to check if the alarm is activated
//*** Variables for the alarm **//
unsigned long currentMillis = 0;
unsigned long interval=300; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned int tone1 = 0000;
unsigned int tone2 = 6;
unsigned int previousTone;
bool connected = true;
void setup() { //run once at sketch startup
Serial.begin(9600); //begin Serial
pinMode(button1, INPUT_PULLUP); //button 1 is an input
pinMode(button2, INPUT_PULLUP); //button 2 is an input
pinMode(connector, INPUT_PULLUP); //button 3 is an input
pinMode(Red, OUTPUT); //the red LED is an output
pinMode(greenLed, OUTPUT); // the green LED is an output
pinMode(buzzer, OUTPUT); //initialize the buzzer pin as an output
previousTone = tone1;
}
void loop() { //run repeatedly
currentMillis = millis(); // grab current time
//en cas que l'alarma estigui
if (alarmActive == 1) {
if (digitalRead(connector) == HIGH) {
connected = false;
}
if (!connected) {
activateAlarm();
}
}
if (digitalRead(button1) == LOW) { //if button1 is pressed
checkEntered1(1); //call checkEntered and pass it a 1
delay(250);//wait, needed for correct functioning, otherwise
//buttons are deemed to be pressed more than once
}
else if (digitalRead(button2) == LOW) { //if button2 is pressed
checkEntered1(2); //call checkEntered1 and pass it a 2
delay(250); //wait
}
}
void checkEntered1(int button) { //check the first element of the entered[] array
if (entered[0] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered2(button); //move on to checkEntered2, passing it "button"
}
else if (entered[0] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[0] = button; //set the first element as the button that has been pressed
Serial.print("1: "); Serial.println(entered[0]); //for debugging
}
}
void checkEntered2(int button) { //check the second element of the entered[] array
if (entered[1] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered3(button); //move on to checkEntered3, passing it "button"
}
else if (entered[1] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[1] = button; //set the second element as the button that has been pressed
Serial.print("2: "); Serial.println(entered[1]); //for debugging
}
}
void checkEntered3(int button) { //check the third element of the entered[] array
if (entered[2] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered4(button); //move on to checkEntered4, passing it "button"
}
else if (entered[2] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[2] = button; //set the third element as the button that has been pressed
Serial.print("3: "); Serial.println(entered[2]); //for debugging
}
}
void checkEntered4(int button) { //check the third element of the entered[] array
if (entered[3] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered5(button); //move on to checkEntered4, passing it "button"
}
else if (entered[3] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[3] = button; //set the third element as the button that has been pressed
Serial.print("4: "); Serial.println(entered[3]); //for debugging
}
}
void checkEntered5(int button) { //check the third element of the entered[] array
if (entered[4] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered6(button); //move on to checkEntered4, passing it "button"
}
else if (entered[4] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[4] = button; //set the third element as the button that has been pressed
Serial.print("5: "); Serial.println(entered[4]); //for debugging
}
}
void checkEntered6(int button) { //check the fourth element of the entered[] array
if (entered[5] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[5] = button; //set the final element as the button that has been pressed
Serial.print("6: "); Serial.println(entered[5]); //for debugging
delay(100); //allow time for processing
compareCode(); //call the compareCode function
}
}
void activateAlarm() {
// check if "interval" time has passed (1000 milliseconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
if (previousTone == tone1) {
tone(buzzer,tone2);
previousTone = tone2;
}
else if (previousTone == tone2){
noTone(buzzer);
previousTone = tone1;
}
previousMillis = millis();
}
}
void compareCode() { //checks if the code entered is correct by comparing the code[] array with the entered[] array
if ((entered[0] == code[0]) && (entered[1] == code[1]) && (entered[2] == code[2]) && (entered[3] == code[3]) && (entered[4] == code[4]) && (entered[5] == code[5])) { //if all the elements of each array are equal
if (alarmActive == 1) { //Check if alarm is activated
digitalWrite(greenLed, LOW); // turn the gren LED off
alarmActive = 0;
noTone(buzzer);
connected = true;
Serial.println("Alarm Deactivated");
delay(250);
}
else {
digitalWrite(greenLed, HIGH); //turn the green LED on
Serial.println("Alarm Activated");
alarmActive = 1;
delay(250);
}
for (int i = 0; i < 7; i++) { //this next loop is for debugging
entered[i] = 0;
}
loop(); //return to loop() (not really necessary)
}
else { //if you (or the intruder) get the code wrong
digitalWrite(Red, HIGH);
delay(1000);
Serial.println("Wrong PassWord");
digitalWrite(Red, LOW);
for (int i = 0; i < 7; i++) { //this next loop is for debugging
entered[i] = 0;
}
}
}