How do I get sound to play with lights on button press

I'm having some trouble figuring out how to add a music note sound to each button that also has an LED for light effect. I'll show you what I have... Gee I wish I had a brain for this, I think it's coming along but I pretty much just chopped the music part into button LED part and it verified, but what came out wasn't music but something similar to an MRI machine lol. Help would be much appreciated, also hopefully I learn....

code as follows:

int speakerPin = 33;
int delayValue = 100;

void setup() {
for(int i=3;i<=10;i++)
{
 pinMode(i,INPUT);
 digitalWrite(i,LOW);
}
 pinMode(speakerPin,OUTPUT);

 pinMode(8, INPUT);
 pinMode(9, INPUT);
 pinMode(10, INPUT);

 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
}

void loop() {

 int currentTone=0;

 int redbutton;
 int yellowbutton;
 int bluebutton;

for(int i=3;i<=10;i++)

{
 if(digitalRead(i)==HIGH)
 {
   currentTone=i;
 }
{
switch(currentTone)
{
 case 8:
 tone(speakerPin,262);
 delay(delayValue);
 break;

 case 9:
 tone(speakerPin,294);
 delay(delayValue);
 break;

 case 10:
 tone(speakerPin,330);
 delay(delayValue);
 break;

redbutton = digitalRead(8);
 yellowbutton = digitalRead(9);
 bluebutton = digitalRead(10);

 if(redbutton == LOW){
   digitalWrite(3, HIGH);
 }
 else if(redbutton == HIGH){
   digitalWrite(3, LOW);
 }
 if(yellowbutton == LOW){
   digitalWrite(4, HIGH);
 }
 else if(yellowbutton == HIGH){
   digitalWrite(4, LOW);
 }
 if(bluebutton == LOW){
   digitalWrite(5, HIGH);
 }
 else if(bluebutton == HIGH){
   digitalWrite(5, LOW);  
 }
}
}
}}

If You format the code You might see what's wrong.

You know you can do:

  digitalWrite(5, ! digitalRead(10));

to invert the buttons for the LEDs?

I wasn't even sure I did the speaker right but I'll keep working at it.

I moved a wire still kind of the same stuff going on. I know to code is totally wrong but it feels like the right idea...

The following code, in the switch/case construct, is never executed, as there is no path to it.

In the Arduino IDE, go to preferences and turn on "verbose" compiler output to see the warnings.

Take a close look at your use of braces. It will help a lot to use CTRL-T in the code editor to format the code.

  break;
 
 redbutton = digitalRead(8);
 yellowbutton = digitalRead(9);
 bluebutton = digitalRead(10);

Do an internet search for something like "arduino piano" to see similar projects.

Here it is all better! I could of maybe done the led in a series with each button but this way works. the code looks way better too.

#define NOTE_C 262
#define NOTE_D 294
#define NOTE_E 330
#define NOTE_F 349

#define ACTIVATED LOW

const int PIEZO = 8;
const int LED = 13;
const int LEDred = 7;
const int LEDyellow = 6;
const int LEDgreen = 5;
const int LEDblue = 4;

const int BUTTON_C = 12;
const int BUTTON_D = 11;
const int BUTTON_E = 10;
const int BUTTON_F = 9;

void setup() 
{
  pinMode(LEDred,OUTPUT);
  pinMode(LED,OUTPUT);
  pinMode(BUTTON_C, INPUT);
  digitalWrite(BUTTON_C,HIGH);

  pinMode(LEDyellow,OUTPUT);
  pinMode(BUTTON_D, INPUT);
  digitalWrite(BUTTON_D,HIGH);

  pinMode(LEDgreen,OUTPUT);
  pinMode(BUTTON_E, INPUT);
  digitalWrite(BUTTON_E, HIGH);

  pinMode(LEDblue, OUTPUT);
  pinMode(BUTTON_F, INPUT);
  digitalWrite(BUTTON_F, HIGH);

  digitalWrite(LED,LOW);
}

void loop() 
{
  while(digitalRead(BUTTON_C) == ACTIVATED)
  {
    tone(PIEZO,NOTE_C);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDred,HIGH);
  }
  while(digitalRead(BUTTON_D) == ACTIVATED)
  {
    tone(PIEZO,NOTE_D);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDyellow,HIGH);
  }
  while(digitalRead(BUTTON_E) == ACTIVATED)
  {
    tone(PIEZO,NOTE_E);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDgreen,HIGH);
  }
  while(digitalRead(BUTTON_F) == ACTIVATED)
  {
    tone(PIEZO,NOTE_F);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDblue,HIGH);
  }
  noTone(PIEZO);
  digitalWrite(LED,LOW);
  digitalWrite(LEDred,LOW);
  digitalWrite(LEDyellow,LOW);
  digitalWrite(LEDgreen,LOW);
  digitalWrite(LEDblue,LOW);
}

Hehe now here it is with 8 light 8 notes to 8 buttons! I'm finally getting this solid. Much better, time to celebrate lol. Though I wish I could stick in a potentiometer and bend the notes something like, 'If button pressed while pot is turned this way it will make it D#, turn it the other way this will make it D flat and so on... Also I couldn't send a video with the sound and all maybe I'll post this up some time in YouTube when I feel like it :smile:

But now I'm working on a potentiometer and getting several buttons pressed at the same time for chords I'm slowly getting there, but any ideas?

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

#define ACTIVATED LOW

const int PIEZO = 37;
const int LED = 13;

const int LEDred1 = 45;
const int LEDyellow1 = 44;
const int LEDgreen1 = 43;
const int LEDblue1 = 42;

const int LEDred2 = 41;
const int LEDyellow2 = 40;
const int LEDgreen2 = 39;
const int LEDblue2 = 38;
//
const int BUTTON_C4 = 53;
const int BUTTON_D4 = 52;
const int BUTTON_E4 = 51;
const int BUTTON_F4 = 50;

const int BUTTON_G4 = 49;
const int BUTTON_A5 = 48;
const int BUTTON_B5 = 47;
const int BUTTON_C5 = 46;

void setup() 
{
  pinMode(LED,OUTPUT);
  pinMode(LEDred1,OUTPUT);
  pinMode(BUTTON_C4, INPUT);
  digitalWrite(BUTTON_C4,HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDyellow1,OUTPUT);
  pinMode(BUTTON_D4, INPUT);
  digitalWrite(BUTTON_D4,HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDgreen1,OUTPUT);
  pinMode(BUTTON_E4, INPUT);
  digitalWrite(BUTTON_E4, HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDblue1, OUTPUT);
  pinMode(BUTTON_F4, INPUT);
  digitalWrite(BUTTON_F4, HIGH);
//
    pinMode(LED,OUTPUT);
  pinMode(LEDred2,OUTPUT);
  pinMode(BUTTON_G4, INPUT);
  digitalWrite(BUTTON_G4,HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDyellow2,OUTPUT);
  pinMode(BUTTON_A5, INPUT);
  digitalWrite(BUTTON_A5,HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDgreen2,OUTPUT);
  pinMode(BUTTON_B5, INPUT);
  digitalWrite(BUTTON_B5, HIGH);

  pinMode(LED,OUTPUT);
  pinMode(LEDblue2, OUTPUT);
  pinMode(BUTTON_C5, INPUT);
  digitalWrite(BUTTON_C5, HIGH);

  digitalWrite(LED,LOW);
}

void loop() 
{
  while(digitalRead(BUTTON_C4) == ACTIVATED)
  {
    tone(PIEZO,NOTE_C4);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDred1,HIGH);
  }
  while(digitalRead(BUTTON_D4) == ACTIVATED)
  {
    tone(PIEZO,NOTE_D4);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDyellow1,HIGH);
  }
  while(digitalRead(BUTTON_E4) == ACTIVATED)
  {
    tone(PIEZO,NOTE_E4);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDgreen1,HIGH);
  }
  while(digitalRead(BUTTON_F4) == ACTIVATED)
  {
    tone(PIEZO,NOTE_F4);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDblue1,HIGH);
  }
  //
    while(digitalRead(BUTTON_G4) == ACTIVATED)
  {
    tone(PIEZO,NOTE_G4);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDred2,HIGH);
  }
  while(digitalRead(BUTTON_A5) == ACTIVATED)
  {
    tone(PIEZO,NOTE_A5);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDyellow2,HIGH);
  }
  while(digitalRead(BUTTON_B5) == ACTIVATED)
  {
    tone(PIEZO,NOTE_B5);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDgreen2,HIGH);
  }
  while(digitalRead(BUTTON_C5) == ACTIVATED)
  {
    tone(PIEZO,NOTE_C5);
    digitalWrite(LED,HIGH);
    digitalWrite(LEDblue2,HIGH);
  }
  {
  
  noTone(PIEZO);
  digitalWrite(LED,LOW);
  digitalWrite(LEDred1,LOW);
  digitalWrite(LEDyellow1,LOW);
  digitalWrite(LEDgreen1,LOW);
  digitalWrite(LEDblue1,LOW);

  digitalWrite(LEDred2,LOW);
  digitalWrite(LEDyellow2,LOW);
  digitalWrite(LEDgreen2,LOW);
  digitalWrite(LEDblue2,LOW);
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.