// LM: Adapted from Sparkfun Example Waveform Sketch (See comments below) // Drive analog microammeter from DAC output // Add 7-segment LED and correlated tone /****************************************************************************** MCP4725 Example Waveform Sketch Joel Bartlett SparkFun Electronics Sept. 11, 2014 https://github.com/sparkfun/MCP4725_Breakout This sketch takes data from a lookup table to provide waveforms to be generated by the MCP4725 DAC. Development environment specifics: Arduino 1.0+ Hardware Version V14 This code is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round! Distributed as-is; no warranty is given. This code builds off the sketch written by Mark VandeWettering, which can be found here: http://brainwagon.org/2011/02/24/arduino-mcp4725-breakout-board/ */ #include //Include the Wire library to talk I2C #include "SevenSeg.h" //This is the I2C Address of the MCP4725, by default (A0 pulled to GND). //Please note that this breakout is for the MCP4725A0. #define MCP4725_ADDR 0x60 //For devices with A0 pulled HIGH, use 0x61 const int unit = 455; // 4095 divided by 9 const long BLINK_DURATION = 50; const long HALFSEC = 500; const long MOMENT = 750; const long ONESEC = 1000; const long TWOSEC = 2000; int digit = 0; // Currently displayed digit // 756 digits of pi -> String pi = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870"; boolean SEVENSEG = true; SevenSeg disp(12, 11, 9, 7, 6, 4, 5); const boolean TONE_ENABLED = true; const int TONES = 10; // One per digit const int MODE_LEN = 32; // Optionally switch scale modes after this many notes (optional) // C-major scale, with subscript 0=C, 1=D, 2=E, ... 7=C', 8=D', 9=E' const int MAJOR[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 659}; // C-major octave + third (C to E') const int MINOR[] = {262, 294, 311, 349, 392, 415, 494, 523, 587, 622}; // C-minor octave + third (C to E-flat') const int TONE_OUT = 8; // Tone pin void setup() { Wire.begin(); // (Sparkfun) Set A2 and A3 as Outputs to make them our GND and Vcc, // which will power the MCP4725 (so that DAC header can be plugged into adjacent analog pins) pinMode(A2, OUTPUT); pinMode(A3, OUTPUT); pinMode(TONE_OUT, OUTPUT); digitalWrite(A2, LOW);//Set A2 as GND digitalWrite(A3, HIGH);//Set A3 as Vcc disp.setCommonCathode(); disp.setDPPin(10); disp.changeDigit(1); // Superfluous /* beep(10); // Countdown if desired delay(MOMENT); */ } void loop() { Wire.beginTransmission(MCP4725_ADDR); Wire.write(64); // cmd to update the DAC if (digit >= pi.length()) { // Pause and restart at beginning digit = 0; disp.changeDigit(1); // Blank the last displayed digit delay(TWOSEC); } char c = pi.charAt(digit++); int dac = (int) c - 48; int note = MAJOR[dac]; // Uncomment the following to use C-minor scale on alternate MODE_LEN phrases: /* if ((digit / MODE_LEN) % 2 == 1) note = MINOR[dac]; */ if (SEVENSEG || TONE_ENABLED) { blink(); // To distinguish successive repeating digits } if (SEVENSEG) { disp.writeDigit(c); } dac *= unit; // Map each digit to 12-bit value for DAC // One byte per Wire.write Wire.write(dac >> 4); // As in Sparkfun example (8 most significant bits) Wire.write(dac & 15 << 4); // (4 least significant bits) Wire.endTransmission(); if (TONE_ENABLED) tone(TONE_OUT, note); delay(MOMENT - BLINK_DURATION); if (TONE_ENABLED) noTone(TONE_OUT); } void blink() { disp.changeDigit(1); delay(BLINK_DURATION); } void beep(int times) { // Startup beeps to allow time to start recording, etc. for (int i=0; i