/* * LiquidatorDAC.ino * G. Forrest Cook W0RIO * Revision: Feb 4, 2022 * License: GPL Version 3 * * Modulate an 8 bit DAC with arbitrary waveforms. * See: http://www.solorb.com/elect/musiccirc/liquidator1/ * Uses a DAC0808 or MC1408P8 Digital to Analog Converter connected to * an Arduino Diecimila board with an Atmel ATMEGA168 processor, it should * also work on a newer Arduino Uno board with an ATMEGA328P processor. * The waveform speed is set by a pot on analog input 1. * An R-C delay is used on analog input 0 to detect the initial power up. * Subsequent presses of the reset button rotate through three different * waveforms. The selected waveform number is stored in EEPROM. */ #include byte eepromval; int LedPin = 13; // on-board red LED int RedPin = 8; int GreenPin = 9; // RGB LEDs (PB0-2) int BluePin = 10; int WaveNum = 0; int dtime; int indx = 0; #define STEPS1 255 // normal sine wave #define STEPS3 255 // Big Bottom exponential sine wave (for driving LED/LVR) #define STEPS2 255 // inverted Big Bottom wave const byte wavetable1[] = {0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76, 79, 81, 84, 87, 90, 93, 96, 99, 103, 106, 109, 112, 115, 118, 121, 124, 127, 131, 134, 137, 140, 143, 146, 149, 152, 156, 159, 162, 165, 168, 171, 174, 176, 179, 182, 185, 188, 191, 193, 196, 199, 201, 204, 206, 209, 211, 213, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 237, 239, 240, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 240, 239, 237, 236, 234, 232, 230, 228, 226, 224, 222, 220, 218, 216, 213, 211, 209, 206, 204, 201, 199, 196, 193, 191, 188, 185, 182, 179, 176, 174, 171, 168, 165, 162, 159, 156, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, 106, 103, 99, 96, 93, 90, 87, 84, 81, 79, 76, 73, 70, 67, 64, 62, 59, 56, 54, 51, 49, 46, 44, 42, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 13, 12, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0}; const byte wavetable2[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 30, 32, 34, 36, 37, 39, 41, 43, 45, 47, 49, 51, 54, 56, 58, 61, 63, 66, 68, 71, 74, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 107, 110, 113, 116, 120, 123, 127, 130, 134, 137, 141, 144, 148, 152, 155, 159, 162, 166, 170, 173, 177, 180, 184, 187, 191, 194, 197, 201, 204, 207, 210, 213, 216, 219, 222, 225, 228, 230, 233, 235, 237, 239, 241, 243, 245, 246, 248, 249, 251, 252, 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 253, 252, 251, 249, 248, 246, 245, 243, 241, 239, 237, 235, 233, 230, 228, 225, 222, 219, 216, 213, 210, 207, 204, 201, 197, 194, 191, 187, 184, 180, 177, 173, 170, 166, 162, 159, 155, 152, 148, 144, 141, 137, 134, 130, 127, 123, 120, 116, 113, 110, 107, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 74, 71, 68, 66, 63, 61, 58, 56, 54, 51, 49, 47, 45, 43, 41, 39, 37, 36, 34, 32, 30, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 15, 14, 13, 12, 11, 10, 9, 9, 8, 7, 6, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0}; const byte wavetable3[] = {255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 249, 248, 247, 246, 245, 245, 244, 243, 242, 241, 240, 238, 237, 236, 235, 233, 232, 231, 229, 228, 226, 225, 223, 221, 219, 218, 216, 214, 212, 210, 208, 206, 203, 201, 199, 197, 194, 192, 189, 187, 184, 181, 179, 176, 173, 170, 167, 164, 161, 158, 155, 152, 148, 145, 142, 138, 135, 132, 128, 125, 121, 118, 114, 111, 107, 103, 100, 96, 93, 89, 85, 82, 78, 75, 71, 68, 64, 61, 57, 54, 51, 48, 45, 42, 39, 36, 33, 30, 27, 25, 22, 20, 18, 16, 14, 12, 10, 8, 7, 6, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 25, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 61, 64, 68, 71, 75, 78, 82, 85, 89, 93, 96, 100, 103, 107, 111, 114, 118, 121, 125, 128, 132, 135, 138, 142, 145, 148, 152, 155, 158, 161, 164, 167, 170, 173, 176, 179, 181, 184, 187, 189, 192, 194, 197, 199, 201, 203, 206, 208, 210, 212, 214, 216, 218, 219, 221, 223, 225, 226, 228, 229, 231, 232, 233, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 245, 246, 247, 248, 249, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255}; void setup() { DDRB = 0x27; // Set port B to outputs for RGB and board LED. DDRD = 0xFF; // Set port D to 8 outputs for DAC. WaveNum = 0; // start with table 0 eepromval = EEPROM.read(0); // get the stored wave table number if (analogRead(A0) <= 512) // initial reset, keep previous wave table { if (eepromval == 0) { WaveNum = 0; PORTB = 3; // Set RGB LED } else if (eepromval == 1) { WaveNum = 1; PORTB = 6; // Set RGB LED } else { WaveNum = 2; PORTB = 5; // Set RGB LED } } else // reset while running detected, step table #. { if (eepromval == 0) // increment to table 1 { EEPROM.write(0, 1); WaveNum = 1; PORTB = 6; Blink13(2); } else if (eepromval == 1) // increment to table 2 { EEPROM.write(0, 2); WaveNum = 2; PORTB = 5; Blink13(3); } else // reset to table 0 { EEPROM.write(0, 0); WaveNum = 0; PORTB = 3; Blink13(1); } } } void loop() { if (WaveNum == 0) // Pick one of the wave tables and loop infinitely. { while (1) { PORTD = wavetable1[indx]; dtime = (analogRead(A1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS1) indx = 0; } } else if (WaveNum == 1) { while (1) { PORTD = wavetable2[indx]; dtime = (analogRead(A1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS2) indx = 0; } } else { while (1) { PORTD = wavetable3[indx]; dtime = (analogRead(A1) + 5) * 8; delayMicroseconds(dtime); indx++; if (indx >= STEPS3) indx = 0; } } } void Blink13 (byte count) // blink the Arduino yellow LED on port 13 { byte i; for (i=0; i