sensors ok, trying usbmidi library

This commit is contained in:
Bachir Soussi Chiadmi 2024-06-19 16:06:47 +02:00
parent 82592807c1
commit fbc4292c0a
1 changed files with 85 additions and 35 deletions

View File

@ -1,33 +1,49 @@
#include <fifo.h>
#include <midi_messages.h>
#include <midi_serialization.h>
#include <usbboard.h>
#include <usbconfig.h>
#include <usbdrv.h>
#include <usbmidi.h>
#include <Wire.h>
#include <VL53L0X.h>
// IR SENSORS
#define XSHUT_pin4 8 //not required for address change
#define XSHUT_pin3 9
#define XSHUT_pin2 10
#define XSHUT_pin1 11
// #define XSHUT_pin4 8 //not required for address change
#define XSHUT_pin3 8
#define XSHUT_pin2 9
#define XSHUT_pin1 10
//ADDRESS_DEFAULT 0b0101001 or 41
#define Sensor4_newAddress 41 //not required address change
#define Sensor3_newAddress 42
#define Sensor2_newAddress 43
#define Sensor1_newAddress 44
// #define Sensor4_newAddress 41 //not required address change
#define Sensor3_newAddress 41
#define Sensor2_newAddress 42
#define Sensor1_newAddress 43
// IR sensors
VL53L0X sensor4;
VL53L0X sensor3;
VL53L0X sensor2;
VL53L0X sensor1;
// bool last_sensor1_state = 0;
// bool last_sensor2_state = 0;
// bool last_sensor3_state = 0;
// bool last_sensor4_state = 0;
const int trackCount = 3;
int tracksUp[trackCount];
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
USBMIDI.write(note & 0x7f);
USBMIDI.write(velocity &0x7f);
}
void setup() {
Serial.println("Setup");
Serial.begin(115200);
// Set the maximum speed in steps per second:
@ -38,12 +54,10 @@ void setup() {
pinMode(XSHUT_pin1, OUTPUT);
pinMode(XSHUT_pin2, OUTPUT);
pinMode(XSHUT_pin3, OUTPUT);
pinMode(XSHUT_pin4, OUTPUT);
digitalWrite(XSHUT_pin1, LOW);
digitalWrite(XSHUT_pin2, LOW);
digitalWrite(XSHUT_pin3, LOW);
digitalWrite(XSHUT_pin4, LOW);
Wire.begin();
@ -64,16 +78,10 @@ void setup() {
sensor3.setAddress(Sensor3_newAddress);
delay(10);
pinMode(XSHUT_pin4, INPUT);
sensor4.init();
sensor4.setAddress(Sensor4_newAddress);
delay(10);
sensor1.setTimeout(0);
sensor2.setTimeout(0);
sensor3.setTimeout(0);
sensor4.setTimeout(0);
// // if (!sensor.init())
// // {
@ -83,33 +91,75 @@ void setup() {
sensor1.startContinuous();
sensor2.startContinuous();
sensor3.startContinuous();
sensor4.startContinuous();
for (int i=0; i<trackCount; ++i) {
tracksUp[i] = 0;
}
}
void loop() {
// Serial.println('loop');
//Handle USB communication
USBMIDI.poll();
while (USBMIDI.available()) {
// We must read entire available data, so in case we receive incoming
// MIDI data, the host wouldn't get stuck.
u8 b = USBMIDI.read();
}
int state1 = sensor1.readRangeContinuousMillimeters();
Serial.print("s1 : ");
Serial.print(state1);
Serial.print(" | ");
// Serial.print("s1 : ");
// Serial.print(state1);
// Serial.print(" | ");
if (state1 > 0 && tracksUp[0] == 0)
{
sendNote(0, 64, 127);
tracksUp[0] = 1;
}
else
{
tracksUp[0] = 0;
}
int state2 = sensor2.readRangeContinuousMillimeters();
Serial.print("s2 : ");
Serial.print(state2);
Serial.print(" | ");
// Serial.print("s2 : ");
// Serial.print(state2);
// Serial.print(" | ");
if (state2 > 0 && tracksUp[1] == 0)
{
sendNote(0, 65, 127);
tracksUp[1] = 1;
}
else
{
tracksUp[1] = 0;
}
int state3 = sensor3.readRangeContinuousMillimeters();
Serial.print("s3 : ");
Serial.print(state3);
Serial.print(" | ");
// Serial.print("s3 : ");
// Serial.println(state3);
// Serial.print(" | ");
if (state3 > 0 && tracksUp[2] == 0)
{
sendNote(0, 66, 127);
tracksUp[2] = 1;
}
else
{
tracksUp[2] = 0;
}
int state4 = sensor4.readRangeContinuousMillimeters();
Serial.print("s4 : ");
Serial.println(state4);
// if (sensor1.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// if (sensor2.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// if (sensor3.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// if (sensor4.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// Flush the output.
USBMIDI.flush();
}