Can you guys help me with this program?

I'm trying to make a poliphonic keyboard
it says "expected unqualified-id before ' { ' token on the line after "char customKey = customKeypad.getKey();"
below is the sketch
thanks!

#include<Keypad.h>

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

const byte ROWS = 7; //seven rows
const byte COLS = 8; //eight columns
const int SPEAKER=13;
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','4','5','6'},
{'7','8','9','a','b','c','d','e'},
{'f','g','h','i','j','k','l','m'},
{'n','o','p','q','r','s','t','u'},
{'w','x','y','z','A','B','C','D'},
{'E','F','G','H','I','J','K','L'},
{'M','N','O'},
};
byte rowPins[ROWS] = {2, 3, 4, 5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12, A2, A3, A1, A0}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

//declare variables
int tonePin = 8; //tone pin
int ctrlPin = 10; //control pin
int buttonPin = 12; //button pin

void setup(){
//set pins to output
pinMode(tonePin, OUTPUT);
pinMode(ctrlPin, OUTPUT);
pinMode(buttonPin, INPUT);
//set control pin to LOW
digitalWrite(ctrlPin, LOW);
}

void loop() {
//check if button is pressed and control pin is HIGH
if ( digitalRead(buttonPin) == HIGH && digitalRead(ctrlPin) == HIGH){
//play tone
tone(tonePin, 440);
}
//check if button is released and control pin is HIGH
else if(digitalRead(buttonPin) == LOW && digitalRead(ctrlPin) == HIGH) {
//stop playing tone
noTone(tonePin);
}
}
char customKey = customKeypad.getKey();
if(customKey){
Serial.println(customKey);
switch(customKey){
case'1':
tone(SPEAKER,NOTE_C2); break;
case'2':
tone(SPEAKER,NOTE_CS2); break;
case'3':
tone(SPEAKER,NOTE_D2); break;
case'4':
tone(SPEAKER,NOTE_DS2); break;
case'5':
tone(SPEAKER,NOTE_E2); break;
case'6':
tone(SPEAKER,NOTE_F2); break;
case'7':
tone(SPEAKER,NOTE_FS2); break;
case'8':
tone(SPEAKER,NOTE_G2); break;
case'9':
tone(SPEAKER,NOTE_GS2); break;
case'a':
tone(SPEAKER,NOTE_A2); break;
case'b':
tone(SPEAKER,NOTE_AS2); break;
case'c':
tone(SPEAKER,NOTE_B2); break;
case'd':
tone(SPEAKER,NOTE_C3); break;
case'e':
tone(SPEAKER,NOTE_CS3); break;
case'f':
tone(SPEAKER,NOTE_D3); break;
case'g':
tone(SPEAKER,NOTE_DS3); break;
case'h':
tone(SPEAKER,NOTE_E3); break;
case'i':
tone(SPEAKER,NOTE_F3); break;
case'j':
tone(SPEAKER,NOTE_FS3); break;
case'k':
tone(SPEAKER,NOTE_G3); break;
case'l':
tone(SPEAKER,NOTE_GS3); break;
case'm':
tone(SPEAKER,NOTE_A3); break;
case'n':
tone(SPEAKER,NOTE_AS3); break;
case'o':
tone(SPEAKER,NOTE_B3); break;
case'p':
tone(SPEAKER,NOTE_C4); break;
case'q':
tone(SPEAKER,NOTE_CS4); break;
case'r':
tone(SPEAKER,NOTE_D4); break;
case's':
tone(SPEAKER,NOTE_DS4); break;
case't':
tone(SPEAKER,NOTE_E4); break;
case'u':
tone(SPEAKER,NOTE_F4); break;
case'w':
tone(SPEAKER,NOTE_FS4); break;
case'x':
tone(SPEAKER,NOTE_G4); break;
case'y':
tone(SPEAKER,NOTE_GS4); break;
case'z':
tone(SPEAKER,NOTE_A4); break;
case'A':
tone(SPEAKER,NOTE_AS4); break;
case'B':
tone(SPEAKER,NOTE_B4); break;
case'C':
tone(SPEAKER,NOTE_C5); break;
case'D':
tone(SPEAKER,NOTE_CS5); break;
case'E':
tone(SPEAKER,NOTE_D5); break;
case'F':
tone(SPEAKER,NOTE_DS5); break;
case'G':
tone(SPEAKER,NOTE_E5); break;
case'H':
tone(SPEAKER,NOTE_F5); break;
case'I':
tone(SPEAKER,NOTE_FS5); break;
case'J':
tone(SPEAKER,NOTE_G5); break;
case'K':
tone(SPEAKER,NOTE_GS5); break;
case'L':
tone(SPEAKER,NOTE_A5); break;
case'M':
tone(SPEAKER,NOTE_AS5); break;
case'N':
tone(SPEAKER,NOTE_B5); break;
case'0':
tone(SPEAKER,NOTE_C6); break;
}
}
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You have code outside of any function, hence the error. If you use Auto Format in the IDE the problem will show up. Take a close look at the the loop() function

void loop()
{
  //check if button is pressed and control pin is HIGH
  if ( digitalRead(buttonPin) == HIGH && digitalRead(ctrlPin) == HIGH)
  {
    //play tone
    tone(tonePin, 440);
  }
  //check if button is released and control pin is HIGH
  else if (digitalRead(buttonPin) == LOW && digitalRead(ctrlPin) == HIGH)
  {
    //stop playing tone
    noTone(tonePin);
  }
}
char customKey = customKeypad.getKey();
if (customKey)
{
etc,etc

If you:

#include <pitches.h>

you can eliminate all the #define NOTE_nn ##` lines

(and... the forum bot says that I need to include a sentence of non-code to post the replay... so here it is.)

consider
some output

31
62
123
247
494
988
1976
3951
 1    31
 a    52
 A   233
const unsigned Npitch = 8*12;
unsigned pitch [Npitch];

void
genPitch (void)
{
    double p = 30.87;
    double k;

    k = pow(2, 1./12);
    for (unsigned n = 0; n < Npitch; n++)  {
        pitch [n] = p + 0.5;
        if (! (n % 12))
            Serial.println (pitch [n]); 
        p *= k;
    }
}

// -----------------------------------------------------------------------------
void loop() {
    if (Serial.available ())  {
        char c = Serial.read();
        int  n;

        if ('a' <= c)
            n = 9 + c - 'a';
        else if ('A' <= c)
            n = 9+26 + c - 'A';
        else 
            n = c - '1';

        char s [80];
        sprintf (s, " %c  %4d", c, pitch [n]);
        Serial.println (s);
    }
}


// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);
    genPitch ();
}

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