Hey there,
I am on my first Arduino project and I am stuck in a corner.
I have a project with different buttons that switch on different LED segments.
Now I want the Arduino to play switch when the LEDs are switched on and off depending on the switched buttons.
I used the PCM library for playing sounds with was created by Michael Smith (if needed i can link it).
This library needs to create the sound files as a char Array and then you can playback these with the function startPlayback(array, sizeof(array));
If I declare these sound files I am able to play them back, but if I create a function where I check the button states and I try to pass these array into the function they do not play. If I call the array in the button function, it is no problem. I think I am passing the array to the function in a wrong way.
ChatGPT says passing them by reference
void myFunction(char* array) {
// Do something with the array
}
myFunction(myArray);
is the right way but it does not work.
Here are extract of my code, it would be cool if you can tell me what I am doing wrong.
//Soundplayback
const unsigned char computer_on[] PROGMEM = {126, etc.
const unsigned char computer_off[] PROGMEM = {123, etc.
//Button state function
bool toggleButtonState(byte buttonPin, bool &ButtonState, byte &lastButtonState, unsigned long &lastTimeButtonStateChanged, byte debounceDuration, char* if_button_on, char* if_button_off) {
if (millis() - lastTimeButtonStateChanged >= debounceDuration){
byte buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState){
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
//Button off
if(buttonState == false) {
if(ButtonState == true){
ButtonState = false;
switch(buttonPin){ //switch button with calling does work
case 2:
startPlayback(computer_off, sizeof(computer_off));
break;
case 3:
startPlayback(computer_off, sizeof(computer_off));
default:
startPlayback(computer_login, sizeof(computer_login));
}
// this part does not work
//startPlayback(computer_login, sizeof(computer_login));
//startPlayback(if_button_on, sizeof(if_button_on));
//calling the function in the programm
Button1State = toggleButtonState(BUTTON_PIN_1, Button1State, lastButtonState1, lastTimeButtonStateChanged, debounceDuration, computer_on, computer_off);