8 time of flight sensor arduino to midi

i asked chatGPT can you help me write a code on arduino leonardo to get signal from 8 time of flight sensor, and convert that signal into sending out midi to a computer from arduino leonardo, thanks
mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
1
Indexable
Never
#include <Wire.h>
#include <MIDI.h>

// Initialize the MIDI library
MIDI_CREATE_DEFAULT_INSTANCE();

// Array to store distance readings from the ToF sensors
int distances[8];

void setup() {
  // Start the I2C bus
  Wire.begin();

  // Initialize the MIDI library
  MIDI.begin();

  // Set the ToF sensors to continuous measurement mode
  for (int i = 0; i < 8; i++) {
    Wire.beginTransmission(i);
    Wire.write(0x00); // Register address
    Wire.write(0x03); // Continuous measurement mode
    Wire.endTransmission();
  }
}

void loop() {
  // Read distance data from each ToF sensor
  for (int i = 0; i < 8; i++) {
    Wire.beginTransmission(i);
    Wire.write(0x02); // Register address
    Wire.endTransmission();
    Wire.requestFrom(i, 2);
    if (Wire.available() >= 2) {
      byte high_byte = Wire.read();
      byte low_byte = Wire.read();
      distances[i] = (high_byte << 8) + low_byte;
    }
  }

  // Send MIDI control change messages for each distance value
  for (int i = 0; i < 8; i++) {
    MIDI.sendControlChange(i, distances[i] >> 2, 1);
  }
  
  // Small delay
  delay(10);
}