Switch Case and loops

Hey everyone, I am trying to associate the digital pins (and some of the analog ones) on my arduino UNO to a certain frequency. (i.e when it sees that this button is pushed, it does this sound). This is the code I have so far:

void setup() {     
Serial.begin(9600);  
  pinMode(13, OUTPUT); 
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, INPUT);
  pinMode(9, INPUT);
  pinMode(8, INPUT);
  pinMode(7, INPUT);
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(4, INPUT);
  pinMode(3, INPUT);
  pinMode(A1, INPUT);
  pinMode(A0, INPUT);
}
int C4 = 1911;
int CS4 = 1804;
int D4 = 1703;
int DS4 = 1607;
int E4 = 1517;
int F4 = 1433;
int FS4 = 1351;
int G4 = 1276;
int GS4 = 1204;
int LA4 = 1136;
int LAS4 = 1073;
int B4 = 1012;

int note = 0;
int c = 0;
int cs = 0;
int d = 0;
int ds = 0;
int e = 0;
int f = 0;
int fs = 0;
int g = 0;
int gs = 0;
int a = 0;
int as = 0;
int b = 0;


void loop() {
  
  c = digitalRead(A0);
  cs = digitalRead(A1) * 2;
  d = digitalRead(3) * 3;
  ds = digitalRead(4) * 4;
  e = digitalRead(5) * 5;
  f = digitalRead(6) * 6;
  fs = digitalRead(7) * 7;
  g = digitalRead(8) * 8;
  gs = digitalRead(9) * 9;
  a = digitalRead(10) * 10;
  as = digitalRead(11) * 11;
  b = digitalRead(12) * 12; 
 note = c + cs + d + ds + e + f + fs + g + gs + a + as + b; 

  switch(note){
    
    case 1:
    digitalWrite(13, HIGH); 
delayMicroseconds(C4);
digitalWrite(13, LOW);
delayMicroseconds(C4);
break;

    case 2:
    digitalWrite(13, HIGH); 
delayMicroseconds(CS4);
digitalWrite(13, LOW);
delayMicroseconds(CS4);
break;

    case 3:
    digitalWrite(13, HIGH); 
delayMicroseconds(D4);
digitalWrite(13, LOW);
delayMicroseconds(D4);
break;

    case 4:
    digitalWrite(13, HIGH); 
delayMicroseconds(DS4);
digitalWrite(13, LOW);
delayMicroseconds(DS4);
break;

    case 5:
    digitalWrite(13, HIGH); 
delayMicroseconds(E4);
digitalWrite(13, LOW);
delayMicroseconds(E4);
break;

    case 6:
    digitalWrite(13, HIGH); 
delayMicroseconds(F4);
digitalWrite(13, LOW);
delayMicroseconds(F4);
break;

    case 7:
    digitalWrite(13, HIGH); 
delayMicroseconds(FS4);
digitalWrite(13, LOW);
delayMicroseconds(FS4);
break;

    case 8:
    digitalWrite(13, HIGH); 
delayMicroseconds(G4);
digitalWrite(13, LOW);
delayMicroseconds(G4);
break;

    case 9:
    digitalWrite(13, HIGH); 
delayMicroseconds(GS4);
digitalWrite(13, LOW);
delayMicroseconds(GS4);
break;

    case 10:
    digitalWrite(13, HIGH); 
delayMicroseconds(LA4);
digitalWrite(13, LOW);
delayMicroseconds(LA4);
break;

    case 11:
    digitalWrite(13, HIGH); 
delayMicroseconds(LAS4);
digitalWrite(13, LOW);
delayMicroseconds(LAS4);
break;

    case 12:
    digitalWrite(13, HIGH); 
delayMicroseconds(B4);
digitalWrite(13, LOW);
delayMicroseconds(B4);
break;
}
}

This code does not work. I assume that it's because the cases don't loop, so the square wave is not created. If I just use this piece of code alone in void loop it gives me the right tone:

   digitalWrite(13, HIGH); 
delayMicroseconds(LA4);
digitalWrite(13, LOW);
delayMicroseconds(LA4);

How do I fix this? Is there a way to make a part of the void loop repeat itself untill it sees something else?

Thanks for helping a newbie out!

Cheers

This code does not work. I assume that it's because the cases don't loop, so the square wave is not created. If I just use this piece of code alone in void loop it gives me the right tone:

That's correct. The switch statement is meant to "do this or this or this or this". That appears to be not what you want.

Each switch appears to correspond with a note. So, what does this mean:

 note = c + cs + d + ds + e + f + fs + g + gs + a + as + b;

in terms a piano, for instance, can understand/reproduce?

After this line...

 note = c + cs + d + ds + e + f + fs + g + gs + a + as + b;

add...

Serial.println(note);

Upload the sketch, then start the serial monitor. See if the result is what you expect.

You might also want to look at http://arduino.cc/en/Reference/Tone

Yes, it does give me what I expect. The problem is not with detecting the which button is pressed, but generating the sound.

note = c + cs + d + ds + e + f + fs + g + gs + a + as + b;

Each note is associated with a number (c = 1 , cs = 2 , d = 3, ds = 4 ...) so that the value of int note would be the key I want.

I see why the switch case command does not work. What should I use instead?

Thanks for your time

Try http://arduino.cc/en/Reference/Tone. You already have some frequencies, although if by C4, you mean middle C, you're way too high.

I didn't use the frequencies in my sketch, it was the time it takes for a half wavelenght. Anyways, I checked out the note command. Would that work in Switch case? For example:

switch(note){
    
    case 1:
    tone(13,C4);
    if(note != 1){
      noTone(13);
    }
    break;

    case 2:
    tone(13,CS4);
    if(note != 2){
      noTone(13);
    }
    break;

case 3:
...
etc...

Would this work? Is it going to repeat itself and make a tone? and if so, will it play another one when another button is pressed?

Thanks everyone, I really appreciate it

Each note is associated with a number (c = 1 , cs = 2 , d = 3, ds = 4 ...) so that the value of int note would be the key I want.

What happens if TWO switches/keys are pressed?

If the idea is that only one switch/key will be pressed at a time, then the switch statement should work for you. Although it is massive overkill.

The note value should be the index into an array of times:

int times[] = {0, 1911, 1804, 1703, 1607, 1517, 1433, 1351, 1276, 1204, 1136, 1073, 1012};
    digitalWrite(13, HIGH); 
    delayMicroseconds(times[note]);
    digitalWrite(13, LOW);
    delayMicroseconds(times[note]);

Now, the thing that you apparently want is to the output to continue for some period of time, or until another switch/key is pressed, right?

If so, then note will be 0 when no key is pressed, so add another global variable, index, and set it to note when note is not 0:

    if(note > 0)
    {
       index = note;
    }
    digitalWrite(13, HIGH); 
    delayMicroseconds(times[index]);
    digitalWrite(13, LOW);
    delayMicroseconds(times[index]);

This way, no key pressed will result in note = 0, and nothing will happen to pin 13. If a key is pressed, note will change, a new index assigned and time selected.

Thank you very much, I have some studying to do on arrays!