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;
}
}
}
}
}




