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! ![]()
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);
}
}
}
}

