Clicking sound on speaker and other problems

Greetings,
some time ago good people from this forum helped me come up with a program for my problem that i had…In short, the idea of the program is to have 10 sensors and 10 LED’s. So the first question in the code below is…how do i simply add 5 more sensors with their corresponding LED’s to light up respectively, blink a few times and then turn back off, just like the first 5 sensors and LED’s that are already in the program. Except, when the first 5 LED’s are all lit up and they do the blink, a MARIO sound effect is triggered. But for the other 5 LED’s, i would somehow like to add a different sound effect to happen [e.g. coin effect]? The sound effects for each individual sensors should play too.
Next problem is i started getting a clicking sound from the buzzer at any given moment the program is runing. I think this represents that a delay function or something must constantly keep restarting a sound effect, but i cannot seem to find the problem…If someone could have a look a the program, fix anything or try to help i will be very gratefull…Keep in mind that i have a very hard time learning C and i dont always understand the code…Thank you in advance!!

/*
 BASED ON State change detection (edge detection) changed for INPUT PULLUP
 and with a struct array
 delay()-less millis()-based pulse pin 13
*/


#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560

TMRpcm audio;   // create an object for use in this sketch

File myFile;
char line[25];
byte index = 0;

// the buttons
struct buttons
{
 byte pin;
 byte led;
 bool state;
 bool prevState;
 byte score;
 byte pressed;
 char muzika[10];//tu dodam string ki bo vseboval ime datoteke z muziko/////lahko se znajša na manj znakov/////////////////////////////////////////////////////////////
};

const byte buttonPins[] = {13, 14, 15, 16 , 17};  //Senz za M A R I O
const byte numberOfButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
buttons mybuttons[numberOfButtons];
const byte ledPins[] = {23, 24, 25, 26, 27};
const byte scores[] = {10, 20, 30, 40, 50};
const char *ImenaMuzik[] ={"boo.wav","coin.wav","opica.wav","boomba.wav","shyguy.wav",};//tu dodam polje stringov ki vsebujejo imena datotek z muziko////////preimenuj v imena datotek/////////////////////////////////////////////////////////

///END SENSOR CONF
bool reading;
const int button = 49;
bool prevState;

//SCORING
long highscore = 0;
long runningTotal;
byte numberOfButtonsPressed = 0;

//the effect led
int effectLedInterval;
int effectLedIntervalMax = 100;
int effectLedIntervalMin = 30;
unsigned long previousMillisEffect;
bool effectState;
bool doEffect = false;


unsigned long before = 0;
const long interval = 100;


void setup()
{
 // initialize serial communication:
 Serial.begin(9600);
 Serial.println("setup() ... ");
 Serial.println("** 679079 **");
 Serial.print("Compiler: ");
 Serial.print(__VERSION__);
 Serial.print(", Arduino IDE: ");
 Serial.println(ARDUINO);
 Serial.print("Created: ");
 Serial.print(__TIME__);
 Serial.print(", ");
 Serial.println(__DATE__);
 Serial.println(__FILE__);

 // initialize the button pins as input with pullup so active low
 //    make sure the button is from pin to ground
 Serial.println(" ");
 Serial.print("Initialising "); Serial.print(numberOfButtons); Serial.println(" buttons");
 for (int i = 0; i < numberOfButtons; i++)
 {
   mybuttons[i].pin = buttonPins[i];
   pinMode(mybuttons[i].pin, INPUT_PULLUP);
   mybuttons[i].led = ledPins[i];
   pinMode(mybuttons[i].led, OUTPUT);
   mybuttons[i].state = digitalRead(mybuttons[i].pin);
   mybuttons[i].prevState = mybuttons[i].state;
   mybuttons[i].score = scores[i];
   mybuttons[i].pressed = 0;
   Serial.print("Button: "); Serial.print(i);
   Serial.print(", Pin: "); Serial.print(mybuttons[i].pin);
   Serial.print(", Led: "); Serial.print(mybuttons[i].led);
   Serial.print(", Score: "); Serial.print(mybuttons[i].score);
   Serial.print(", Pressed: "); Serial.print(mybuttons[i].pressed);
   Serial.print(", State: "); Serial.print(mybuttons[i].state);
   Serial.print(", Prev state: "); Serial.println(mybuttons[i].prevState);
   strcpy(mybuttons[i].muzika, ImenaMuzik[i]);//tu vstavim ime muzike v spremenljivko za ta button////////////////////////////////////////////////////////////////////
 }


 Serial.println(" ");
 Serial.println("setup() done");
 Serial.println("Press a button....");
 Serial.println(" ");

 pinMode(49, INPUT_PULLUP);


 audio.speakerPin = 11; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
 audio.setVolume(5);
 if (!SD.begin(SD_ChipSelectPin)) {
   return;
 } else {
   Serial.println("SD OK");
 }

 ////////////////////////////////////////////////////////////////////////
//Write high score to LCD on startup
  myFile = SD.open("test.txt");
 if (myFile) {
   Serial.println("test.txt:");

   // read from the file until there's nothing else in it:
   while (myFile.available()) {
     line[index] = (myFile.read());
     // check if character is '\r'
     if (line[index] == '\r')
     {
       // add terminatin NUL character
       line[index] == '\0';
       // done
       break;
     }
     // next character will go in next element of line
     index++;
   }
   // close the file:
   highscore = atol(line);
   myFile.close();
 }
}

void loop()
{
 checkForButtonStateChange();
 if (doEffect) blinkLedsAndMakeANoise();
 
  checkIfStopWasPressed();
  text();
} //loop

void checkForButtonStateChange()
{
 for (int i = 0; i < numberOfButtons; i++)
 {
   mybuttons[i].state = digitalRead(mybuttons[i].pin);
   // compare the buttonState to its previous state
   if (mybuttons[i].state != mybuttons[i].prevState) // means it changed... but which way?
   {
     if (mybuttons[i].state == HIGH)  // changed to pressed
     {
       if(audio.isPlaying()==0){ //returns 1 if music playing, 0 if not
       audio.play(mybuttons[i].muzika);//plays effect for specific button/////////////////////////////////////////////////////////////////////////////
       }
       Serial.print(i);
       Serial.print(" newly pressed");
       if (mybuttons[i].pressed == 0)
       {
         mybuttons[i].pressed = 1;
         numberOfButtonsPressed++;
         Serial.print(", Unique buttons pressed "); Serial.print(numberOfButtonsPressed);

       }
       digitalWrite(mybuttons[i].led, HIGH);
       runningTotal = runningTotal + mybuttons[i].score;
       Serial.print(", Score "); Serial.println(runningTotal);
     }
     // poor man's de-bounce
     delay(50);
   }
   // save the current state as the last state, for next time through the loop
   mybuttons[i].prevState = mybuttons[i].state;
 }

 if (numberOfButtonsPressed == 5)
 {
   Serial.print("All leds on, score "); Serial.println(runningTotal);
   for (int i = 0; i < numberOfButtons; i++)
   {
     mybuttons[i].pressed = 0;
     audio.play("mario.wav");
   }
   doEffect = true;
   numberOfButtonsPressed = 0;
 }
} // checkForButtonStateChange()


void blinkLedsAndMakeANoise()
{
 static byte numberOfEffects = 0;
 if (millis() - previousMillisEffect >= effectLedInterval)
 {
   effectLedInterval = random(effectLedIntervalMin, effectLedIntervalMax);
   previousMillisEffect = millis();
   effectState = !effectState;
   for (int i = 0; i < numberOfButtons; i++)
   {
     digitalWrite(mybuttons[i].led, effectState);
   }
   numberOfEffects++;
 }

 if (numberOfEffects == 25)
 {
   doEffect = false;
   numberOfEffects = 0;
   for (int i = 0; i < numberOfButtons; i++)
   {
     digitalWrite(mybuttons[i].led, LOW);
   }
 }
}


void checkIfStopWasPressed()
{
 reading = digitalRead(49);
 if (reading == 0) { //button was pressed
   delay(50);
   if (runningTotal > highscore) {  //check if current score > highsore
     highscore = runningTotal;      //if current score> highscore, write that score to SD card
     SD.remove("test.txt");  //izbriše file z starim rezultatom, ustvari nov file z novim high score-om.
     delay(50);
     myFile = SD.open("test.txt", FILE_WRITE);
     if (myFile) {
       Serial.print("Writing to test.txt...");
       myFile.println(highscore);
       myFile.close();
     }
   }
 }
}


void text() {
 if (millis() - before >= interval) {
   before = millis();
   lcd.clear();
   lcd.setCursor(4, 0);
   lcd.print(runningTotal);
   lcd.setCursor(4, 1);
   lcd.print(highscore);
 }
}

Hi,

Greetings,
some time ago good people from this forum helped me come up with a program for my problem that i had...
In short, the idea of the program is to have 10 sensors and 10 LED's.
So the first question in the code below is...how do i simply add 5 more sensors with their corresponding LED's to light up respectively, blink a few times and then turn back off, just like the first 5 sensors and LED's that are already in the program.
Except, when the first 5 LED's are all lit up and they do the blink, a MARIO sound effect is triggered.
But for the other 5 LED's, i would somehow like to add a different sound effect to happen [e.g. coin effect]?
The sound effects for each individual sensors should play too.
Next problem is i started getting a clicking sound from the buzzer at any given moment the program is runing.
I think this represents that a delay function or something must constantly keep restarting a sound effect, but i cannot seem to find the problem..
If someone could have a look a the program, fix anything or try to help i will be very gratefull..
Keep in mind that i have a very hard time learning C and i dont always understand the code...
Thank you in advance!!

Sorry I had to spread your post out to read the points.

What model Arduino are you using?

Tom... :slight_smile:

Hello, it is an Arduino Mega 2560, with some IR sensors as inputs, and drive some TIP 120 transitors that power 12 V led lights.

Sensors: IR Infrared Obstacle Avoidance Sensor Module for Arduino Smart Car Robot 3 Wire Reflective Photoelectric New|sensor module|obstacle avoidance sensor moduleavoidance sensor - AliExpress

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.