//Guitar frequency to 1V/Oct CV output. //Input signal via fundamental extractor + sidechain envelope detector //Mick Bailey 2023 //Version 2.1 #define LED 5 // LED pin 5 #define gate_out 7 // Gate output pin #define env_det 2 // envelope detector input - pull down resistor to ground needed #define guitar_in 8 // guitar input #include //Log(2) library #include Adafruit_MCP4725 MCP4725; int Htime; // integer for storing high time int Ltime; // integer for storing low time float Ttime; // integer for storing total time of a cycle float frequency; // for storing frequency float voltage_conversion; // frequency to voltage conversion float dac_value; // calculated DAC value int dac_output; // rounded DAC output (0 to 4095) void setup() { // outputs pinMode(LED, OUTPUT); pinMode(gate_out, OUTPUT); // set outputs low digitalWrite(LED, LOW); digitalWrite(gate_out, LOW); pinMode(env_det, INPUT); //envelope detector pin 2 pinMode(guitar_in, INPUT); //guitar input pin 8 MCP4725.begin(0x60); // set the I2C Address of the MCP4725 } void loop() { // note detection if (digitalRead(env_det) == HIGH) { //Calculate frequency and output to DAC Htime = pulseIn(guitar_in, HIGH); // read high time Ltime = pulseIn(guitar_in, LOW); // read low time Ttime = Htime + Ltime; // calculate total time frequency = 1000000 / Ttime; // calculate frequency voltage_conversion = log2(frequency / 55) ; // frequency to voltage in volts conversion dac_value = voltage_conversion * 819; // get DAC value as an integer dac_output = dac_value; // set DAC integer output - MCP4725 only accepts integers. dac_output = max(dac_output, 0); // limit the minimum DAC value to 0 dac_output = min(dac_output, 4095); // limit the maximum DAC value to 4095 MCP4725.setVoltage(dac_output, false); // send output to DAC bitSet (PORTD, gate_out); // turn the Gate on bitSet (PORTD, LED); // turn LED on } // note ended else { bitClear (PORTD, gate_out); // turn the Gate off bitClear (PORTD, LED); // turn LED off } }