Help With Variables

Hello There, I Have started on a project, and am not sure how to do what I need.

What I need is for when somebody presses the "up" button it changes the pitch to the next pitch up, and the "down" button changes the pitch to the next pitch down, and If it reaches the last note on either side, it goes to the next pitch. EX:

F3?F#3?G3?G#3?A3?A#3?B3?C4?C#4?D4?D#4?E4?F4?F3

So when It reaches the top, it would repeat, and when it reaches the bottom, it would do the same, but loop to the top.

So far this is the code that I have, I have the pitches and frequencies, but I cannot figure out how to switch to the next pitch.
Code:

#include <LiquidCrystal.h>
#include <DFR_Key.h>

//Initialize Tones
/*
T1= F3, T2= F#3, T3= G3, T4= G#3, T5= A3, T6= A#3, T7= B3, T8= C4, T9=C#4,
T10= D4, T11= D#4, T12= E4, T13= F4.
*/
int const T1 = 175;
int const T2 = 185;
int const T3 = 196;
int const T4 = 208;
int const T5 = 220;
int const T6 = 233;
int const T7 = 247;
int const T8 = 262;
int const T9 = 277;
int const T10 = 294;
int const T11 = 311;
int const T12 = 330;
int const T13 = 349;
int currentNote = T1;
                
//Pin assignments for DFRobot LCD Keypad Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 
//---------------------------------------------

DFR_Key keypad;

int localKey = 0;
String keyString = "";
                 
void setup() 
{ 
  pinMode(50, OUTPUT);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Connor's Tuner");
  delay(2500);

  keypad.setRate(10);

}

void loop() 
{ 
  /*
  keypad.getKey();
  Grabs the current key.
  Returns a non-zero integer corresponding to the pressed key,
  OR
  Returns 0 for no keys pressed,
  OR
  Returns -1 (sample wait) when no key is available to be sampled.
  */
  localKey = keypad.getKey();
  
  if (localKey != SAMPLE_WAIT)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Current Note:");
    lcd.setCursor(0, 12);
  }
  if (localKey == 3)
  {
    currentNote = currentNote + 1;
  }
}

I have no idea if how I'm doing it is right, or if it is even possible.
Thanks for any help!

int const T1 = 175;
int const T2 = 185;
int const T3 = 196;
int const T4 = 208;
int const T5 = 220;
int const T6 = 233;
int const T7 = 247;
int const T8 = 262;
int const T9 = 277;
int const T10 = 294;
int const T11 = 311;
int const T12 = 330;
int const T13 = 349;

This should all be in an array. That way, you can increment your current counter and use it as the array index:

Thanks, I just tried putting it into an array to get it to work. Now the problem is getting the notes to change.
This is the code, but im not sure if im changing the value of the array correctly

 #include <LiquidCrystal.h>
 #include <DFR_Key.h>

 //Initialize Tones
 /*
 175= F3, 185= F#3, 196= G3, 208= G#3, 220= A3, 233= A#3, 247= B3, 262= C4, 227=C#4,
 294= D4, 311= D#4, 330= E4, 349= F4.
 */
 int notes[13] = {175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349};

 int currentNote = notes[0];
                
 //Pin assignments for DFRobot LCD Keypad Shield
 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 
 //---------------------------------------------

 DFR_Key keypad;

 int localKey = 0;
 String keyString = "";
                 
 void setup() 
 { 
   pinMode(50, OUTPUT);
   lcd.begin(16, 2);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Connor's Tuner");
   delay(2500);
   lcd.clear();

   keypad.setRate(10);

 }

 void loop() 
 { 
   /*
   keypad.getKey();
   Grabs the current key.
   Returns a non-zero integer corresponding to the pressed key,
   OR
   Returns 0 for no keys pressed,
   OR
   Returns -1 (sample wait) when no key is available to be sampled.
   */
   localKey = keypad.getKey();
  
   if (localKey != SAMPLE_WAIT)
   {
   
     lcd.setCursor(0, 0);
     lcd.print("Current Note:");
     lcd.setCursor(0, 12);
     lcd.print(currentNote);
   }
   if (localKey == 3)
   {
     currentNote = currentNote + 1;
   }
   if (localKey == 4)
   {
    currentNote = currentNote - 1; 
   }
   if (localKey == 1)
   {
    tone(50, currentNote); 
   }
 }

The problem is you have the array declared, and apart from setting currentNote to to it, you never reference the array again.

int notes[13] = {175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349};

int currentNote = notes[0];

Ok, so now currentNote is 175.

if (localKey == 3)
   {
     currentNote = currentNote + 1;
   }

So now currentNode is 176, but you don't have that anywhere in your notes array, so clearly that's not going to work.

currentNote should just be used as an index; it's values should be between 0 and 12.

Then, to get a specific note, you would use currentNote as the array index:

int noteToRun = notes[currentNote];

Maybe change int currentNote = notes[0]; to int currentNote = 0; and tone(50, currentNote); to tone(50, notes[currentNote]);

You should also do bounds checking of currentNote = currentNote + 1; and currentNote = currentNote - 1; to make sure they don't go above or bleow the number of elements in the notes[] array

Thanks guys, that got the code working with the selections. My only problem now is whenever I press a button, it will jump by more than one, and the display doesn't show the correct pitch number, but the pitches are correct.

 #include <LiquidCrystal.h>
 #include <DFR_Key.h>

 //Initialize Tones
 /*
 175= F3, 185= F#3, 196= G3, 208= G#3, 220= A3, 233= A#3, 247= B3, 262= C4, 227=C#4,
 294= D4, 311= D#4, 330= E4, 349= F4.
 */
 int notes[13] = {175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349};

 int currentNote = 0;
                
 //Pin assignments for DFRobot LCD Keypad Shield
 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 
 //---------------------------------------------

 DFR_Key keypad;

 int localKey = 0;
 String keyString = "";
                 
 void setup() 
 { 
   pinMode(50, OUTPUT);
   lcd.begin(16, 2);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Connor's Tuner");
   delay(2500);
   lcd.clear();

   keypad.setRate(10);

 }

 void loop() 
 { 
   /*
   keypad.getKey();
   Grabs the current key.
   Returns a non-zero integer corresponding to the pressed key,
   OR
   Returns 0 for no keys pressed,
   OR
   Returns -1 (sample wait) when no key is available to be sampled.
   */
   localKey = keypad.getKey();
  
   if (localKey != SAMPLE_WAIT)
   {
   
     lcd.setCursor(0, 0);
     lcd.print("Current Note:");
     lcd.setCursor(0, 12);
     lcd.print(currentNote);
   }
   if (localKey == 3)
   {
     currentNote = currentNote + 1;
   }
   if (localKey == 4)
   {
    currentNote = currentNote - 1; 
   }
   if (localKey == 1)
   {
    tone(50, notes[currentNote], 3000); 
   }
   if (currentNote > 12)
   {
    currentNote = 0; 
   }
   if (currentNote < 0)
   {
    currentNote = 12; 
   }
 }

Im not sure about the lcd part, but is the selection error something as simple as changing

keypad.setRate(10);

to a larger number?

I'm not familiar with the library but multiple jumps sounds like a button de-bounce problem.

You could replace

if (currentNote > 12)
   {
    currentNote = 0; 
   }
   if (currentNote < 0)
   {
    currentNote = 12; 
   }

with

currentNote = currentNote % 12

tab00:
Thanks guys, that got the code working with the selections. My only problem now is whenever I press a button, it will jump by more than one, and the display doesn't show the correct pitch number, but the pitches are correct.

Does the library return when a button is pushed down, or when a signal edge occurs? I'm guessing the former as that would explain why you are having an issue of multiple increments.

You need to store the last reading in a variable and check to see if it was 0 the last time you checked. If so, then the button has just been pushed down. You're looking for a signal edge to occur so that it only runs once for each button push.