push button code - missing purpose

Hi everyone,

I'm working off a blog trying to build up my understanding of the Arduino in an attempt to build a pushbutton-controlled MP3 player for my toddler. This particular sketch features 10 buttons, but the sketch author failed to explain what it is supposed to do. If anyone has an idea by looking at the code, please let me know. I'm also including the fritzing schematic.

Thanks so much! :slight_smile:

The code:

// constants won't change
 
// the number of the pushbutton pins
const int buttonPins[] = { 2, 5, 8, 9, 10, A0, A1, A2, A3, A4 };
 
// variables will change
 
// variable for reading the pushbutton status
int buttonStates[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
// variable for remember the number of button pressed
int counters[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 
// the setup routine runs once when you turn the device on or you press reset
void setup()
{
    // initialize the pushbutton pins as input and enable internal pull-up resistor
    for (int i = 0; i < (sizeof(buttonPins) / sizeof(int)); i++)
    {
        pinMode(buttonPins[i], INPUT_PULLUP);
    }
 
    // disable LED L
    pinMode(13, OUTPUT);
    digitalWrite(13, LOW);
 
    // initialize serial communication at 9600 bits per second
    Serial.begin(9600);
}
 
 
// the loop routine runs over and over again forever
void loop()
{
    int state;
 
    // go through all button pins
    for (int i = 0; i < 10; i++)
    {
        // read the state of the pushbutton value
        state = digitalRead(buttonPins[i]);
        
        // recognize state changes: button pressed and button released
        if (state != buttonStates[i])
        {
            // remember new button state
            buttonStates[i] = state;
 
            // print out the state of the button
            Serial.print(buttonPins[i]);
            Serial.print(" State changed ");
            Serial.println(buttonStates[i]);
 
            // button is pressed
            if (buttonStates[i] == LOW)
            {
                // increment number of button pressed
                counters[i]++;
 
                // print out the number of button pressed
                Serial.print(buttonPins[i]);
                Serial.print(" counter: ");
                Serial.println(counters[i]);
            }
            // button is released
            else
            {
                // print out new line
                Serial.println();
 
                // wait before next click is recognized
                delay(100);
            }
        }
    }
}

It seems very well commented. Which part are you having trouble with?

The sketch prints some information about a button when that button becomes (goes from HIGH to LOW) pressed.

That is a general purpose counter sketch which records the number of times each button is pressed.

For your purpose, an MP3 player control, that is probably overkill but it does contain the classic elements of button debounce, watching state changes etc.

Presumably you'll be using a standard MP3 player module, maybe like this, and that dictates what buttons you require and their connections.

@CrossRoads, when I push the buttons, I don't see anything happening. So I'm trying to figure out what practical purpose does the sketch serve vis a vis the layout of the breadboard. Thanks.

OP's image

Show us a good image of your wiring.

You do have your serial monitor open and set to the right baud rate.

Hi everyone,

I'm attaching a picture of my breadboard. Also, when I open the serial monitor, I immediately get the following:

2 State changed 1

5 State changed 1

8 State changed 1

9 State changed 1

10 State changed 1

14 State changed 1

15 State changed 1

16 State changed 1

17 State changed 1

18 State changed 1

and then start pushing buttons one by one from bottom to top one by one, I get the following:

2 State changed 1

5 State changed 1

8 State changed 1

9 State changed 1

10 State changed 1

14 State changed 1

15 State changed 1

16 State changed 1

17 State changed 1

18 State changed 1

10 State changed 0
10 counter: 1
10 State changed 1

14 State changed 0
14 counter: 1
14 State changed 1

15 State changed 0
15 counter: 1
15 State changed 1

16 State changed 0
16 counter: 1
16 State changed 1

17 State changed 0
17 counter: 1
17 State changed 1

18 State changed 0
18 counter: 1
18 State changed 1

From what I can observe while I'm pushing the buttons, the first 4 buttons (at the bottom) give no response.

Thanks so much for your responses!

The power rails of your breadboard are split in the middle.

Jumper them.