Arduino ne démarre pas le code

Lors que je téléverse le code, tout fonctionne bien, mais lorsque je déconnecte le usb de mon arduino Leonardo, alors il ne se passe plus rien.

Je dois retéléversé mon code pour que tout fonctionne à nouveau.

Merci beaucoup

#include <Wire.h>
#include "Adafruit_MPR121.h"
#include "MIDIUSB.h"
#define _BV(bit) (1 << (bit))

Adafruit_MPR121 cap = Adafruit_MPR121();

int noteMidi[] = {60, 62, 64, 65, 67, 69, 71};
int noteClavier[] = {"c", "d", "e", "f", "g", "a", "b"};
int noteMaximum = 7;

uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
  Serial.begin(115200);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }

  Serial.println("Adafruit MPR121 Capacitive Touch sensor test");

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  currtouched = cap.touched();
  
  for (uint8_t i = 0; i < noteMaximum; i++) {
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      //Serial.print(i); Serial.println(" touched");
      Serial.println("Sending note on");
      noteOn( 10, noteMidi[i], 64);   // Channel 0, middle C, normal velocity
      MidiUSB.flush();
    }
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      //Serial.print(i); Serial.println(" released");
      Serial.println("Sending note off");
      noteOff(10, noteMidi[i], 64);  // Channel 0, middle C, normal velocity
      MidiUSB.flush();
    }
  }
  lasttouched = currtouched;
  delay(100);
}
void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

Peux-tu préciser comment tu as connecté le capteur ?
La doc indique que l'adresse I2C change selon la manière de le connecter :

The MPR121 ADDR pin is pulled to ground and has a default I2C address of 0x5A
You can adjust the I2C address by connecting ADDR to other pins:

  • ADDR not connected: 0x5A
  • ADDR tied to 3V: 0x5B
  • ADDR tied to SDA: 0x5C
  • ADDR tied to SCL: 0x5D

tu demandes ici d'attendre une activité sur le port série pour pouvoir exécuter le reste du code..... ne t'étonnes donc pas du comportement constaté quand le câble USB est débranché !

1 Like

C'est le Sheild et la pin ADDR n'est pas connectée.

Un gros merci !!

Je commence, donc j'avais interprété cela différement.

Serial est toujours disponible sur une UNO.
Le while (!Serial) est utile sur les cartes qui ont l'USB natif dans le CPU.

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