Buttons no longer working

Hello, I'm making a mini piano for a tech class, and the buttons stopped working recently. This setup was working for 8 buttons and 8 LEDs when I put it aside to get ready to create the soldered version. Now, I can't seem to get the buttons to make sound, nor do they light up the LEDs. I've tried many different versions of code in hopes to at least get the buttons to work. I just tried again, and now the LEDs aren't lighting up when the song plays either (it plays a song and says the name of the song on an LCD screen, then you play it back. The song plays and the LCD screen scrolls correctly). Does anything look incorrect on my breadboard? I only have 2 buttons set up right now to test again.

Here's the code I'm currently working with:

#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 493
#define NOTE_C5 523

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// LCD Setup (I2C Address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Pins
const int speakerPin = 12;  // Speaker connected via transistor
const int buttonPins[8] = {0, 1, 2, 3, 4, 5, 6, 7}; // Note buttons
const int ledPins[8] = {8, 9, 10, 11, A0, A1, A2, A3}; // LEDs

// Notes and Frequencies
const int melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // C4 to C5
const char *noteNames[] = {"C", "D", "E", "F", "G", "A", "B", "C5"};

// "Two Bits" Notes
const int song1Notes[] = {NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, 0, NOTE_B4, NOTE_C5};
const int song1LEDs[] = {0, 4, 0, 4, 0, 1, 0, 3};

// "Twinkle Twinkle Little Star" Notes
const int song2Notes[] = { NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, 0, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4,
  NOTE_C4};
const int song2LEDs[] = {0, 0, 4, 4, 5, 5, 4, 
                         3, 3, 2, 2, 1, 1, 0};

bool learnedTwoBits = false;  // Track song learning progress

void setup() {
    lcd.init();
    lcd.backlight();
    pinMode(speakerPin, OUTPUT);

    // Initialize LEDs and buttons
    for (int i = 0; i < 8; i++) {
        pinMode(ledPins[i], OUTPUT);
        pinMode(buttonPins[i], INPUT);
    }

    // Show welcome message
    scrollText("Learn Two Bits!");
    playSong(song1Notes, song1LEDs, 8);
}

void loop() {
    if (!learnedTwoBits) {
        scrollText("Your turn: Two Bits");
        checkUserInput(song1Notes, song1LEDs, 8);
        learnedTwoBits = true;
        scrollText("Now Twinkle!");
        delay(1000);
        playSong(song2Notes, song2LEDs, 14);
    } else {
        scrollText("Your turn: Twinkle");
        checkUserInput(song2Notes, song2LEDs, 14);
    }
}

// Scroll text across the LCD
void scrollText(String text) {
    lcd.clear();
    text = "    " + text + "    ";
    for (int i = 0; i < text.length() - 15; i++) {
        lcd.setCursor(0, 0);
        lcd.print(text.substring(i, i + 16)); 
        delay(250);
    }
}

// Play a song with LED guidance
void playSong(const int songNotes[], const int songLEDs[], int length) {
    for (int i = 0; i < length; i++) {
        tone(speakerPin, songNotes[i], 400);
        digitalWrite(ledPins[songLEDs[i]], HIGH);
        delay(500);
        digitalWrite(ledPins[songLEDs[i]], LOW);
        delay(100);
    }
    noTone(speakerPin);
    delay(1000);
}

// Let the user play along and validate their input
void checkUserInput(const int songNotes[], const int songLEDs[], int length) {
    for (int i = 0; i < length; i++) {
        bool correctNote = false;
        while (!correctNote) {
            for (int j = 0; j < 8; j++) {
                if (digitalRead(buttonPins[j]) == HIGH && melody[j] == songNotes[i]) {
                    tone(speakerPin, melody[j], 300);
                    digitalWrite(ledPins[j], HIGH);
                    scrollText(String("Note: ") + noteNames[j]);
                    digitalWrite(ledPins[j], LOW);
                    noTone(speakerPin);
                    delay(300);
                    correctNote = true;
                }
            }
        }
    }
}


Hi there,

First of all you can change

pinMode(buttonPins[i], INPUT);

to

pinMode(buttonPins[i], INPUT_PULLUP);

and remove the resistors, wire one side of the button directly to the pin, and the other to GND.

Besides that you have done the right thing to remove circuitry. To further troubleshoot, use Serial to send events to your computer. The IDE got a serial monitor you can use for that.

Oh and

digitalRead

must now detect LOW.

1 Like
  • Showing breadboard connections only makes volunteers jump through hoops, we are too old to jump :old_man:

  • Always show us a schematic (hand drawn is fine) and your connections image(s).

  • With switches like that, use diagonal leads when wiring.

  • Wire your LEDs explicitly, not though other circuit wiring.

  • Avoid using pins 0 and 1




FYI

1 Like

Thank you! I wasn't sure if it was INPUT_PULLUP or not. You mean for me to just remove the buttons resistors, is that correct? the LEDs still need them, I assume. And yeah, I've used the serial monitor but wasn't sure it'd tell me what I need to know this time. I'll try it!
For the digitalRead, should it be set to low in the setup()?

// Pins
const int speakerPin   = 12;  // Speaker connected via transistor
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Note buttons
const int ledPins[]    = {10, 11, A0, A1, A2, A3, A4, A5}; // LEDs

I see that you hardwired the LEDs to the buttons, it complicates things a bit, not sure it works even.

You insert a Serial.write after a digitalRead, and in that write for example ( "Button 2 was pressed" )

One thing that you will noticed sooner or later is that buttons have this bounce thing going. It means a button will not make firm contact right away but bounce a bit. There are debounce libraries that you can download in the IDE (v 2) and use instead of digitalRead.

I guess you can use this circuit to use the button for both LED and digitalRead

A4 and A5 are used for the LCD screen. I have no other pins other than 0 and 1

Don't use them when you troubleshoot then, since those are used for Serial.

It worked before, so I was surprised that it suddenly stopped working

Can you upload sketches to the UNO? Does it work if you power it by other means than through USB?

  • Lets see your schematic.
  • Then you cannot use Serial.prints( . . . ) for debugging :grimacing:

Hm, @bleauwonder uses an R4. That changes things a bit, right?

I don't need the LCD screen plugged in for debugging, just for the full setup. and yes, I use an R4 @ledsyn

Yes, I can upload just fine and the LEDs were lighting up per the code at first. I have a battery but I'm not sure how to connect it, to be honest

I think you can use 0 and 1 then. First serial port is present in a header on the board, second uses 0 and 1. You only need one :slight_smile:

Can you wire up a digital pin to a resistor + LED and to GND, and upload the Blink sketch? Make sure to match the pin number.

If you have a Multimeter that would be a useful tool for you to troubleshoot as well.

The blink sketch works, just checked all the pins and they all blinked successfully. I don't have a multimeter, unfortunately

I also just set up the turn on/off led with button and that's working well. Maybe I just need to try to set it up again. Thanks for the help!

1 Like

Ok great! Perhaps it was just a wire that got loose before.

If you are into electronics project, I recommend a multimeter, the swiss armyknife for electronics.

I'm happy to hear this, good luck with the mini-piano!