MCP4725 eval module only changing large values

Hi all

Posted a thread on the adafruit website.

Am starting to realize it maybe a programming mistake in that some rounding is occuring that might be keeping this providing the full range of analog values

can someone check if this is segment of code is logical

Seg2 comes from serial command.
int seg3= seg2.toInt();
dac.setVoltage((((seg3/1000)* 4095) / 5), false); //Set voltage to 1V
Serial.println(seg2); //CMD1 responce

MCP4725 Eval not responding to changes < 0.5V - adafruit industries

larger segment of code

/*

 */
 #include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;


#define digitalPin13 13   //  Port 13 for reading the push button on the light field module 
#define DAC_RESOLUTION    (9)   // Set this value to 9, 8, 7, 6 or 5 to adjust the resolution
 #define potentiometerPin A1

//Interrupt Setup Variables
const int ledPin = LED_BUILTIN;
volatile unsigned long timer1_counter = 31250; // Preload value for 1 second interrupt



int x =0;          
int interlockstate=0;
int pushbuttonlightfield=0;
int analogPin = A5;
int Thumb_Dimmer_Pin = A1;
int Thermo_Pin = A2;
int ledPinFlashY = 0;
int ledPinFlashG = 0;
int flashcount= 0;
int turnontrimpot = 0;
int onetime =0;
int timeouttime_default= 1000;
float voltagevalue=0;
float Thumbvoltagevalue=0;
float Thermovoltagevalue=0;
float trimpotlowerlimit = 3.0;
char delimiter = ',';

//ISR//float value = 100;   //3035;


void setup()
{ 
  noInterrupts(); // Disable all interrupts during setup
  Serial.begin(9600); //begin serial data
    Serial.setTimeout(timeouttime_default) ;  // set a short timeout otherwise it hold stale stuff for the next command

    
   //Interrupt Setup
   TCCR1A = 0;
   TCCR1B = 0;
   TCNT1 = timer1_counter; // preload timer
   TCCR1B |= (1 << WGM12); // CTC mode
   TCCR1B |= (1 << CS12);  // 256 prescaler
   TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
   interrupts(); // enable all interrupts


   //This actually needs to be here but dont have it spit out an output
   //It is initalizing the DAQ
      //Serial.println(" MCP4725 Test");
      if (dac.begin(0x62))    {      //could be 0x60 or 0x62
          }

   //setup the LEDs to toggle blinking when PCB inserted
   //software needs to turn this off
   digitalWrite(digitalPin12, LOW); // turn off  Yellow LED in filter module
   digitalWrite(digitalPin10, LOW); // turn on Green LED on filter module
   ledPinFlashY = 12;
   ledPinFlashG = 10; 


}    //   close setup

ISR(TIMER1_COMPA_vect) {
  TCNT1 = timer1_counter; // preload timer
  digitalWrite(ledPinFlashG, !digitalRead(ledPinFlashG)); // Example: Toggle LED
  digitalWrite(ledPinFlashY, !digitalRead(ledPinFlashY)); // Example: Toggle LED
}

void loop() 
{

      delay (500);    //rev 3, delay to slow system down


  //check if serial communication is avaiable
  if (Serial.available() > 0) {
      String rx_byte = Serial.readStringUntil('\n');

       // Find the index of the first delimiter
       // DIMM send string is "DIMM,1"
          int firstDelimiterIndex = rx_byte.indexOf(delimiter);
          String seg1 = rx_byte.substring(0, firstDelimiterIndex);
          int secondDelimiterIndex = rx_byte.indexOf(delimiter, firstDelimiterIndex + 1);
          String seg2 = rx_byte.substring(firstDelimiterIndex + 1, secondDelimiterIndex);

        rx_byte.trim();
       
      if (seg1 == "DIMM")
         {
          int seg3= seg2.toInt();
          dac.setVoltage((((seg3/1000)* 4095) / 5), false);    //Set voltage to 1V
          Serial.println(seg2);      //CMD1 responce
        } 

  }

}

Modify

dac.setVoltage((((seg3/1000)* 4095) / 5), false);

by

dac.setVoltage((((seg3/1000.0)* 4095) / 5.0), false);

Note that now the divisors are 1000.0 and 5.0 otherwise the divisions are performed between integers and returns integers results.

thank you so much !

was stuck on that for so long

I am confused.. mathematically the difference would be

dac.setVoltage((((seg3/1000.0)* 4095) / 5.0), false);
dac.setVoltage((((seg3/1000)* 4095) / 5), false);

(((3000/1000)* 4095) / 5) → 2457

(((3200/1000)* 4095) / 5) → 2620.8

So even as integer values, I woudl get 2457 vs 2620

but the 3200 start point was not producing a result

I am guessing the difference might be it doesnot know how to process the remainder.?

When the math are all integer types, the outcome is truncated
(looks like the remainder ignored, in fact there is none)
3200 / 1000 => (int)/(int) => 3
3200 / 1000.0 => (int) / (float) becomes (float) / (float) => 3.2


Performance wise you might write

dac.setVoltage(seg3 * 0.001 * 4095 * 0.2, false);

As the compiler sees three constants it will be calculated (partially) compile time resulting in

dac.setVoltage(seg3 * 0.819), false);

So runtime only one multiplication left.