OCTAVE BUTTON

byte noteON = 144;
int numbts = 13;
int bts[13];
boolean btgs[13];
int note_no[] = {60,61,62,63,64,65,66,67,68,69,70,71};
 boolean octaveUp ;
 boolean octaveDown ;
// button layout
// 13 12 11 10
// 9  8  7  6
// 5  4  3  2

// notes layout Flstudio
// 0  1  2  3
// 12 13 14 15
// 24 25 26 27

void setup() {
  pinMode(octaveUp, INPUT);
  pinMode(octaveDown,INPUT);
  octaveDown = digitalRead(14);
  octaveUp= digitalRead(15);
  
  Serial.begin(9600);
  for(int i=0; i<numbts; i++) bts[i] = i+2;
  for(int i=0; i<numbts; i++) btgs[i] = false;
  for(int i=0; i<numbts; i++) pinMode(bts[i], INPUT_PULLUP);
}
void loop() {
  

  for(int i=0; i<numbts; i++){
    if (!btgs[i]) {
      if (digitalRead(bts[i])==LOW ) {
        if (octaveUp == LOW){
          MIDImessage(noteON, note_no[i]+12, 127);
          delay(2);//crude form of button debouncing
        btgs[i] = true;}
        else if(octaveDown == LOW){
          MIDImessage(noteON, note_no[i]-12, 127);
        delay(2);//crude form of button debouncing
        btgs[i] = true;
        }
        else {
          MIDImessage(noteON, note_no[i], 127);
        delay(2);//crude form of button debouncing
        btgs[i] = true;}
      }
    else {
      if (digitalRead(bts[i])==HIGH) {
        MIDImessage(noteON, note_no[i], 0);          // Turn them both off
        delay(2);//crude form of button debouncing
        btgs[i] = false;
      }
    }
  }
}
}
//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

This is my code for 12 button midi with 2 extra buttons for octave Up and Down. the code compiles well but when i upload the code to my board the TX led lights up and gives random signals to my daw. i am struck here guys Help!

  pinMode(octaveUp, INPUT);
  pinMode(octaveDown,INPUT);
  octaveDown = digitalRead(14);
  octaveUp= digitalRead(15);

Are octaveUp and octaveDown pin numbers or not?

What, exactly, is connected to pins 14 and 15? How, EXACTLY, are those things wired?

The pin 14 and 15 stamds for analog A0 and A1. I read this over this forum. I been searching for Answers from a long time. I need to know where am I going wrong. And if anyone spots any mistake do point it out. Help appreciated

And if anyone spots any mistake do point it out.

Do you intend to ignore other people, too?

I'll have a try. This makes no sense:

  pinMode(octaveUp, INPUT);
  pinMode(octaveDown,INPUT);
  octaveDown = digitalRead(14);
  octaveUp= digitalRead(15);

What do you think is the result of setting pinMode for something defined as a boolean?

Steve

What do you think is the result of setting pinMode for something defined as a boolean?

Since the default value for a boolean is false, pin false (0) is set as an input pin. Twice.

So what should be a correct way to make this code work. If the oct up is pressed once it should C5 to C6 and if you press it again C6 to C7. No I am not ignoring anyone I am new to this programming though but thought to try something out maybe. By the way Thanks for your response. But do let me know how can I write this code.

PaulS:
Since the default value for a boolean is false, pin false (0) is set as an input pin. Twice.

I knew that! But I'm still not sure the OP gets it.

Steve

GKA:
So what should be a correct way to make this code work. If the oct up is pressed once it should C5 to C6 and if you press it again C6 to C7. No I am not ignoring anyone I am new to this programming though but thought to try something out maybe. By the way Thanks for your response. But do let me know how can I write this code.

If we say something you don't understand then ask. We'd like you to learn something. If we just give you working code all you will have learned is that you can get away without bothering to learn how to write your own programs.

  1. You need to define the PINS used for the two octave buttons.
  2. Currently you are adding 12 to MIDI note only if the octave button is being held on at the exact time that the message is sent. What you need to do is remember (in a variable) when each button is pressed and how often they have been pressed to decide what the offset is i.e. +12 for C5, +24 for C6, 0 for C4 (MIDI note 60 = C4) etc.

When you've done those we can look at the rest of it.

Steve

Yeah just guide me. I will definitely try my best. Slipstick what u mean to say is I need to define the pins right. so my question is do I need to change the arrary of 13 ? Is digitalread is good idea for analog pins? Because I am connecting a button to pin A0 and the other end to gnd.

Is digitalread is good idea for analog pins? Because I am connecting a button to pin A0 and the other end to gnd.

A0 is the name of the first analog pin being used as a digital pin. The pin is NOT being used as an analog pin.

So, are octaveUp and octaveDown pin numbers or pin states? If they are pin numbers, they need default values other than false. If they are pin states, they do not get used in pinMode() calls.

Boolean octaveUp = A0;
Boolean octaveDown = A1;
pinMode(octaveUp, INPUT);
pinMode(octaveDown ,INPUT);

Actually the definition of octaveUp and down is like this I made a mistake there. And as u said paul the A0 is same as digital pin. So the ocatve variable can be assigned the different value as other languages right.

define your pin numbers sensibly, then the code will read better:

#define octaveDownPin 14
#define octaveUpPin 15

void setup() {
  pinMode(octaveUpPin, INPUT);
  pinMode(octaveDownPin,INPUT);
  octaveDown = digitalRead(octaveDownPin);
  octaveUp= digitalRead(octaveUpPin);

Incidentally why are you reading pins in setup() - that's for setup, not actually running of the sketch,
so keep the pinMode calls there and do the pin reads from loop() or a function that loop() calls.

Ohh I see. I didn't knew that thanks Mark and what about the rest of it ? I mean the conditions I wrote in code are they correct??

Post the latest version so we are all on the same page.

I mean the conditions I wrote in code are they correct??

No. You need to post your code again, after using Tools + Auto Format, so we can see that you understand the difference between pin numbers and pin states.

Variables that are of type boolean should be used to hold true or false. HIGH and LOW values should be stored in variables of type byte or uint8_t.

#define octaveUp  14 
#define octaveDown 15 
byte noteON = 144;
int numbts = 13;
int bts[13];
boolean btgs[13];
int note_no[] = {60,61,62,63,64,65,66,67,68,69,70,71};

// button layout
// 13 12 11 10
// 9  8  7  6
// 5  4  3  2

// notes layout Flstudio
// 0  1  2  3
// 12 13 14 15
// 24 25 26 27

void setup() {
  pinMode(octaveUp, INPUT);
  pinMode(octaveDown,INPUT);
  
  
  Serial.begin(9600);
  for(int i=0; i<numbts; i++) bts[i] = i+2;
  for(int i=0; i<numbts; i++) btgs[i] = false;
  for(int i=0; i<numbts; i++) pinMode(bts[i], INPUT_PULLUP);
}
void loop() {
  

  for(int i=0; i<numbts; i++){
    if (!btgs[i]) {
      if (digitalRead(bts[i])==LOW ) {
        if (octaveUp == HIGH){
          MIDImessage(noteON, note_no[i]+12, 127);
          delay(2);//crude form of button debouncing
        btgs[i] = true;}
        else if(octaveDown == HIGH){
          MIDImessage(noteON, note_no[i]-12, 127);
        delay(2);//crude form of button debouncing
        btgs[i] = true;
        }
        else {
          MIDImessage(noteON, note_no[i], 127);
        delay(2);//crude form of button debouncing
        btgs[i] = true;}
      }
    else {
      if (digitalRead(bts[i])==HIGH) {
        MIDImessage(noteON, note_no[i], 0);
        MIDImessage(noteON, note_no[i]+12, 0);// Turn them both off
        MIDImessage(noteON, note_no[i]-12, 0);
        delay(2);//crude form of button debouncing
        btgs[i] = false;
      }
    }
  }
}
}
//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

THis is where i am now guys. It do get compile. If you guys find any thing wrong do let me know thanks for help :slight_smile:

  for(int i=0; i<numbts; i++) bts[i] = i+2;
  for(int i=0; i<numbts; i++) btgs[i] = false;
  for(int i=0; i<numbts; i++) pinMode(bts[i], INPUT_PULLUP);

really should be

  for(int i=0; i<numbts; i++)
  {
     bts[i] = i+2;
     btgs[i] = false;
     pinMode(bts[i], INPUT_PULLUP);
  }
}
void loop() {
 

  for(int i=0; i<numbts; i++){

It really does not make sense to put the { on the same line as the function and/or statement, to save real estate, and then put all those blank lines in the function.

Put blank lines BETWEEN functions. Put every { on a line BY ITSELF. Put every } on a line BY ITSELF.

#define octaveUp  14 
        if (octaveUp == HIGH){

I'm reasonably confident that 14 is NOT equal to HIGH, and never will be. I'm pretty sure that 15 is not HIGH, either.

Now, can you see why we recommend that you use Pin in the name of all variables (or constants) that hold pin numbers?

Why is that Soo Paul. What I changes I made to codes are I define the octave up and down to 14 and 15 and I removed the digitalread from the setup() and wrote the condition like this

If (digitalRead(octaveUp)==HIGH){

And compiled it and it do get compiled and when I try to upload code to board the TX light up constantly so what it seems like it the signal are generated infinitely because somewhere the code is wrong maybe the for loop as u said or the condition

Why is that Soo Paul.

Why is what "Soo"?

and wrote the condition like this

So, you made some code changes, without posting the new code. It does something, but that something is not what you want, but we don't know what either one of the things are.

How ARE the octave up and octave down switches wired? Why are those pins INPUT, while the bts pins are INPUT_PULLUP?

It sounds like you might be having floating pin problems.