Adding octave buttons for piano?

but my problem is that how turn on and off the sound repeatedly

You should post all the code you are working on.

The code in reply #18 had no limits on the value that octave could have. It also had no way of getting the octave down.

If I am to use +12 it will shift 12 octaves.

Are you still multiplying by 12 in the note assignment loop? That seems to me the only way that would happen.

Hi Mike,

Here is the full code:

#include <CapacitiveSensor.h>

#define numberOfInputs 8       //8 inputs for 'keys' buttons and two for octave up and down.
#define Thresshold 300         //Define the thresshold 
#define octave_buttons 2        //Define octave buttons
#define semi_tone 1.05946309435929

long total_octave[octave_buttons];
long total[numberOfInputs];
long lastTotal[numberOfInputs];
boolean midi_on[numberOfInputs];
boolean midiMode = true;       // if midiMode is false, the Arduino is in Debug mode. Make sure to put the midiMode to true if you want the Arduino to act as a Midi device

int octave = 0;


CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);       
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2,5);
CapacitiveSensor   cs_2_6 = CapacitiveSensor(2,6);
CapacitiveSensor   cs_2_7 = CapacitiveSensor(2,7);
CapacitiveSensor   cs_2_8 = CapacitiveSensor(2,8);
CapacitiveSensor   cs_2_9 = CapacitiveSensor(2,9);
CapacitiveSensor   cs_2_10 = CapacitiveSensor(2,10);
CapacitiveSensor   cs_2_11 = CapacitiveSensor(2,11);
CapacitiveSensor   cs_2_12 = CapacitiveSensor(2,12);


////////////////////////////////////////////////////////////

void setup() {
  if (midiMode) {
    Serial.begin(31250);
  }
  else {
    Serial.begin(9600);
  } 
}

////////////////////////////////////////////////////////////

void loop() {
   
  total[0] =   cs_2_3.capacitiveSensor(60);            
  total[1] =   cs_2_4.capacitiveSensor(60);
  total[2] =   cs_2_5.capacitiveSensor(60);
  total[3] =   cs_2_6.capacitiveSensor(60);              
  total[4] =   cs_2_7.capacitiveSensor(60);
  total[5] =   cs_2_8.capacitiveSensor(60);
  total[6] =   cs_2_9.capacitiveSensor(60);             
  total[7] =   cs_2_10.capacitiveSensor(60);
  // Set capacitive sensor for octave buttons.
  total_octave[0] =   cs_2_11.capacitiveSensor(60);
  total_octave[1] =   cs_2_12.capacitiveSensor(60);


////////////////////////////////////////////////////////////

  for( int i = 0; i < numberOfInputs; i++) {             //Send a MIDI message for the corresponding input
    if (total[i] > Thresshold && !midi_on[i]) {
      sendMidi(0x90, 60 + i + octave, 127);                      
      midi_on[i] = true;
    }
    if (total[i] < Thresshold && midi_on[i]) {
      sendMidi(0x90, 60 + i + octave, 0);                         //turn note off
      midi_on[i] = false;
    }
  }

////////////////////Set octave up button ///////////////////
 
    if (total_octave[0] > Thresshold) {
      octave = semi_tone + 1;                    
    }         
    if (total_octave[1] > Thresshold) {
      octave = semi_tone - 1;                    
    }         
  }
/////////////////////send MIDI message//////////////////////

void sendMidi(int Statusbyte, int Databyte1, int Databyte2) {
  if (midiMode) {
    Serial.write(Statusbyte);//send note on or note off command 
    Serial.write(Databyte1);//send pitch data
    Serial.write(Databyte2);//send velocity data
  }
  else { 
    if (Databyte2 == 127) {
      Serial.println("Note On");
    }
    else {
      Serial.println("Note Off");
      Serial.println("pitch data");
    }
  }
}

I have managed to shift the octave up in semitones (I decided this would be better for me as it will be more flexible) by using:

#define semi_tone 1.05946309435929

if (total_octave[0] > Thresshold) {
      octave = semi_tone + 1;                    
    }         
    if (total_octave[1] > Thresshold) {
      octave = semi_tone - 1;                    
    }

This works well, the only problem I have now is it will only shift up or down 1 semitone and the button will not work anymore. I guess this is because it needs to be a continuous button loop (sorry if that is not the right terminology). Any ideas as to why?

Are you still multiplying by 12 in the note assignment loop? That seems to me the only way that would happen.

No, this is why it is perplexing me?

The code in reply #18 had no limits on the value that octave could have. It also had no way of getting the octave down.

How do I go about setting a limit to the amount of shifts for up and down?

Thanks, Mike!

How do I go about setting a limit to the amount of shifts for up and down?

I told you in reply #12

#define semi_tone 1.05946309435929

WHAT!!
Give that you do:-

 octave = semi_tone + 1;

What effect do you think adding a floating point number to an integer will be?
You will never get octave to be anything other than 0 or 2

You seem to be going backwards.
I would forget for the moment trying to get the octave change with a cap sense input, just wire in a push button until you get the logic of the code correct.

What effect do you think adding a floating point number to an integer will be?
You will never get octave to be anything other than 0 or 2

I guess I'm trying some things out and learning as I go along. Until a week a go I was completely new to this so I'm not too phased about making mistakes, it's the only way I'll learn.

I'll keep going with the capsense until I get it. I'm stubborn like that. :slight_smile:

Thanks for your help so far, Mike!

I'll keep going with the capsense until I get it. I'm stubborn like that.

When you get more experience you will learn not to fight a battle on two fronts.

I did not say abandon the idea of using capsense, but tackle your problems one at a time. At the moment you are making a pigs ear of the octave switch, so it makes little sense in trying to solve that with a flaky switch.

Stubbornness is not a virtue in these situations. It is not macho to try and do too much, you just end up getting frustrated. You need to learn this more that the minutia of coding because without it you will never get any satisfaction from this pass time.

I hear what you are saying. I'm not saying that to be 'macho', it's just the way I am. I will do something until I learn it. I intend to do more learning before I start a new project so I will have a better understanding of what I'm trying to accomplish, and how to implement it.

For now, I seemed to have solved my issue. I added 'contrain' to limit the semitone shift like so:

octave = constrain(octave, -4, +6);
    if (total_octave[0] > Thresshold) {
      octave = octave + 1; 
      delay(500);                   
    }         
    if (total_octave[1] > Thresshold) {
      octave = octave - 1;  
      delay(500);

This may not be the best way of doing things, but it works well.

So everything is working. Thanks for the help, Mike!

Just a question.
What key do you think this keyboard is playing?

PolyKrome:
I'm still having trouble with the the octave up shifting from C3 to D4 and so on. I am having to use:

octave = octave + 1

in order to shift just one octave ...... If I am to use +12 it will shift 12 octaves.

This I think is the key to the problem. The MIDI system can not cope with 12 octaves, it can only cope with 11 octaves over the whole range. You are adding a number based on the key pressed to 60. Now 60 is middle C and the MIDI system can only cope with five octaves above this value, a MIDI note number of 120. Therefore you simply can not have a shift of 12 octaves on a MIDI system.

Therefore I would suggest that you don't know what an octave actually is. It is a doubling in frequency, or in terms of MIDI it is the equivalent of adding 12 to the note number. I suggest that what you think is an octave is in fact a semitone.

Your code is also faulty in that it is not even playing the 8 notes from the keyboard correctly.
To make 8 notes cover an octave you do not have a consistent space between each note. You certainly do not have a space of one MIDI note number, because that is a semitone. That is why I asked you about what key you thought your keyboard was playing in. Basically it is not playing in any key at all that is recognised in music.

To play in the key of C, starting at middle C you have to play the MIDI notes:-
60, 62, 64, 65, 67, 69, 71
the next octave above starts at MIDI note number 72, that is adding 12 from the first base note of 60. Notice how these notes are normally separated by two MIDI not numbers, but on two occasions are only separated by one note. That is between 64 & 64 and 71 & 72.

Once you understand this you should be able to sort out your:-

(with the bug still happening - C3 to D4)

As not actually being a problem but as a result in expecting something that is not going to happen due to the nature of how musical notes work.