using a pot to read from an array

Hello, I'm using this example code to understand how to take a reading from a pot and instead of reading those values, convert them to their note names using an array and then read them on the serial monitor...
Obviously what I have here isn't working but if anyone could point me in the right direction I'd be more than grateful!

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
int mynotes[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

int mVal1 = 0;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  int index = map(sensorValue, 0, 1023, 0, 12);
  
      int myNote = mynotes[index];
  // print out the value you read:
  Serial.println(myNote);
  delay(1);        // delay in between reads for stability
}
int mynotes[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

You set up an array of integers (int) and then try to fill it with things like “C#”. Does that really look like an integer to you?

Steve

Should it look something like this…

char *mynotes[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

Yes and this:

int index = map(sensorValue, 0, 1023, 0, 12);

Needs an 11 and not a 12. You have 12 notes, they are numbered 0 - 11.

Thanks for that but my readings are still numbers up in the 500’s and it’s not reading from the array still.
Probably a stupid question but must there always be a for loop for an array to work?

I don't ever use map fx. I'd divide instead. Probably want to add a little hysteresis too, so it "locks" into notes. Eg... Goes from 485 to wherever the division is, then expand the bounds of that slot..
Say 24 on each side.

Thanks but that's the least of my troubles at the moment. I really don't get how to read from the array.

How do you have things wired? Can you draw a quick schematic and post it? Put a line in to print the sensorValue before the map to make sure what it reads.

Post the current code with the changes you've made.

It's simple really. The only thing I have wired is a pot to analog input A0.
The sketch is originally from the Arduino IDE (AnalogReadSerial) and I've added what I thought might be needed from other sketches to get my idea working. It seems like it should be simple to execute the concept but I haven't found anything yet that remotely mimics this idea which is frustrating.

This is the AnalogReadSerial sketch unmodified

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

As it stands with my posted code, when reading the pot's data through the serial monitor I'm getting a reading of 557-2573 through the pot's rotation.

this is the last thing i did with some modification

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
char * mynotes[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

int mVal1 = 0;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  int index = map(sensorValue, 0, 1023, 0, 11);
 
      int myNote = mynotes[index];
  // print out the value you read:
  Serial.println(myNote);
  delay(1);        // delay in between reads for stability
}

Now the pot reads....557-584

Change your print to reflect the fact that yours is now an array of NULL-terminated strings:

Serial.println( myNotes[index] );

Thanks Blackfin....That was the missing piece!! Now functioning as needed.

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
 char *mynotes[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

int mVal1 = 0;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  int index = map(sensorValue, 0, 1023, 0, 11);
 
      int myNote = mynotes[index];
  // print out the value you read:
  //Serial.println(myNote);
  Serial.println( mynotes[index] );
  delay(1);        // delay in between reads for stability
}

@goatboyrobbie,

I think you may run into problem with your basic idea of using a Pot – especially without adding hysteresis as suggested in Reply #5.

You’ll invariably land on a spot that lies right at the threshold between 2 values of the map() function’s return value. By nature of analog ADC readings, the computed digital values will jump between values around this threshold due to noise causing quantization errors. Hysteresis will help this. But, a better solution would be using a digital input such as a rotary encoder.

Thanks gfvalvo. I used this piece of code just to get the pot/array working before I introduce it into another piece of code I'm working on. While the other piece of code uses a single pot to select a midi note, a button then commits that note to memory so I'm not worried about using hysteresis for now (that is to say that I can hear the note which I'm selecting). The project will need an LCD and so I've thought about using rotary encoders for scrolling screens and inputting data but it seems like you need a lot of code just to make them work.