Okay, I want to preface this saying that I am running this on a NodeMCU and that I am aware that their pins are labeled differently. I have, to the best of my knowledge, solved that by defining stuff.
Onto the project/problem.
I have an mp3-board that can play a number of audio files based on what certain pins are (1 or 0). So There are 8 of these pins. 11111110 will play song a, 11111101 will play song b. I have tried this using wires and it works as advertised (and I am using the numbers they used in the datasheet here).
I have now attached said NodeMCU to that board. I then used
pinMode(D1, OUTPUT);
digitalWrite(D1, HIGH);
And it will play the song associated with that pin. Same with the second pin.
Problems start with the third song, which would be 11111100, i.e. two buttons pressed, two pins "triggering". When I use the above code for two pins, nothing happens. No sound at all.
What am I doing wrong? Where is the error in my thinking? I hope I made sense, and thanks for the help in advance!
It's this one (not the actual vendor, but the right module) - https://de.aliexpress.com/item/32999890318.html
This is also where I have my information from. I am using it in work mode "I/O integrated mode 0".
It works like that as a stand-alone module when I use wires to connect the appropriate pins to ground. There, connecting two pins to ground works to trigger that third track.
Slumpert:
I am confused, are you wired up so that “No” buttons pressed = 11111111 input for the MP3 player?
I have copied the nomenclature from the module's datasheet - there, 11111111 stands for "no botton pressed" or rather "no pin connected to ground".
In my code, I have set all 8 pins to output and then to LOW in the setup. Then I set a single pin to HIGH in the loop (which I realize for this testing run I could do in the setup as well), and it works for a single button - the player plays the appropriate file. But when I set two pins to HIGH (akin to pressing two buttons), there is no reaction.
I realize that this is somewhat counter-intuiitive (and possibly quite wrong), since LOW should be ground? I thought so at the start but that did not really work out. As far as trouble-shooting goes, I have currently 3 files on the microSD card, so if I am triggering any of the other 252 files I would not know.
The "I/O Integrated Mode 0" is probably the best, that's what you already have.
The nodeMCU is a 3.3V processor, so I assume it is okay to put 3.3V output voltage on the pins.
This example is not tested !
I don't know which pins of the nodeMCU are used, as an example I use 2, 3, 4, 5, 6, 7, 8, 9.
The MP3 module has pullup resistors on those pins, that might change the startup mode of the nodeMCU. You have to carefully check if those pins can be used.
const int songPins[] = { 2, 3, 4, 5, 6, 7, 8, 9};
void setup()
{
// Set the pins for the songs as OUTPUT and HIGH.
// When the pinMode is used, the output is set default as LOW.
// That can be avoided by presetting it with digitalWrite(..., HIGH),
// but I don't know if the nodeMCU can do that as well.
for( int i=0; i<8; i++)
{
pinMode( songPins[i], OUTPUT);
digitalWrite( songPins[i], HIGH);
}
}
// Play a song, 1...255 and 0 is no song.
void playSong( int n)
{
byte data = (byte) n;
data ~= data; // bitwise flip all bits
// Set the output pins
for( int i=0; i<8; i++)
{
if( bitRead( data, i) == 0)
{
digitalWrite( songPins[i], LOW);
}
}
// wait some time, half a second might be enough
delay( 500);
// Return the pins to their HIGH state again.
for( int i=0; i<8; i++)
{
digitalWrite( songPins[i], HIGH);
}
}
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks for the replies and apology for taking longer to get back to you. I have used @Koepel's example and it worked great! It plays the required songs and allows me to take it from there. Thank you very much!
@TomGeorge: I will make sure to adhere to the rules and the proper formatting of code next time I have a question. This issue has been resolved, and I do not actually have a final circuit yet - but now I can get to that! Thanks!