problem with arduino coding

hi guys!

I am trying to generate a rising ramp voltage using DAC MCP4725.

Now the problem occurs when i try to generate two different voltage output levels (as needed in real life situations ). The DAC op never settles (keeps varying) for both cases.

I tried to comment out the code for second vtg level and i get op as expected i.e a vtg starting from 0 and rising linearly finally fixing at 12.2mv.

But i need at least two different voltage ops.

my code:

include<Wire.h>

define MCP4725 0x61 //MCP4725 address as 0x61 ,

unsigned int c,d,e,f;
byte buffer[3];
int A0state,A3state,range;

void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
pinMode (A0,INPUT);
pinMode (A1, INPUT);
pinMode (A3, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
/* switch (range)
{
case 1:/
A0state= digitalRead(A0);
if (A0state==HIGH)
{
c++;
if(c>=9)// 1 step =1.35mv so 9
1.35=12.15mv
c=9;}
else
c=0;
getVoltage(c);

/A3state=digitalRead(A3);
if (A3state==HIGH)
{
d++;
if(d>=5)
d=5;}
else
d=0;
getVoltage(d);
/

}

void getVoltage ( unsigned int n)
{
Serial.println (n);
buffer[0]= 0b01000000;
buffer[1]= n>>4; // sending msb values
buffer[2]= n<<4; // sending lsb values
Wire.beginTransmission(MCP4725); //Joins I2C bus with MCP4725 with 0x61 address

Wire.write(buffer[0]); //Sends the control byte to I2C
Wire.write(buffer[1]); //Sends the MSB to I2C
Wire.write(buffer[2]); //Sends the LSB to I2C

Wire.endTransmission(); //Ends the transmission
delay(400);
}

Please help.
Cherrs,
Marrc
(P.S: forgive my rather rustic code as i am a newbee)

Please go back and change that quote section to a code section, we need to see the indentation to read code.

Use [​code][​/code] tags when posting, not quote.

# include<Wire.h>
# define MCP4725 0x61  //MCP4725 address as 0x61  ,

unsigned int c;
byte buffer[3];
int A0state;

void setup() {
 Wire.begin();   
 Serial.begin(9600);
 pinMode(A0, INPUT);
 pinMode(A1, INPUT);
 pinMode(A3, INPUT);
}

void loop() {
 A0state = digitalRead(A0);
 if (A0state == HIGH)
 {
    c++;   
    if(c >= 9) // 1 step =1.35mv so 9*1.35=12.15mv
    {
      c = 9;
    }
  }
  else
  {
    c = 0;
  }
  setVoltage(c);
}
 
void setVoltage (unsigned int n)
{
  Serial.println (n);
 
  buffer[0]= 0b01000000;
  buffer[1]= n>>4;  // sending msb values
  buffer[2]= n<<4;  // sending lsb values
 
  Wire.beginTransmission(MCP4725);  //Joins I2C bus with MCP4725 with 0x61 address
  Wire.write(buffer[0]);            //Sends the control byte to I2C
  Wire.write(buffer[1]);            //Sends the MSB to I2C
  Wire.write(buffer[2]);            //Sends the LSB to I2C 
  Wire.endTransmission();           //Ends the transmission
 
  delay(400);
}

I tidied up a bit so I could read it. So you want “c” (not a great choice of names BTW) to increase to 9 and stop when A0 is high? Otherwise reset to 0?

Two different voltage levels with the same MCP4725? How will that work, it only has one output?

The original code you had repeatedly sent one voltage, then the other. That was because the call to “getVoltage” (I renamed it setVoltage!) was outside the conditional “if”. If you want it to only happen when some condition is true, place inside the required if or else. Your code indentation and bracketing sucked as well, so if you got that right you might have been able to see the wood for the trees… (sorry).

As a tip for a beginner I would avoid if/else without matching { } and indentation. It’s legal C, but it’s easy to make mistakes. In fact at my 2 professional C coding jobs its a coding standard to ALWAYS use braces for clarity, even if there is only one thing in the if/else code block.