In this video – I use an Arduino and photoresistor to make some pretty neat music!
I DID make a mistake in the video (one of the hazards of doing one-shot videos in a short time frame). I forgot to include one of the programs (loopMIDI). The software you will need to use is:
And in addition you’ll need The USB-MIDI library installed on Arduino (Look it up using the library manager)
And finally you’ll need to install this code on your Arduino:
#include <MIDI.h>
struct CustomBaudRateSettings : public MIDI_NAMESPACE::DefaultSerialSettings {
static const long BaudRate = 115200;
};
#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial1);
#else
MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial);
#endif
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>> MIDI((MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>&)serialMIDI);
int lastNote = -1; // Variable to keep track of the last note played
int Scale = 2; // Global variable to select the scale
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
analogReference(DEFAULT);
}
void loop() {
int sensorValue = analogRead(A0); // Read the analog input on A0
// Ensure the mapped value is always between 0 and 7
int index = constrain(map(sensorValue, 170, 220, 0, 7), 0, 7);
// Define several scales in the same pitch range
int scales[5][8] = {
{60, 62, 64, 65, 67, 69, 71, 72}, // Major Scale
{60, 62, 63, 65, 67, 68, 70, 72}, // Minor Scale
{60, 62, 64, 67, 69, 72, 74, 76}, // Pentatonic Major
{60, 63, 65, 66, 67, 70, 72, 75}, // Blues Scale
{60, 62, 63, 66, 67, 68, 71, 72} // Harmonic Minor Scale
};
// Select the scale based on the global variable 'Scale'
int *selectedScale = scales[constrain(Scale - 1, 0, 4)]; // Ensure Scale is within valid range
int note = selectedScale[index]; // Select the note from the chosen scale based on the sensor value
// Check if the new note is different from the last note played
if (note != lastNote) {
// If there is a note currently playing, stop it
if (lastNote != -1) {
MIDI.sendNoteOff(lastNote, 0, 1); // Stop the last note
}
// Play the new note
MIDI.sendNoteOn(note, 127, 1);
lastNote = note; // Update the last note played
}
delay(100); // Short pause to allow for sensor value to stabilize
}
In the line of code where index is set, there are two values, 170 and 220 — for my situation those were the min and max values that my ADC measured – you may need to change these for your setup.