benoit-roussel_turntable/turntable-sensors/turntable-sensors.ino

208 lines
4.9 KiB
Arduino
Raw Permalink Normal View History

2024-05-24 09:54:52 +02:00
#include <Wire.h>
#include <VL53L0X.h>
2024-06-20 18:08:27 +02:00
#include "MIDIUSB.h"
2024-05-24 09:54:52 +02:00
// IR SENSORS
2024-06-19 16:06:47 +02:00
// #define XSHUT_pin4 8 //not required for address change
#define XSHUT_pin3 8
#define XSHUT_pin2 9
#define XSHUT_pin1 10
2024-05-24 09:54:52 +02:00
//ADDRESS_DEFAULT 0b0101001 or 41
2024-06-19 16:06:47 +02:00
// #define Sensor4_newAddress 41 //not required address change
#define Sensor3_newAddress 41
#define Sensor2_newAddress 42
#define Sensor1_newAddress 43
2024-05-24 09:54:52 +02:00
// IR sensors
VL53L0X sensor3;
VL53L0X sensor2;
VL53L0X sensor1;
// bool last_sensor1_state = 0;
// bool last_sensor2_state = 0;
// bool last_sensor3_state = 0;
2024-06-19 16:06:47 +02:00
const int trackCount = 3;
int tracksUp[trackCount];
2024-06-20 18:08:27 +02:00
// https://github.com/arduino/tutorials/blob/master/ArduinoZeroMidi/PitchToNote.h
uint8_t intensity = 127;
2024-06-19 16:06:47 +02:00
2024-05-24 09:54:52 +02:00
void setup() {
2024-06-19 16:06:47 +02:00
Serial.println("Setup");
Serial.begin(115200);
// Set the maximum speed in steps per second:
2024-05-24 09:54:52 +02:00
2024-06-20 18:08:27 +02:00
// wait until serial port opens for native USB devices
// while (! Serial) {
// delay(1);
// }
2024-05-24 09:54:52 +02:00
// VL53L0X
/*WARNING*/
//Shutdown pins of VL53L0X ACTIVE-LOW-ONLY NO TOLERANT TO 5V will fry them
pinMode(XSHUT_pin1, OUTPUT);
pinMode(XSHUT_pin2, OUTPUT);
pinMode(XSHUT_pin3, OUTPUT);
digitalWrite(XSHUT_pin1, LOW);
digitalWrite(XSHUT_pin2, LOW);
digitalWrite(XSHUT_pin3, LOW);
Wire.begin();
//Change address of sensor and power up next one
pinMode(XSHUT_pin1, INPUT);
2024-06-20 18:08:27 +02:00
// sensor1.init();
sensor1.setTimeout(500);
if (!sensor1.init())
{
Serial.println("Failed to detect and initialize sensor1!");
while (1) {}
}
2024-05-24 09:54:52 +02:00
sensor1.setAddress(Sensor1_newAddress);
delay(10);
pinMode(XSHUT_pin2, INPUT);
2024-06-20 18:08:27 +02:00
// sensor2.init();
sensor2.setTimeout(500);
if (!sensor2.init())
{
Serial.println("Failed to detect and initialize sensor2!");
while (1) {}
}
2024-05-24 09:54:52 +02:00
sensor2.setAddress(Sensor2_newAddress);
delay(10);
pinMode(XSHUT_pin3, INPUT);
2024-06-20 18:08:27 +02:00
// sensor3.init();
sensor3.setTimeout(500);
if (!sensor3.init())
{
Serial.println("Failed to detect and initialize sensor3!");
while (1) {}
}
2024-05-24 09:54:52 +02:00
sensor3.setAddress(Sensor3_newAddress);
delay(10);
2024-06-20 18:08:27 +02:00
// sensor1.setTimeout(0);
// sensor2.setTimeout(0);
// sensor3.setTimeout(0);
2024-05-24 09:54:52 +02:00
// // if (!sensor.init())
// // {
// // Serial.println("Failed to detect and initialize sensor!");
// // while (1) {}
// // }
sensor1.startContinuous();
sensor2.startContinuous();
sensor3.startContinuous();
2024-06-19 16:06:47 +02:00
for (int i=0; i<trackCount; ++i) {
tracksUp[i] = 0;
}
}
void loop() {
2024-06-20 18:08:27 +02:00
// Serial.println("loop");
2024-06-19 16:06:47 +02:00
2024-06-20 18:08:27 +02:00
Serial.print("s1 : ");
2024-06-26 15:26:23 +02:00
// int state1 = sensor1.readRangeContinuousMillimeters();
// Serial.print(state1);
// if ((state1 == 0 || state1 > 50) && tracksUp[0] == 0)
// {
// Serial.print(" ON");
// noteOn(0, 84, intensity);
// MidiUSB.flush();
// tracksUp[0] = 1;
// }else{
// Serial.print(" OFF");
// noteOff(0, 84, 0);
// MidiUSB.flush();
// tracksUp[0] = 0;
// }
checkSensor(sensor1, 0, 84);
2024-06-20 18:08:27 +02:00
Serial.print(" | ");
2024-06-19 16:06:47 +02:00
2024-05-24 09:54:52 +02:00
2024-06-20 18:08:27 +02:00
Serial.print("s2 : ");
2024-06-26 15:26:23 +02:00
// int state2 = sensor2.readRangeContinuousMillimeters();
// Serial.print(state2);
// if ((state2 == 0 || state2 > 50) && tracksUp[1] == 0){
// Serial.print(" ON");
// noteOn(0, 86, intensity);
// MidiUSB.flush();
// tracksUp[1] = 1;
// }else{
// Serial.print(" OFF");
// noteOff(0, 86, 0);
// MidiUSB.flush();
// tracksUp[1] = 0;
// }
checkSensor(sensor2, 1, 86);
2024-06-20 18:08:27 +02:00
Serial.print(" | ");
2024-06-19 16:06:47 +02:00
2024-05-24 09:54:52 +02:00
2024-06-20 18:08:27 +02:00
Serial.print("s3 : ");
2024-06-26 15:26:23 +02:00
// int state3 = sensor3.readRangeContinuousMillimeters();
// Serial.print(state3);
// // Serial.print(" | ");
// if ((state3 == 0 || state3 > 50) && tracksUp[2] == 0){
// Serial.print(" ON");
// noteOn(0, 88, intensity);
// MidiUSB.flush();
// tracksUp[2] = 1;
// }else{
// Serial.print(" OFF");
// noteOff(0, 88, 0);
// MidiUSB.flush();
// tracksUp[2] = 0;
// }
checkSensor(sensor3, 2, 88);
2024-06-20 18:08:27 +02:00
Serial.println("");
2024-05-24 09:54:52 +02:00
// if (sensor1.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// if (sensor2.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// if (sensor3.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
2024-06-19 16:06:47 +02:00
2024-06-20 18:08:27 +02:00
}
2024-06-26 15:26:23 +02:00
void checkSensor(VL53L0X sensor, int index, int note){
int state = sensor.readRangeContinuousMillimeters();
// Serial.print("s3 : ");
Serial.print(state);
// Serial.print(" | ");
if ((state == 0 || state > 50) && tracksUp[index] == 0){
Serial.print(" ON");
noteOn(0, note, intensity);
MidiUSB.flush();
tracksUp[index] = 1;
}else{
Serial.print(" OFF");
noteOff(0, note, 0);
MidiUSB.flush();
tracksUp[index] = 0;
}
}
2024-06-20 18:08:27 +02:00
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};
2024-06-19 16:06:47 +02:00
2024-06-20 18:08:27 +02:00
MidiUSB.sendMIDI(noteOff);
}