leds are on when they should be off

I have a midi controller and am adding leds to each switch. The problem is that the LEDs are ON at bootup, when I want them to be off. Sorry the code is long ;(

// midi controller for katana head    nick@12testing.net
// jan 2019

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

MIDI_CREATE_DEFAULT_INSTANCE();
// Constants
#define SWITCH1 6 // channel 1
#define SWITCH2 7 // channel 2
#define SWITCH3 8 // channel 3
#define SWITCH4 9 // channel 4
#define SWITCH5 2 // FX1
#define SWITCH6 3 // FX2
#define SWITCH7 4 // BANK
#define SWITCH8 5 // VOL
# define LED1 14 // channel 1
# define LED2 15 // channel 2
# define LED3 16 // channel 3
# define LED4 17 // channel 4
# define LED5 10 // FX1
# define LED6 11 // FX2
# define LED7 12 // BANK
# define LED8 13 // VOL - not yet implemented
#define SWITCHES 8    // how many switches?

int switches[SWITCHES] = { SWITCH1, SWITCH2, SWITCH3, SWITCH4, SWITCH5, SWITCH6, SWITCH7, SWITCH8 };
int switchState[SWITCHES] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };   // Initial state of switch
int leds[SWITCHES] = { LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8 };
int currentSwitch = 0;
int thisLed = 0;
int FX1status = 0;
int FX2status = 0;
int BANKstatus = 0;
int VOLstatus = 0;
int bank = 0;
int channel = 0;
unsigned long SwitchMillis = 0; // when button was released
unsigned long DebounceMillis = 100; //
unsigned long CurrentMillis = 0;

void setup()
{
  MIDI.begin(1);
  for ( currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++ )
  {
    pinMode( switches[currentSwitch], INPUT );          // Set pin for switch
    digitalWrite( switches[currentSwitch], HIGH );      // Turn on internal pullup
    pinMode( leds[currentSwitch], OUTPUT );             // Set pin for LED
  }
  //  MIDI.sendControlChange(82, 100, 1); // preset lower volume for switch 8
  //  Serial.begin(9600);   Serial.println("test");

}

void loop()
{
  CurrentMillis = millis();  // get the time at the start of this loop()
  if (CurrentMillis - SwitchMillis >= DebounceMillis)  //is it time to check the switches?
  {
    SwitchMillis = millis(); //re-initilize Timer
    for ( currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++ )
    {
      if ( (digitalRead(switches[currentSwitch]) != switchState[currentSwitch] ) && (switchState[currentSwitch] == HIGH) )
      {
        channel = (bank + currentSwitch);
        switch ( currentSwitch )
        {
          case 0: // channel 1
            MIDI.sendProgramChange(channel, 1);
            lightPin(currentSwitch);
            break;

          case 1: // channel 2
            MIDI.sendProgramChange(channel, 1);
            lightPin(currentSwitch);
            break;

          case 2: // channel 3
            MIDI.sendProgramChange(channel, 1);
            lightPin(currentSwitch);
            break;

          case 3: // channel 4
            MIDI.sendProgramChange(channel, 1);
            lightPin(currentSwitch);
            break;

          case 4: // FX1
            MIDI.sendControlChange(16, 50, 1);  //
            if (FX1status == 0 ) {
              digitalWrite( leds[currentSwitch], HIGH );
              FX1status = 1; //
            }
            else              {
              MIDI.sendControlChange(16, 0, 1);  //
              digitalWrite( leds[currentSwitch], LOW );
              FX1status = 0; // switch led status
            }
            break;

          case 5: // FX2
            MIDI.sendControlChange(17, 50, 1);  //
            if (FX2status == 0 ) {
              digitalWrite( leds[currentSwitch], HIGH );
              FX2status = 1; //
            }
            else              {
              MIDI.sendControlChange(17, 0, 1);  //
              digitalWrite( leds[currentSwitch], LOW );
              FX2status = 0; // switch led status
            }
            break;

          case 6: // bank A/B
            if (BANKstatus == 0 ) {
              bank += 5;
              BANKstatus = 1; //
            }
            else              {
              bank = 0;
              BANKstatus = 0;
            }
            break;

          case 7: // vol cut
            //           MIDI.sendControlChange(82, 127, 1);  //
            if (VOLstatus == 0 ) {
              digitalWrite(currentSwitch, HIGH ); // off
              VOLstatus = 1; //
            }
            else              {
              //             MIDI.sendControlChange(82, 100, 1);  //
              digitalWrite(currentSwitch, LOW); //
              VOLstatus = 0; // switch led status
            }
            break;
        } // end of cases
      } // end of switch change
      switchState[currentSwitch] = digitalRead( switches[currentSwitch] );
    } // end of switch loop
  } // end of check time loop
}  // end of void loop

void lightPin( int activeLed )    // switch chosen LED on, others off
{
  MIDI.sendControlChange(17, 0, 1);  //
  MIDI.sendControlChange(16, 0, 1);  //
  for ( thisLed = 0; thisLed < 6; thisLed++ )
  {
    if (activeLed == thisLed)
    {
      digitalWrite( leds[thisLed], HIGH );
    }
    else
    {
      digitalWrite( leds[thisLed], LOW );
    }
  }
}

Hi,
Immediately after you declare each output pin, digitalWrite a LOW to it in setup.

Tom.... :slight_smile:

TomGeorge:
Hi,
Immediately after you declare each output pin, digitalWrite a LOW to it in setup.

Tom.... :slight_smile:

In setup I added

    digitalWrite( leds[currentSwitch], LOW );      //

No joy. it happens even if I empty the loop completely

As a test, replace loop with a function that does nothing. What happens to the LEDs now?

wildbill:
As a test, replace loop with a function that does nothing. What happens to the LEDs now?

The same...

Weird, if I change to

digitalWrite( leds[currentSwitch], HIGH );

it seems to work!

Then you wired your LEDs backwards. They should be connected with the anode to the Arduino pin, and the cathode (flat side, short leg) to ground.

Pieter

PieterP:
Then you wired your LEDs backwards. They should be connected with the anode to the Arduino pin, and the cathode (flat side, short leg) to ground.

Pieter

Oh f**k! I assumed they wouldn't work if I wired them the wrong way round, but I wasn't thinking about the digital aspect ;(

many thanks, the learning ladder gets ever steeper!

Maybe the LEDs weren't wired backwards. There should also be a curretn limiting resistor in series.

Sometime LEDs are wired up to Vcc and need a LOW to light up, other times wired down to ground and need a HIGH. Just depends and obv. you have to know (or decide when you design it).

You'll discover the same thing when you play with pushbuttons. You just have to know what HIGH means, presse dot not pressed.

alto777

OK, so I read HIGH as LOW :wink:

Thanks for the help, these are frustrating little issues!