Hello,
I want to expant the number of outputs from a single dac to 16 analog voltages using a CD74HC4067 board and holding capactiors going to a op amp buffer.
But deffinitly need some pointers because i cannot seem to get the order right.
I want to turn 16 midi cc messages on 16 channels to control voltages and depending on the midi channel i want to port them to a sertain demux output.
So i want to map midi channel spesific cc data to dacwrite where the midi channel follows the currentchannel of the demux. Hope this makes sense.
When demux channel is at lets say 5 i want to only map cc messages from midichannel 6 to the dacwrite function.
The sketch i made now has some problems. and i am not at all sure if im on the right track. i get a super stepped output from the dac so i think it only maps cc to the dac for a short burst of time.
And one problem i am having is that the dac does not reset to 0v after communication with the midi to dacwrite (channel specific) stops. So all these values are keps at the last written value and thus bleed into other demux outputs pins.
I dont think i have the ''/ Only write to the DAC if the current mux channel matches the MIDI channel'' part right. And wonder if there is a better way to do this. maybe even in the setup.
I found the following on how to set up the dac of the esp32s2 but tried to install the different dac drivers to no avail. Are they compatible with the arduino ide? I would like to try the oneshot.h driver.
hosted on github but linked and explained here: Analog to Digital Converter (ADC) Oneshot Mode Driver - ESP32-S2 - — ESP-IDF Programming Guide latest documentation
The holding capacitor and buffer should work a bit like a sample and hold. or a slewlimited to slew the sudo stepped output from the 4067.
This is my sketch:
#include <Arduino.h>
#include <USB.h>
#include <ESPNATIVEUSBMIDI.h>
#include <MIDI.h>
const int S0 = 33;
const int S1 = 35;
const int S2 = 37;
const int S3 = 39;
int currentChannel = 0;
ESPNATIVEUSBMIDI espnativeusbmidi;
MIDI_CREATE_INSTANCE(ESPNATIVEUSBMIDI, espnativeusbmidi, MIDI)
void handleControlChange(byte channel, byte control, byte value);
void setup() {
espnativeusbmidi.begin();
USB.productName("ESP Native MIDI");
USB.begin();
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleControlChange(handleControlChange);
dacWrite(DAC1, 0);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
Serial.begin(115200);
}
void setPin(int outputPin) {
digitalWrite(S0, (outputPin & 0x01) ? HIGH : LOW);
digitalWrite(S1, (outputPin & 0x02) ? HIGH : LOW);
digitalWrite(S2, (outputPin & 0x04) ? HIGH : LOW);
digitalWrite(S3, (outputPin & 0x08) ? HIGH : LOW);
}
void loop() {
static unsigned long previousMillis = 0;
const unsigned long channelSwitchInterval = 10; // Delay between channel switches (milliseconds)
unsigned long currentMillis = millis();
// Check if it's time to switch channels
if (currentMillis - previousMillis >= channelSwitchInterval) {
previousMillis = currentMillis;
// Switch to the next channel
currentChannel = (currentChannel + 1) % 16;
setPin(currentChannel);
//Serial.print("Current Channel: ");
//Serial.println(currentChannel);
}
// Process incoming MIDI messages
MIDI.read();
}
void handleControlChange(byte channel, byte control, byte value) {
// Check if the received control change is from CC3
if (control == 3) {
// Map the MIDI value to the DAC range
int dacValue = map(value, 0, 127, 0, 255);
// Only write to the DAC if the current mux channel matches the MIDI channel
if (currentChannel + 1 == channel) {
dacWrite(DAC1, dacValue);
}
}
}