Set of variables into one word?

Hi everyone! :slight_smile:

I'm new to Arduino, and I've been doing some minor projects from the project book. I'm currently working on the seventh project, the keyboard instruments, and I'm trying to enhance it. So far, I've added more notes, a volume control, a pitch pot and an arpeggio pot. I had to think really hard to get the arpeggio thing to work, but I was really happy when I found a way to make it work!

Anyway, my next idea is to add songs that will play when I press a single button, but typing each note is going to be a real hassle, so I've been trying to find a way not to have to type each "tone" variable. Is there a way to get something like "A4" being a short for:

tone (3, 440);
delay(100);

By the way, I know about the "pitches.h" library, but I wanted to try to make it work on my own!

Thanks! :slight_smile:

The crude way to do this would be to get user input into a variable, say userInput, then do

if (strcmp(userInput, "A4") == 0
{
  tone (3, 440);
  delay(100);
}

How many different user inputs do you envisage ?

I don't understand... What's "strcmp"?

And if I guess correctly, I think I should need 48 different inputs.

You could create a list like this

#define A4 440
#define A5 880

etc

Then the command becomes

tone(3, A4);


strcmp() is string compare -> compares char arrays, a.k.a. strings (which are different from Strings)

Wouldn't it be the same as this?

int A4 = 440;

And it defines the pitch of the note, but my problem is that I'd like to have both the pitch AND the lenth of the note in one keyword! Like A4h being a 440hz A half note, A4q being a 440hz A quarter note, etc...

int A4 = 440;
Requires ram
Define does not.

Also, see:

Oh, ok! Thanks for the explanation, Larry! :slight_smile:

I'm gonna try this #define thing...

I’ve tried with this:

#define A4h tone(8, 440); delay(798); noTone(8); delay(2);
#define A4q tone(8, 440); delay(398); noTone(8); delay(2);
#define A4e tone(8, 440); delay(198); noTone(8); delay(2);
#define A4s tone(8, 440); delay(98); noTone(8); delay(2);

void setup() {
}

void loop() {
  int buttons = analogRead(A0);
  if (buttons >= 1006 && buttons <= 1023) {
    A4q
    A4h
    A4q
    A4s
    A4s
    A4s
    A4s
    
    }
  else if (buttons >= 940 && buttons <= 950) {
    A4e
    A4s
    A4s
    A4s
    A4s
    A4q
    A4e
    A4e
    }
  else if (buttons <= 10){
    noTone(8);
    }
}

And it works flawlessly!!! Thanks a ton for your help, guys, you’re amazing!! :smiley:

Be very careful when using define.
See trap #13:

Yes, I saw that in your first link, thanks!

But in my case, the semicolons are required, right? Else I'd have "tone(8, 440) delay(798) noTone(8 ) delay(2)", and that wouldn't work, would it?

One pitfall might be

if (x ==2)
  A4h

What do you mean? I don't see any problem with your code...

Do the macro expansion, then see if you see a problem.

I don't understand... What's "strcmp"?

Is Google broken in your country ?

Uh... What's a macro expansion?
Don't forget I'm a total newbie! :stuck_out_tongue:

This #define A4h tone(8, 440); delay(798); noTone(8); delay(2); is a macro.
When the pre processor sees "A4h" after this, it replaces the text "A4h" with the text in the macro.
That is macro expansion.

michinyon:
Is Google broken in your country ?

I searched, but I don't understand the explanations... As you subtly pointed out, I'm not a native english speaker, so understanding technical stuff like that is quite hard! That's why I'm asking questions here! :slight_smile:

A #define tells the compiler to replace any occurrence of the name with the thing after it. So...this

if (x ==2)
  A4h

Becomes...

if (x ==2)
  tone(8, 440); 
  delay(798); 
  noTone(8); 
  delay(2);

Notice any problems here?

AWOL:
This

#define A4h tone(8, 440); delay(798); noTone(8); delay(2);

is a macro.
When the pre processor sees “A4h” after this, it replaces the text “A4h” with the text in the macro.
That is macro expansion.

Okaaay! So:

if (x ==2)
  A4h

would become:

if (x ==2)
  tone(8, 440); delay(798); noTone(8); delay(2);

Apart from the missing brackets, I don’t see the problem… Then again, I’m totally new to programming!

Bingo! no curly braces!