I can give you my sketch for the MIDI adaptation :
#include <MIDI.h> // MIDI Library
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
MIDI_CREATE_DEFAULT_INSTANCE();
int led_ready = 22;
int led_test = 24;
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
digitalWrite(led_ready,HIGH); //Turn LED on
delay(1000);
digitalWrite(led_ready,LOW);
}
void setup() {
pinMode(led_ready, OUTPUT);
pinMode(led_test, OUTPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleNoteOn(MyHandleNoteOn); //call the function above when a note is pressed
}
void loop() {
digitalWrite(led_test,HIGH);
MIDI.read(); //Check the midi info
}
I have two sketches.
The first one is just a sketch which runs leds with buttons. It works perfectly when the Rx is unplugged (This sketch doesn't use the Rx at all)
The second one is the sketch I gave above. I have added a digitalWrite(led_test,HIGH); in the loop in order to see if the sketch is running. This one needs the Rx to receive the MIDI information. But if the Rx is unplugged, it has to light the led_test anyway.
Now my situation is :
When the Rx is unplugged : The first sketch works fine, the second one doesn't (which is normal because the MIDI information are not received). The led_test is lightening so the sketch is running.
When the Rx is plugged : The first sketch doesn't work (even if it doesn't need the Rx connection). The second sketch doesn't work and the led_test is off.
That's why I concluded that when the Rx is plugged, the sketch is not started by the board.
The results are the same either I use the USB alimentation or the jack one.
The circuit linked to the Rx seems to work fine. I see the byte on the Rx pin when I play a note on the piano.
I've already try to replace the resistor 270 Ohms with a 470 Ohms, with no results.
The first sketch works really fine. That's why I didn't post the code.
My problem is that no sketch runs when the Rx is plugged. No matter if the sketch is supposed to use the Rx or not. The Rx pin seems to prevent the board from running any sketch.
Could the MIDI instrument be confusing the bootloader by sending it crap during startup, causing it to think you're trying to upload code to it?
Of course, you could have done something in your code that you don't realize is causing the problem, but we can't check that since you won't post the code.
int bouton_acquisition = 23; //Bouton de lancement de l'acquisition à partir de l'état de veille
int bouton_raz = 25; //Bouton de retour à l'état de veille (à actionner pendant l'acquisition ou pendant la préparation du cocktail)
int bouton_lancement = 27; //Bouton de fin d'acquisition/lancement du cocktail
int led_acquisition = 24; //allumée lorsque l'acquisition est en cours
int led_ready = 22; //allumée lorsque la carte est en état de veille
int led_production=26;
int etat_acquisition;
int etat_raz;
int etat_lancement;
void setup() {
pinMode(bouton_acquisition, INPUT_PULLUP);
pinMode(bouton_raz, INPUT_PULLUP);
pinMode(bouton_lancement, INPUT_PULLUP);
pinMode(led_production, OUTPUT);
pinMode(led_acquisition, OUTPUT);
pinMode(led_ready, OUTPUT);
etat_raz=HIGH; // On initialise les états des boutons
etat_lancement=HIGH;
etat_acquisition=HIGH;
}
void loop() {
etat_raz=HIGH; // On initialise les états des boutons
etat_lancement=HIGH;
etat_acquisition=HIGH;
int raz = 0; //Discriminer la sortie de l'acquisition pour raz ou pour production
while (etat_acquisition == HIGH) { // état de veille tant que le bouton de lancement d'acquisition n'est pas actionné
digitalWrite(led_ready,HIGH);
digitalWrite(led_acquisition,LOW);
digitalWrite(led_production, LOW);
etat_acquisition=digitalRead(bouton_acquisition);
}
digitalWrite(led_ready,LOW);
while ((etat_raz == HIGH) && (etat_lancement == HIGH)) { //ACQUISITION
digitalWrite(led_acquisition,HIGH);
etat_lancement=digitalRead(bouton_lancement);
etat_raz=digitalRead(bouton_raz);
if ((etat_raz == LOW)&&(etat_lancement == HIGH)) { //raz=1 si l'acquisition est avortée
raz=1;
}
}
digitalWrite(led_acquisition, LOW);
if (raz==0) { //Calcul de l'algorithme de recette
digitalWrite(led_production, HIGH);
delay(5000);
digitalWrite(led_production, LOW);
}
}
I'm saying the Rx prevent the code from running because when it is plugged, none of the 2 sketches seem to run.
But when I start the board while the Rx is unplugged, le first sketch works fine, the second one has the led_test lightening (which proves that the loop function is active).
bizarre. c'est que RX que tu débranches ou le photocoupleur avec? il n y a pas de court circuit, la carte démarre bien?
strange. you only dicsonnect the RX pin or the whole 6N138 photocoupler. there's no short circuit? the board turn on?
I disconnect only the Rx from the board.
The board turn on yes. No short circuit I think. I can read with the oscilloscope the bite on my rx pin when I play a piano note. I checked the whole circuit many times.
I tried to use a SoftwareSerial, that is to say redefining the Rx pin on another input. But I don't know if the assignment works. When I start the sketch, the led show me that the loop is active, but I don't have the other led when I press a key. But in this case it could be a problem of my sketch.
Here is the code :
#include <SoftwareSerial.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
const int rxPin=3;
const int txPin=4;
const int led_ready = 22;
const int led_acquisition = 24;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
MIDI_CREATE_INSTANCE(SoftwareSerial, mySerial, midi1)
void MyNoteOn(byte channel, byte pitch, byte velocity) { //Called when a key is pressed
digitalWrite(led_ready,HIGH); //Turn LED on
if (velocity == 0) {
digitalWrite(led_ready,LOW); //Turn LED off
}
delay(10000);
}
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(led_ready, OUTPUT);
pinMode(led_acquisition, OUTPUT);
mySerial.begin(31250);
midi1.begin(MIDI_CHANNEL_OMNI);
midi1.setHandleNoteOn(MyNoteOn); //Send to the MyHandleNoteOn function when a midi event occur
}
void loop() { // Main loop
digitalWrite(led_acquisition,HIGH);
midi1.read(); //Watch the midi connection
}
well and if you hold the RX input to the ground through a 100 ohms resistor the board starts?
can you take a picture of your wiring with the board optocoupler etc?
Check the voltage on pin 0, the RX input. This should be high. If you are holding it low with a badly wired MIDI interface that will stop it running. This is because the boot loader thinks it is receiving data and is waiting to receive a valid byte.
With the MIDI input unplugged pin 0 on the arduino will read high. If it is not reading high then your circuit is faulty.
The problem was a stupid thing.
I just had to plug the Rx after starting the board. In fact MIDI sent signals preventing the board from starting a sketch. The solution is just to start the board before starting the piano.
I thought that plugging a cable while the board was running was a bad idea, that's why I didn't find the solution sooner..