Why doesn't this code run after re-plug? Is it scared?

I have this program (Below), that runs fine the first time its uploaded and after hitting the reset button on the board, however once I unplug it and then re-plug it, no go. Iv'e tried it with an UNO and Mega.

Things I have tried include;
Multiple boards
Other power supplies, cords
Burning bootloaders
Testing both boards with "Blink" and "no delay blink" (Works fine with both under the exact same circumstances)

Le Code...

#include <MIDI.h>

// Initialize the MIDI library with default settings
MIDI_CREATE_DEFAULT_INSTANCE();

const int pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
const int numPins = sizeof(pins) / sizeof(pins[0]);
const int debounceDelay = 50; // Debounce delay in milliseconds

// Array to store the last debounce time for each pin
unsigned long lastDebounceTime[numPins] = {0};
// Array to store the last stable state for each pin
bool lastPinState[numPins] = {HIGH};
// Array to store the current reading for each pin
bool pinReading[numPins] = {HIGH};

void setup() {
  // Initialize MIDI communication
  MIDI.begin(MIDI_CHANNEL_OMNI);

  // Set up each pin as an input with an internal pull-up resistor
  for (int i = 0; i < numPins; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int i = 0; i < numPins; i++) {
    bool reading = digitalRead(pins[i]);

    if (reading != lastPinState[i]) {
      // Reset the debounce timer
      lastDebounceTime[i] = millis();
    }

    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      // If the reading has been stable for longer than the debounce delay
      if (reading != pinReading[i]) {
        pinReading[i] = reading;

        // Determine the note number (arbitrary mapping for demonstration)
        int note = i + 60; // Mapping pin to MIDI note (C4 = 60)

        if (reading == LOW) {
          // Send MIDI Note On message
          MIDI.sendNoteOn(note, 127, 1);
        } else {
          // Send MIDI Note Off message
          MIDI.sendNoteOff(note, 0, 1);
        }
      }
    }

    // Save the reading for the next loop iteration
    lastPinState[i] = reading;
  }
}

I'm trying to setup a MIDI pedalboard for a 1970's CONN theater organ that had an irreplaceable part go kaput on it.
Any help would be greatly appreciated!

No, it isn't scared. Chips don't have emotions (not yet, anyway) :wink:

How do you know it does not run?

Maybe add some Serial.print to check?

Does the midi player accept multiple 'begin'?

1 Like

I have it in front of me at the computer right now. the "testing" i'm doing consists of me sticking a jumper wire going from ground to the pins (2-12) to simulate the key switches. When it's "working" the TX led blinks when I attach (note on) and remove it (note off).

What could possibly go wrong within these lines of your code ?

oh, it might be that debounceDelay is never reached ... :slight_smile:

debounceDelay = 50 (milliseconds) right?

That only proves it does not send anything...
It does not prove that the sketch is not running...

It would be a good idea if you would separate your debounce from your writing to midi...
Your program flow is quite difficult to follow.

Also: add Serial.print("some message") after each if and else... That will show where the program goes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.