-1
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
Adafruit_MCP4725 dac;
 
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

//declaring variables================================

uint16_t adc0;
uint16_t adc1;
//int adc0;
//int adc1;
int DACstartvalue_init;
float volts0;
float volts1;
long time0;
long time1;
long time2;
long time3;
long time4;


// variables to hold the parsed data
int DACstartvalue = 0;
int DACendvalue = 0;
int increment = 0;
float interval = 0;
int noofcycle = 0;
boolean newData = false;

//====================================================

void setup() {
    Serial.begin(115200);
    //Wire.setClock(400000); 
    Wire.setClock(3400000L); 
    dac.begin(0x60);
    ads.begin(0x48);
    ads.setGain(GAIN_TWOTHIRDS);
    ads.setDataRate(0x00E0); 
    ads.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1, /*continuous=*/true);
  }

//======================================================

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//=========================================================

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    DACstartvalue = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    DACendvalue = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    increment = atoi(strtokIndx);     // convert this part to a integer

    strtokIndx = strtok(NULL, ",");
    interval = atof(strtokIndx);      // convert this part to a float

    strtokIndx = strtok(NULL, ",");   // this continues where the previous call left off
    noofcycle = atoi(strtokIndx);     // convert this part to an integer

}

void TEST() 
{
      Serial.println("<");
      delay(5000);
      time0=millis();
      time1=time0;
      dac.setVoltage(DACstartvalue,false);
      adc0 = ads.readADC_SingleEnded(0);
      volts0 = ads.computeVolts(adc0);
      adc1 = ads.readADC_SingleEnded(3);
      volts1 = ads.computeVolts(adc1);
      Serial.print(0.0);
      Serial.print(",");
      Serial.print(volts0,4);
      Serial.print(",");
      Serial.println(volts1,4);
      DACstartvalue_init=DACstartvalue;
      int i=0;
      while (i < noofcycle)
      {
      // Forward Scan Loop
      while(1)
      { 
        time2=millis();
        if (time2-time1>=interval)
        {
         DACstartvalue_init=DACstartvalue_init+increment;
         if (DACstartvalue_init>=DACendvalue)
         {
         dac.setVoltage(DACendvalue,false);
         adc0 = ads.readADC_SingleEnded(0);
         volts0 = ads.computeVolts(adc0);
         adc1 = ads.readADC_SingleEnded(3);
         volts1 = ads.computeVolts(adc1);
         Serial.print((time2-time0));
         Serial.print(",");
         Serial.print(volts0,4);
         Serial.print(",");
         Serial.println(volts1,4);
         time1=time2;
         break;
         }
         dac.setVoltage(DACstartvalue_init,false);
         adc0 = ads.readADC_SingleEnded(0);
         volts0 = ads.computeVolts(adc0);
         adc1 = ads.readADC_SingleEnded(3);
         volts1 = ads.computeVolts(adc1);
         Serial.print((time2-time0));
         Serial.print(",");
         Serial.print(volts0,4);
         Serial.print(",");
         Serial.println(volts1,4);
         time1=time2;
         }
       }
  // Reverse Scan Loop     
  while(1)
      {
        time3=millis();
        if (time3-time1>=interval)
        {
         DACstartvalue_init=DACstartvalue_init-increment;
         if (DACstartvalue_init<=DACstartvalue)
         {
          dac.setVoltage(DACstartvalue,false);
          adc0 = ads.readADC_SingleEnded(0);
          volts0 = ads.computeVolts(adc0);
          adc1 = ads.readADC_SingleEnded(3);
          volts1 = ads.computeVolts(adc1);
          Serial.print((time3-time0));
          Serial.print(",");
          Serial.print(volts0,4);
          Serial.print(",");
          Serial.println(volts1,4);
          time1=time3;
          break;
          }
          dac.setVoltage(DACstartvalue_init,false);
          adc0 = ads.readADC_SingleEnded(0);
          volts0 = ads.computeVolts(adc0);
          adc1 = ads.readADC_SingleEnded(3);
          volts1 = ads.computeVolts(adc1);
          Serial.print((time3-time0));
          Serial.print(",");
          Serial.print(volts0,4);
          Serial.print(",");
          Serial.println(volts1,4);
          time1=time3;
        }
      }
i++;
  }
Serial.println(">");
}

void loop() 
{
    recvWithStartEndMarkers();
    if (newData == true) 
    {
        strcpy(tempChars, receivedChars);
        parseData();
        TEST();
        newData = false;
   }
}

I AM USING ARDUINO NANO. I WANT TO SET A SPECIFIC VOLTAGE USING MCP4725 AND READ THE SAME USING ADS1115 AT THE SAME INSTANCE. HELP REQUIRED:

  1. TO RUN THE TEST LOOP AT A SPECIFIED INTERVAL
  2. TO REFINE THE CODE
  3. IF POSSIBLE, TO SET AND MEASURE THE VOLTAGE AT DIFFERENT BUT SPECIFIED TIME INTERVAL
9
  • A) Please do not shout, all caps are read as such. B) Check out the blinky example without delay. The main point is to use millis(). Sep 2 at 6:19
  • I have used millis() but still there is some time lag. Sep 2 at 6:27
  • Please edit your question and add such important requirements. While you at it, rewrite the rest of your prose. How much lag is acceptable? And how much lag do you have? Sep 2 at 6:41
  • Honestly I don't quite understand what the actual problem with your current code is. You are already doing timed stuff with millis. If you want to time at different intervals, just an additional timestamp variable (like time3) and an additional if condition for the thing, that you want to time with a different interval. And what exactly do you mean with lack?
    – chrisl
    Sep 2 at 7:18
  • ok thanks.and how to set the sampling rate to 860 SPS. Sep 2 at 8:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.