I am attempting to send a value between 0 and 4096 to a 12 bit DAC using the serial monitor. My code successfully prints to the monitor what I type into it, but fails to send that value to the DAC. Please help.
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
#define DAC_RESOLUTION (9) // Set this value to 9, 8, 7, 6 or 5 to adjust the resolution
#include <OneWire.h>
#define ONE_WIRE_BUS 10
int setPoint;
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A6, OUTPUT);
Serial.begin(9600);
dac.begin(0x62);
dac.setVoltage(0, false);
}
void loop() {
while (Serial.available() == 0) {}
//delay(500);
Serial.println("");
Serial.println("DAC set to:");
while (Serial.available() > 0) {
Serial.write(Serial.read()); // note it is Serial.WRITE
}
setPoint = Serial.parseInt();
dac.setVoltage(setPoint, false);
}
This loop reads all of the characters in the serial buffer and passes them back out to the terminal. Once a character is read with read() it is removed from the serial input buffer.
By the time you get here, there is nothing left for parse() to read because the previous loop has read all of the characters, forwarded them on to serial out and emptied the serial buffer while doing so.
All you really need is:
while (Serial.available() > 0) {
setPoint = Serial.parseInt();
Serial.println(setPoint);
dac.setVoltage(setPoint, false);
}
parseInt() takes care of reading digits plus return and converting received characters into a value that is then placed into the int variable. All you then need to do is print the captured value to the terminal to confirm it has been correctly received and then send it to the DAC.
The library examples for that device use
#include <Wire.h>
BitSeeker,
Thanks for the reply! I tried your code, and it almost works. For a moment the DAC begins to do as commanded, but then a mysterious zero is sent which sets its output back to zero.
Thanks, that seems to work about the same as #include <OneWire.h>.
Nope. We're running at 9600 baud. Read one character, buffer's empty, code moves on long before the next character is "available".
But, the method is flawed anyway.
Yes, absolutely correct. In Practice only one character would actually be received and processed at a time.
Regarding parseInt(), the Arduino documentations says the following:
This is only a speculation, but I am wondering how it might behave when receiving a CR+LF? Might that be interpreted as a value terminated by the CR and then nothing terminated by NL which returns a value of zero? I don't know, but if you are using the Serial Monitor and if its set to 'Both NL + CR', then you might try an experiment and change the line ending character to just 'New line' and see what happens.
Do you have a suggested approach?
Hi, @BitSeeker
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Have you tried one of the Library examples?
Tom....

Selecting No Line Ending did the trick. I no longer get a mysterious 0 being out put to the DAC! Thank You!
Hi, that would not be my first choice. The full design is proprietary. But thanks for the offer. This test circuit is simple. Its just an Arduino Micro connected to a 12 bit DAC via I2C.