Midi controller loop help

Hey all,

Been working on something for a while which works, but I would like to add some extraneous functionality of a bank button to give me 10 other options when I have a switch engaged.

Currently my code is (simply)

If Button pressed, Delay, Send a midi pc command Turn all led off Light corresponding led to button

Else if button 2 pressed

What I would like help on is to make the small buttons send a different pc message with an external switch engaged.

So new code would go something like

If button 1 pressed and extsw high

Send pc 10

If button 1 pressed and extsw low

Send pc 1

I've read a few bits handout && and else if but it's not totally clear

Cheers

Baz.

So new code would go something like

If button 1 pressed and extsw high

Send pc 10

If button 1 pressed and extsw low

Send pc 1

Yes that sounds right. What exactly do you not understand? Each of those lines can be converted directly into C.

If button 1 pressed and extsw high

if( digitalRead(bitton1pin) == LOW && digitalRead (extswPin) == HIGH ) { // do stuff
}

Cheers @Grumpy_Mike

I have added this to my code and it now looks like this (only 1 button shown not 10)

void loop() { 
  if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == LOW ) {
    delay(10); // software de-bounce
  if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == LOW )
      midiOut.sendProgramChange(00, 1); 
  if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == HIGH ) {
    delay(10); // software de-bounce
  if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == HIGH)
      midiOut.sendProgramChange(11, 1); 
      digitalWrite (LED2,LOW);
      digitalWrite (LED3,LOW);
      digitalWrite (LED4,LOW);
      digitalWrite (LED5,LOW);
      digitalWrite (LED6,LOW);
      digitalWrite (LED7,LOW);
      digitalWrite (LED8,LOW);
      digitalWrite (LED9,LOW);
      digitalWrite (LED10,LOW);
      digitalWrite(LED1,HIGH);
             delay(250);

When I do this with program change 08 and 09 I get the error below. ive checked and its not sending the same 8 or 9 on another button.

00 and 11 = button 1 01 and 12 = button 2 etc

I've googled the error and cant find anything, but if i change 08 to just 8 it compiles?

master exit status 1 invalid digit "8" in octal constant arduino

Leading zeros tell the compiler what follows is an octal integer literal. https://en.cppreference.com/w/cpp/language/integer_literal Remove the unnecessary leading zeros.

Pieter

It helps enormously if you can post all your code.

master exit status 1 invalid digit "8" in octal constant arduino

Do you know what octal is? It is a number expressed in the base 8. The Arduino can use diffrent numbers bases, binary (base 2 ), octal (base 8 ), decimal (base 10 ) and hexadecimal ( base 16 ).

So when you use a number you have to tell it what base to use, unless it is decimal and that is the default.

Here is a list:- https://www.arduino.cc/reference/en/language/variables/constants/integerconstants/

So by writing zero eight 08 the first zero tells the compiler you want to specify an octal number, but the second number tells it wants to use eight. Well as octal uses numbers 0 through 7, any number higher would be an error. That is what the compiler is telling you. So if you want to use 8 just specify 8 and that will give you the number you need as you have found.

Edit Just beaten to it by Pieter

I have added this to my code and it now looks like this (only 1 button shown not 10)

That is because you only ever read buttonOne, and you are using the same state for the switch, and you don't do something in every if. This is what you need to do:-

 if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == LOW ) {
   midiOut.sendProgramChange(00, 1);
  }
  if(digitalRead(buttonOne) == LOW && digitalRead (SW1) == HIGH ) {
      midiOut.sendProgramChange(00, 2);
 }
  if(digitalRead(buttonTwo) == LOW && digitalRead (SW1) == LOW ) {
    midiOut.sendProgramChange(00, 3);
 }
  if(digitalRead(buttonTwo) == LOW && digitalRead (SW1) == HIGH ) {
      midiOut.sendProgramChange(00, 4);
}

Also you always turn every LED off each time round the loop, and never turn any on.

I am not sure what your midiOut.sendProgramChange function accepts because you have not posted all the code, but I am assuming here it is channel number followed by the program change value.

When you get more skilled at programming you will find that there are much better ways of doing this rather than having turgid code repeating nearly the same thing over and over. But that is for another day.