# coding problem

**URL:** <https://community.sparkfun.com/t/coding-problem/28416>\
**Category:** Arduino\
**Created:** [March 10, 2014, 3:59am UTC](https://community.sparkfun.com/t/coding-problem/28416 "2014-03-10T03:59:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 10, 2014, 3:59am UTC](https://community.sparkfun.com/t/coding-problem/28416/1 "2014-03-10T03:59:42Z")

</div>

Hello and thanks for the opportunity.

I am a severe newbie!

I am using Arduino UNO and added Adafruit A/D converter ADS1015 for battery charger monitor, with LCD to show amps and volts.

I have followed tutorials but still have some problems.

In this case the error in IDE reads, “expected unqualified-id before ‘{’ token” on line 154.

Why is that, and how do I fix it?

Thanks so much for your help in advance.

===========================================

/\*!

#if ARDUINO \>= 100

#include “Arduino.h”

#else

#include “WProgram.h”

#endif

#include \<Wire.h\>

/\* I2C ADDRESS/BITS \*/

#define ADS1015\_ADDRESS (0x48) // 1001 000 (ADDR = GND)

/\* CONVERSION DELAY (in mS) \*/

#define ADS1015\_CONVERSIONDELAY (1)

/\* POINTER REGISTER \*/

#define ADS1015\_REG\_POINTER\_MASK (0x03)

#define ADS1015\_REG\_POINTER\_CONVERT (0x00)

#define ADS1015\_REG\_POINTER\_CONFIG (0x01)

#define ADS1015\_REG\_POINTER\_LOWTHRESH (0x02)

#define ADS1015\_REG\_POINTER\_HITHRESH (0x03)

/\* CONFIG REGISTER \*/

#define ADS1015\_REG\_CONFIG\_OS\_MASK (0x8000)

#define ADS1015\_REG\_CONFIG\_OS\_SINGLE (0x8000) // Write: Set to start a single-conversion

#define ADS1015\_REG\_CONFIG\_OS\_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress

#define ADS1015\_REG\_CONFIG\_OS\_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion

#define ADS1015\_REG\_CONFIG\_MUX\_MASK (0x7000)

#define ADS1015\_REG\_CONFIG\_MUX\_SINGLE\_0 (0x4000) // Single-ended AIN0

#define ADS1015\_REG\_CONFIG\_MUX\_SINGLE\_1 (0x5000) // Single-ended AIN1

#define ADS1015\_REG\_CONFIG\_MUX\_SINGLE\_2 (0x6000) // Single-ended AIN2

#define ADS1015\_REG\_CONFIG\_MUX\_SINGLE\_3 (0x7000) // Single-ended AIN3

#define ADS1015\_REG\_CONFIG\_PGA\_MASK (0x0E00)

#define ADS1015\_REG\_CONFIG\_PGA\_6\_144V (0x0000) // +/-6.144V range = Gain 2/3

#define ADS1015\_REG\_CONFIG\_PGA\_4\_096V (0x0200) // +/-4.096V range = Gain 1

#define ADS1015\_REG\_CONFIG\_PGA\_2\_048V (0x0400) // +/-2.048V range = Gain 2 (default)

#define ADS1015\_REG\_CONFIG\_PGA\_1\_024V (0x0600) // +/-1.024V range = Gain 4

#define ADS1015\_REG\_CONFIG\_PGA\_0\_512V (0x0800) // +/-0.512V range = Gain 8

#define ADS1015\_REG\_CONFIG\_PGA\_0\_256V (0x0A00) // +/-0.256V range = Gain 16

#define ADS1015\_REG\_CONFIG\_MODE\_MASK (0x0100)

#define ADS1015\_REG\_CONFIG\_MODE\_CONTIN (0x0000) // Continuous conversion mode

#define ADS1015\_REG\_CONFIG\_MODE\_SINGLE (0x0100) // Power-down single-shot mode (default)

#define ADS1015\_REG\_CONFIG\_DR\_MASK (0x00E0)

#define ADS1015\_REG\_CONFIG\_DR\_128SPS (0x0000) // 128 samples per second

#define ADS1015\_REG\_CONFIG\_DR\_250SPS (0x0020) // 250 samples per second

#define ADS1015\_REG\_CONFIG\_DR\_490SPS (0x0040) // 490 samples per second

#define ADS1015\_REG\_CONFIG\_DR\_920SPS (0x0060) // 920 samples per second

#define ADS1015\_REG\_CONFIG\_DR\_1600SPS (0x0080) // 1600 samples per second (default)

#define ADS1015\_REG\_CONFIG\_DR\_2400SPS (0x00A0) // 2400 samples per second

#define ADS1015\_REG\_CONFIG\_DR\_3300SPS (0x00C0) // 3300 samples per second

#define ADS1015\_REG\_CONFIG\_CMODE\_MASK (0x0010)

#define ADS1015\_REG\_CONFIG\_CMODE\_TRAD (0x0000) // Traditional comparator with hysteresis (default)

#define ADS1015\_REG\_CONFIG\_CMODE\_WINDOW (0x0010) // Window comparator

#define ADS1015\_REG\_CONFIG\_CPOL\_MASK (0x0008)

#define ADS1015\_REG\_CONFIG\_CPOL\_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)

#define ADS1015\_REG\_CONFIG\_CPOL\_ACTVHI (0x0008) // ALERT/RDY pin is high when active

#define ADS1015\_REG\_CONFIG\_CLAT\_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted

#define ADS1015\_REG\_CONFIG\_CLAT\_NONLAT (0x0000) // Non-latching comparator (default)

#define ADS1015\_REG\_CONFIG\_CLAT\_LATCH (0x0004) // Latching comparator

#define ADS1015\_REG\_CONFIG\_CQUE\_MASK (0x0003)

#define ADS1015\_REG\_CONFIG\_CQUE\_1CONV (0x0000) // Assert ALERT/RDY after one conversions

#define ADS1015\_REG\_CONFIG\_CQUE\_2CONV (0x0001) // Assert ALERT/RDY after two conversions

#define ADS1015\_REG\_CONFIG\_CQUE\_4CONV (0x0002) // Assert ALERT/RDY after four conversions

#define ADS1015\_REG\_CONFIG\_CQUE\_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)

#include \<LiquidCrystal.h\>

typedef enum

{

GAIN\_TWOTHIRDS = ADS1015\_REG\_CONFIG\_PGA\_6\_144V,

GAIN\_ONE = ADS1015\_REG\_CONFIG\_PGA\_4\_096V,

GAIN\_TWO = ADS1015\_REG\_CONFIG\_PGA\_2\_048V,

GAIN\_FOUR = ADS1015\_REG\_CONFIG\_PGA\_1\_024V,

GAIN\_EIGHT = ADS1015\_REG\_CONFIG\_PGA\_0\_512V,

GAIN\_SIXTEEN = ADS1015\_REG\_CONFIG\_PGA\_0\_256V

} adsGain\_t;

class Adafruit\_ADS1015

{

protected:

// Instance-specific properties

uint8\_t m\_i2cAddress;

uint8\_t m\_conversionDelay;

uint8\_t m\_bitShift;

adsGain\_t m\_gain;

public:

Adafruit\_ADS1015(uint8\_t i2cAddress = ADS1015\_ADDRESS);

void begin(void);

uint16\_t readADC\_SingleEnded(uint8\_t channel);

int16\_t readADC\_Differential\_0\_1(void);

int16\_t readADC\_Differential\_2\_3(void);

void startComparator\_SingleEnded(uint8\_t channel, int16\_t threshold);

int16\_t getLastConversionResults();

void setGain(adsGain\_t gain);

adsGain\_t getGain(void);

private:

};

// Derive from ADS1105 & override construction to set properties

class Adafruit\_ADS1115 : public Adafruit\_ADS1015

{

};

// from Adafruit-4-channel-adc-breakouts/programming

Adafruit\_ADS1015 ads1015;

void setup(void)

{

Serial.begin(9600);

ads1015.begin();

}

void loop(void)

{

int16\_t adc0, adc1, adc2, adc3;

adc0 = ads1015.readADC\_SingleEnded(0);

adc1 = ads1015.readADC\_SingleEnded(1);

adc2 = ads1015.readADC\_SingleEnded(2);

adc3 = ads1015.readADC\_SingleEnded(3);

Serial.print("AIN0: "); Serial.println(adc0);

Serial.print("AIN1: "); Serial.println(adc1);

Serial.print("AIN2: "); Serial.println(adc2);

Serial.print("AIN3: "); Serial.println(adc3);

Serial.println(" ");

delay(1000);

}

//end of ADS1015

LiquidCrystal lcd (12, 11, 5, 4, 3, 2);

const int numReadings = 100;

int readings[numReadings];

int index = 0;

int total = 0;

int average = 0;

int inputPin = 0;

{ lcd.begin(16, 2);

Serial.begin(9600);

for (int thisReading = 0; thisReading \< numReadings; thisReading ++)

readings[thisReading] = 0;

total = total - readings [index];

readings [index] = analogRead (chgaPin);

total = total + readings[index];

index = index + 1;if (index \>= numReadings) index = 0;

average = total / numReadings ;

Serial.println (average);

delay(1);

int chgaReading = analogRead(adc0);

float chgaVolts = chgaReading \* 1.0 / 27.0 ;

lcd.setCursor(0, 0);

lcd.print (“+ A”);

lcd.setCursor (1, 0);

lcd.print(chgaVolts);

int bavoltReading = analogRead(adc2);

float bavoltVolts = bavoltReading \* 1.0 / 56.0 ;

lcd.setCursor (9, 0);

lcd.print (" V");

lcd.setCursor (10, 0);

lcd.print (bavoltVolts);

}

---

<div class="post-metadata">

**Author:** ![Valen](https://avatars.discourse-cdn.com/v4/letter/v/9dc877/32.png) [@Valen](https://community.sparkfun.com/u/Valen)\
**Post date:** [March 10, 2014, 1:43pm UTC](https://community.sparkfun.com/t/coding-problem/28416/2 "2014-03-10T13:43:07Z")

</div>

> “expected unqualified-id before ‘{’ token” on line 154.

You ended the loop with a } after the delay(1000); on line 140. So the compiler next sees a code-block opening {-bracket at line 154 without a function-header and doesn’t know what to do with this.

Tip: Please edit your post in the full editor and press the code button. This generates [code] [/ code] tags in the window. Please place that long list of code in between the inner ] and [ brackets. That makes the post easier to read, and make the page alot shorter.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 10, 2014, 2:16pm UTC](https://community.sparkfun.com/t/coding-problem/28416/3 "2014-03-10T14:16:19Z")

</div>

Please go back and read these …

[http://arduino.cc/en/Tutorial/Foundations#.Ux3HxIWmWVI](http://arduino.cc/en/Tutorial/Foundations#.Ux3HxIWmWVI)

[http://arduino.cc/en/Tutorial/Sketch#.Ux3H74WmWVI](http://arduino.cc/en/Tutorial/Sketch#.Ux3H74WmWVI)

[http://arduino.cc/en/Tutorial/BareMinimum#.Ux3BwoWmWVI](http://arduino.cc/en/Tutorial/BareMinimum#.Ux3BwoWmWVI)

[http://arduino.cc/en/Reference/Setup#.Ux3GAIWmWVI](http://arduino.cc/en/Reference/Setup#.Ux3GAIWmWVI)

[http://arduino.cc/en/Reference/Loop#.Ux3GLIWmWVI](http://arduino.cc/en/Reference/Loop#.Ux3GLIWmWVI)

[http://arduino.cc/en/Reference/Function … x3IKIWmWVI](http://arduino.cc/en/Reference/FunctionDeclaration#.Ux3IKIWmWVI)

[http://arduino.cc/en/Reference/LiquidCr … x3GXYWmWVI](http://arduino.cc/en/Reference/LiquidCrystalConstructor#.Ux3GXYWmWVI)

Your code is a mess of cut’n’paste. For example this block resides outside of the setup() and loop() functions and yet is not a function itself. Parts of it are supposed to reside in both of the aforementioned.

```auto
{ lcd.begin(16, 2);

Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading ++)
readings[thisReading] = 0;

total = total - readings [index];
readings [index] = analogRead (chgaPin);
total = total + readings[index];
index = index + 1;if (index >= numReadings) index = 0;
average = total / numReadings ;
Serial.println (average);
delay(1);

int chgaReading = analogRead(adc0);
float chgaVolts = chgaReading * 1.0 / 27.0 ;

lcd.setCursor(0, 0);
lcd.print ("+ A");
lcd.setCursor (1, 0);
lcd.print(chgaVolts);

int bavoltReading = analogRead(adc2);
float bavoltVolts = bavoltReading * 1.0 / 56.0 ;

lcd.setCursor (9, 0);
lcd.print (" V");
lcd.setCursor (10, 0);
lcd.print (bavoltVolts);
}

```

(click to open/enlarge)

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 10, 2014, 4:46pm UTC](https://community.sparkfun.com/t/coding-problem/28416/4 "2014-03-10T16:46:53Z")

</div>

Thanks very much for the detailed replies.

I will investigate EVERYTHING that’s been said to check my problems/errors and try to remedy all I’ve done incorrectly before I reply again.

My sincere thanks, and especially for the DETAILS of my errors. As a newbie at programming and microprocessors this is invaluable information to get me going to fix my errors.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 10, 2014, 9:36pm UTC](https://community.sparkfun.com/t/coding-problem/28416/5 "2014-03-10T21:36:42Z")

</div>

I would advise you to break your project into pieces. Get the code for just the LCD working first. See if you can display some hard coded messages. Then add the A/D code and get that working.

Is there some “burning” reason to use the external 12bit ADC vs the Uno’s internal 10 bit ADC ? The latter would be a lot simpler.

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 11, 2014, 1:36am UTC](https://community.sparkfun.com/t/coding-problem/28416/6 "2014-03-11T01:36:01Z")

</div>

Good morning, and thanks for yours.

I had the whole code working with UNO and LCD using internal A/D but found that the display wobbled around too much, viz. input to display (say) 20.00V would bounce between 20.30 to 19.40 and everything in between. I put this down to the D/A sampling and tried to average but eventually decided that the 12-bit D/A should solve that, i.e. discarding the LSB (or 2). So I bought the board and added the program to my sketch. Clearly I mismatched the code, winding up with my messy ‘total’ sketch.

With hindsight, I should have made this clearer to begin with.

I will go back to the tutorials to find the mistakes. I just jumped in at the deep end because I was/am eager to get the project to fly.

Thank you for your help and advice.

I will reply later after learning up some more.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 12, 2014, 3:44am UTC](https://community.sparkfun.com/t/coding-problem/28416/7 "2014-03-12T03:44:38Z")

</div>

While going to a less noisy ADC might help, I doubt noise at the ADC end was your problem. Even with a 4:1 voltage divider, a UNO lsb would only be ~20 mV. And I typically see only 1-2 lsb’s of noise. So your 500+ mV must be coming from somewhere else.

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 12, 2014, 2:13pm UTC](https://community.sparkfun.com/t/coding-problem/28416/8 "2014-03-12T14:13:59Z")

</div>

Yes, you are right.

Typo my side : I should have written 20.03 to 19.04 changes.

It still causes doubt into the system, e.g. another could say, “Why does the voltage jump around without anything happening?”

Hence my choice to switch to the 12bit ADC (and discard the LSB).

(Example : standard 3 + 1/2 digit DMM may jump 1 LSB = 20mV on 20V scale, so LSB could be ignored…)

I have since last tried to tie up my first sketch (which worked, giving me the wobbly readings) to new sketch including the ADS1015 but still get the errors with programming which is my fault. Despite much re-learning from tutorials (on-going) and advice, I can’t solve.

Also, I have not properly identified the inputs and coordinated (range, gain…) and having problems doing that.

In each case I have reduced the max. input range voltages to 0…+4.90V (irrespective of the real input voltage) to avoid damage to the APS.

My first challenge is to get the sketch working as it is after fixing what I can, so that I can find the errors and correct them in reality.

```auto
/*!

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

/* I2C ADDRESS/BITS */
#define ADS1015_ADDRESS (0x48) // 1001 000 (ADDR = GND)

/* CONVERSION DELAY (in mS) */
#define ADS1015_CONVERSIONDELAY (1)

/* POINTER REGISTER */
#define ADS1015_REG_POINTER_MASK (0x03)
#define ADS1015_REG_POINTER_CONVERT (0x00)
#define ADS1015_REG_POINTER_CONFIG (0x01)
#define ADS1015_REG_POINTER_LOWTHRESH (0x02)
#define ADS1015_REG_POINTER_HITHRESH (0x03)

/* CONFIG REGISTER */
#define ADS1015_REG_CONFIG_OS_MASK (0x8000)
#define ADS1015_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
#define ADS1015_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
#define ADS1015_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion

#define ADS1015_REG_CONFIG_MUX_MASK (0x7000)

#define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
#define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
#define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
#define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3

#define ADS1015_REG_CONFIG_PGA_MASK (0x0E00)
#define ADS1015_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
#define ADS1015_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
#define ADS1015_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
#define ADS1015_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
#define ADS1015_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
#define ADS1015_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16

#define ADS1015_REG_CONFIG_MODE_MASK (0x0100)
#define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
#define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)

#define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
#define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
#define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
#define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
#define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second

#define ADS1015_REG_CONFIG_CMODE_MASK (0x0010)
#define ADS1015_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
#define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator

#define ADS1015_REG_CONFIG_CPOL_MASK (0x0008)
#define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
#define ADS1015_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active

#define ADS1015_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
#define ADS1015_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
#define ADS1015_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator

#define ADS1015_REG_CONFIG_CQUE_MASK (0x0003)
#define ADS1015_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
#define ADS1015_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
#define ADS1015_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
#define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)

#include <LiquidCrystal.h>

typedef enum
{
  GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V,
  GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V,
  GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V,
  GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V,
  GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V,
  GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V
} adsGain_t;

class Adafruit_ADS1015
{
protected:
   // Instance-specific properties
   uint8_t m_i2cAddress;
   uint8_t m_conversionDelay;
   uint8_t m_bitShift;
   adsGain_t m_gain;

 public:
  Adafruit_ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
  void begin(void);
  uint16_t readADC_SingleEnded(uint8_t channel);
  int16_t readADC_Differential_0_1(void);
  int16_t readADC_Differential_2_3(void);
  void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
  int16_t getLastConversionResults();
  void setGain(adsGain_t gain);
  adsGain_t getGain(void);

 private:
};

// Derive from ADS1105 & override construction to set properties
class Adafruit_ADS1115 : public Adafruit_ADS1015 
{ };
// from Adafruit-4-channel-adc-breakouts/programming

Adafruit_ADS1015 ads1015;
 
void setup()

  
Serial.begin(9600);
{
ads1015.begin();
//}
 
void loop(void)
{
int16_t adc0, adc1, adc2, adc3;
 
adc0 = ads1015.readADC_SingleEnded(0);
adc1 = ads1015.readADC_SingleEnded(1);
adc2 = ads1015.readADC_SingleEnded(2);
adc3 = ads1015.readADC_SingleEnded(3);
Serial.print("AIN0: "); Serial.println(adc0);
Serial.print("AIN1: "); Serial.println(adc1);
Serial.print("AIN2: "); Serial.println(adc2);
Serial.print("AIN3: "); Serial.println(adc3);
Serial.println(" ");
delay(1000);
}
//end of ADS1015
{
Serial.begin(9600);
  
  int chgaReading = analogRead(adc0);
  float chgaVolts = chgaReading * 1.0 / 27.0 ;
    
  lcd.setCursor(0, 0);
  lcd.print ("+ A");
  lcd.setCursor (1, 0);
  lcd.print(chgaVolts);
    
  int bavoltReading = analogRead(adc2);
  float bavoltVolts = bavoltReading * 1.0 / 56.0 ;
  
  lcd.setCursor (9, 0);
  lcd.print (" V");
  lcd.setCursor (10, 0);
  lcd.print (bavoltVolts);
 }

```

which gives me errors :

bm04a:122: error: expected initializer before ‘Serial’

bm04a:123: error: expected unqualified-id before ‘{’ token

I need some help.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 14, 2014, 3:51am UTC](https://community.sparkfun.com/t/coding-problem/28416/9 "2014-03-14T03:51:35Z")

</div>

Again a lot of spaghetti tossed at the wall to see what’ll stick.

ie - what’s this for. I did a quick look at the Adafruit tutorial code and don’t remember seeing it there. Looks to be part of some (background) library code, which _should_ already be accounted for by some #include…

```auto
class Adafruit_ADS1015
{
protected:
   // Instance-specific properties
   uint8_t m_i2cAddress;
   uint8_t m_conversionDelay;
   uint8_t m_bitShift;
   adsGain_t m_gain;

 public:
  Adafruit_ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
  void begin(void);
  uint16_t readADC_SingleEnded(uint8_t channel);
  int16_t readADC_Differential_0_1(void);
  int16_t readADC_Differential_2_3(void);
  void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
  int16_t getLastConversionResults();
  void setGain(adsGain_t gain);
  adsGain_t getGain(void);

 private:
};

// Derive from ADS1105 & override construction to set properties
class Adafruit_ADS1115 : public Adafruit_ADS1015
{ };

```

Also why do you have this twice ?

```auto
Serial.begin(9600);

```

What does it do and where does it belong ?

Lastly (for now) … do you really need _all_ those defines ? It might not hurt but it sure makes it harder to read your code, which shouldn’t be much more than what’s in the tutorial.

Simple is better than complicated.

ps - noise is present in every circuit. Average and/or filter to reduce. Your assumption is that it’s all due to the ADC and that increased bit depth = less noise. This isn’t always true.

---

<div class="post-metadata">

**Author:** ![Valen](https://avatars.discourse-cdn.com/v4/letter/v/9dc877/32.png) [@Valen](https://community.sparkfun.com/u/Valen)\
**Post date:** [March 14, 2014, 11:46am UTC](https://community.sparkfun.com/t/coding-problem/28416/10 "2014-03-14T11:46:03Z")

</div>

> Newbie10:  
> …
> 
> which gives me errors :
> 
> bm04a:122: error: expected initializer before ‘Serial’
> 
> bm04a:123: error: expected unqualified-id before ‘{’ token
> 
> I need some help.
> 
> Thanks in advance.

Similar reason as my first message in this thread. Function call to Serial.begin found being outside of { } block. Actually, there isn’t even a full { } block, as you can see in the image below.

Tip: Use a program like Notepad++ to see your code with keyword highlighting, { }bracket count checks, and proper line-numbering. It can analyse your Arduino code as it is similar/same as C++ code.

It helps making the obvious beginner errors stand out. The Arduino IDE does this partially (keyword highlighting), it doesn’t show line-numbering. Nor does it show which { }-brackets are related.

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 15, 2014, 5:09am UTC](https://community.sparkfun.com/t/coding-problem/28416/11 "2014-03-15T05:09:17Z")

</div>

Okay, thanks. I downloaded Notepad++ and fixed a lot more.

Background. I did this as my first sketch (which flies like a bird) (not like MH370…) :

```auto
#include <LiquidCrystal.h>

int chgaPin = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  lcd.begin(16, 2);
}

void loop() {

  int chgaReading = analogRead(chgaPin);
  float chgaVolts = chgaReading * 1.0 / 5255.0;
  float chgaC = (chgaVolts) * 100.0;
  lcd.print("+ A");
  lcd.setCursor(2, 0);
  lcd.print(chgaC);

  delay(600);
}

```

but then decided I needed the 12-bit DAC so pasted from Adafruit which started my severe challenge.

I am now at this :

```auto
 #if ARDUINO >= 100
 #include "Arduino.h"
 #else
 #include "WProgram.h"
 #endif
 #include <Wire.h>
 #include <LiquidCrystal.h>
 
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
/* I2C ADDRESS/BITS */
#define ADS1015_ADDRESS (0x48) // 1001 000 (ADDR = GND)

/* CONVERSION DELAY (in mS) */
#define ADS1015_CONVERSIONDELAY (1)

/* POINTER REGISTER */
#define ADS1015_REG_POINTER_MASK (0x03)
#define ADS1015_REG_POINTER_CONVERT (0x00)
#define ADS1015_REG_POINTER_CONFIG (0x01)
#define ADS1015_REG_POINTER_LOWTHRESH (0x02)
#define ADS1015_REG_POINTER_HITHRESH (0x03)

/* CONFIG REGISTER */
#define ADS1015_REG_CONFIG_OS_MASK (0x8000)
#define ADS1015_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
#define ADS1015_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
#define ADS1015_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion

#define ADS1015_REG_CONFIG_MUX_MASK (0x7000)

#define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
#define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
#define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
#define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3

#define ADS1015_REG_CONFIG_PGA_MASK (0x0E00)
#define ADS1015_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
#define ADS1015_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
#define ADS1015_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
#define ADS1015_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
#define ADS1015_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
#define ADS1015_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16

#define ADS1015_REG_CONFIG_MODE_MASK (0x0100)
#define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
#define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)

#define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
#define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
#define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
#define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
#define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second

#define ADS1015_REG_CONFIG_CMODE_MASK (0x0010)
#define ADS1015_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
#define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator

#define ADS1015_REG_CONFIG_CPOL_MASK (0x0008)
#define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
#define ADS1015_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active

#define ADS1015_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
#define ADS1015_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
#define ADS1015_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator

#define ADS1015_REG_CONFIG_CQUE_MASK (0x0003)
#define ADS1015_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
#define ADS1015_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
#define ADS1015_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
#define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)

#include <LiquidCrystal.h>

typedef enum
{
  GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V,
  GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V,
  GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V,
  GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V,
  GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V,
  GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V
} 
adsGain_t;

class Adafruit_ADS1015
{
protected:
  // Instance-specific properties
  uint8_t m_i2cAddress;
  uint8_t m_conversionDelay;
  uint8_t m_bitShift;
  adsGain_t m_gain;

public:
  Adafruit_ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
  void begin(void);
  uint16_t readADC_SingleEnded(uint8_t channel);
  int16_t readADC_Differential_0_1(void);
  int16_t readADC_Differential_2_3(void);
  void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
  int16_t getLastConversionResults();
  void setGain(adsGain_t gain);
  adsGain_t getGain(void);

private:
};

// Derive from ADS1105 & override construction to set properties
class Adafruit_ADS1115 : 
public Adafruit_ADS1015
{   
};
// from Adafruit-4-channel-adc-breakouts/programming

Adafruit_ADS1015 ads1015;

void setup(void)
{
  Serial.begin(9600);

  ads1015.begin();
}
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads1015.readADC_SingleEnded(0);
  adc1 = ads1015.readADC_SingleEnded(1);
  adc2 = ads1015.readADC_SingleEnded(2);
  adc3 = ads1015.readADC_SingleEnded(3);
  Serial.print("AIN0: "); 
  Serial.println(adc0);
  Serial.print("AIN1: "); 
  Serial.println(adc1);
  Serial.print("AIN2: "); 
  Serial.println(adc2);
  Serial.print("AIN3: "); 
  Serial.println(adc3);
  Serial.println(" ");
  delay(1000);

  //end of ADS1015

  void loop(void)
    lcd.begin (16, 2);

  int chgaReading = analogRead(adc0);
  float chgaVolts = chgaReading * 1.0 / 27.0 ;

  lcd.setCursor(0, 0);
  lcd.print ("+ A");
  lcd.setCursor (1, 0);
  lcd.print(chgaVolts);

  int bavoltReading = analogRead(adc2);
  float bavoltVolts = bavoltReading * 1.0 / 56.0 ;

  lcd.setCursor (9, 0);
  lcd.print (" V");
  lcd.setCursor (10, 0);
  lcd.print (bavoltVolts);
}

```

which gives me just one error : unexpected unqualified-id before ‘{’… (situated just above “int16\_t adc0, adc1. adc2, adc3;”)

which I just cannot resolve. Whatever I try here simply adds to the problem. I realise that there is something BEFORE this point causing the hassle but I can’t find it.

(I also realise now that I should have attacked the problem in a different way, and have therefore wiped out other [momentarily unnecessary] stuff to simplify the sketch. Once THIS one is running I will test it and if that flies I’ll add the other stuff in small steps, checking regularly while climbing the ladder.)

Thanks for your patience and help…

---

<div class="post-metadata">

**Author:** ![Valen](https://avatars.discourse-cdn.com/v4/letter/v/9dc877/32.png) [@Valen](https://community.sparkfun.com/u/Valen)\
**Post date:** [March 15, 2014, 1:34pm UTC](https://community.sparkfun.com/t/coding-problem/28416/12 "2014-03-15T13:34:30Z")

</div>

> which gives me just one error : unexpected unqualified-id before ‘{’… (situated just above “int16\_t adc0, adc1. adc2, adc3;”)…

Again as the very first message: an isolated code block, without a function header to precede it telling the compiler how to interpret it. void setup ends right after ads1015.begin(); on line 127. What do you think starts after that with the { bracket?

And I’ll spoil you with the next error you will get. At line 148 there is the function header of loop **inside it’s own** codeblock. You can’t put void loop() anywhere you want to, it has to come before the { bracket that starts the code-block containing it’s functions. Guess where you need to put void loop to make it work. 😉

[EDIT]Also it is: void loop()

, not: void loop(void)

Reference: [http://arduino.cc/en/Tutorial/BareMinimum](http://arduino.cc/en/Tutorial/BareMinimum)

[http://arduino.cc/en/Reference/Braces#.UyRWaz5quvQ](http://arduino.cc/en/Reference/Braces#.UyRWaz5quvQ)

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 15, 2014, 2:42pm UTC](https://community.sparkfun.com/t/coding-problem/28416/13 "2014-03-15T14:42:37Z")

</div>

How about we look at the code Adafruit published for a tutorial.

```auto
#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1015 ads1015;

void setup(void)
{
  Serial.begin(9600);
  Serial.println("Hello!");
  
  Serial.println("Getting single-ended readings from AIN0..3");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV)");
  ads1015.begin();
}

void loop(void)
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads1015.readADC_SingleEnded(0);
  adc1 = ads1015.readADC_SingleEnded(1);
  adc2 = ads1015.readADC_SingleEnded(2);
  adc3 = ads1015.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.print("AIN1: "); Serial.println(adc1);
  Serial.print("AIN2: "); Serial.println(adc2);
  Serial.print("AIN3: "); Serial.println(adc3);
  Serial.println(" ");
  
  delay(1000);
}

```

Notice how simple it is because it “includes” the library code for the ADC rather than copying it into the sketch. Why don’t you start w/the above and add your LCD code to it ? Then we can add what’s needed to adjust the gain. But remember to install the ADC library (code Adafruit has already written for this ADC) into the Arduino’s file system as directed here. But use the manual installation instructions and be sure to change the subfolders name (after unzipping) from Adafruit\_ADS1x15-master to Adafruit\_ADS1015. Move that subfolder and it’s contents to where your added libraries are.

[http://arduino.cc/en/Guide/Libraries#.UyRgrIUVBrA](http://arduino.cc/en/Guide/Libraries#.UyRgrIUVBrA)

The library code is here.

[https://github.com/adafruit/Adafruit\_ADS1X15](https://github.com/adafruit/Adafruit_ADS1X15)

There’s a “download zip” button at the lower right part of the page.

Then startup the Arduino IDE, copy and paste the tutorial code into the new empty code window and see that it “verifies” for you as it just did for me.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 15, 2014, 4:02pm UTC](https://community.sparkfun.com/t/coding-problem/28416/14 "2014-03-15T16:02:47Z")

</div>

```auto
 #if ARDUINO >= 100
 #include "Arduino.h"
 #else
 #include "WProgram.h"
 #endif

```

Why is the above in your program ? It’s not in the tutorial code. Do you need it ? KISS.

```auto
/* I2C ADDRESS/BITS */
#define ADS1015_ADDRESS (0x48) // 1001 000 (ADDR = GND)

/* CONVERSION DELAY (in mS) */
#define ADS1015_CONVERSIONDELAY (1)

/* POINTER REGISTER */
#define ADS1015_REG_POINTER_MASK (0x03)
#define ADS1015_REG_POINTER_CONVERT (0x00)
#define ADS1015_REG_POINTER_CONFIG (0x01)
#define ADS1015_REG_POINTER_LOWTHRESH (0x02)
#define ADS1015_REG_POINTER_HITHRESH (0x03)

/* CONFIG REGISTER */
#define ADS1015_REG_CONFIG_OS_MASK (0x8000)
#define ADS1015_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
#define ADS1015_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
#define ADS1015_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion

#define ADS1015_REG_CONFIG_MUX_MASK (0x7000)

#define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
#define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
#define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
#define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3

#define ADS1015_REG_CONFIG_PGA_MASK (0x0E00)
#define ADS1015_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
#define ADS1015_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
#define ADS1015_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
#define ADS1015_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
#define ADS1015_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
#define ADS1015_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16

#define ADS1015_REG_CONFIG_MODE_MASK (0x0100)
#define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
#define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)

#define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
#define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
#define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
#define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
#define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second

#define ADS1015_REG_CONFIG_CMODE_MASK (0x0010)
#define ADS1015_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
#define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator

#define ADS1015_REG_CONFIG_CPOL_MASK (0x0008)
#define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
#define ADS1015_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active

#define ADS1015_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
#define ADS1015_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
#define ADS1015_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator

#define ADS1015_REG_CONFIG_CQUE_MASK (0x0003)
#define ADS1015_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
#define ADS1015_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
#define ADS1015_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
#define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)

```

Same comments as above. Admittedly you may need a few of the defines to set the gain per a later tutorial.

```auto
#include <LiquidCrystal.h>

```

Didn’t I already see this at the top of your sketch, where it belongs ? Yes I did, once is enough.

```auto
typedef enum
{
  GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V,
  GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V,
  GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V,
  GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V,
  GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V,
  GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V
}
adsGain_t;

class Adafruit_ADS1015
{
protected:
  // Instance-specific properties
  uint8_t m_i2cAddress;
  uint8_t m_conversionDelay;
  uint8_t m_bitShift;
  adsGain_t m_gain;

public:
  Adafruit_ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
  void begin(void);
  uint16_t readADC_SingleEnded(uint8_t channel);
  int16_t readADC_Differential_0_1(void);
  int16_t readADC_Differential_2_3(void);
  void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
  int16_t getLastConversionResults();
  void setGain(adsGain_t gain);
  adsGain_t getGain(void);

private:
};

// Derive from ADS1105 & override construction to set properties
class Adafruit_ADS1115 :
public Adafruit_ADS1015
{   
};

```

What’s all this ? It’s not in the tutorial code because by telling the compiler to “include” the ADC library you don’t have/want to copy it’s code into yours. Again KISS.

```auto
void setup(void)
{
  Serial.begin(9600);

  ads1015.begin();
}

```

Your setup() function is missing at least one thing that I found in your loop() function.

```auto
lcd.begin (16, 2);

```

```auto
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads1015.readADC_SingleEnded(0);
  adc1 = ads1015.readADC_SingleEnded(1);
  adc2 = ads1015.readADC_SingleEnded(2);
  adc3 = ads1015.readADC_SingleEnded(3);
  Serial.print("AIN0: ");
  Serial.println(adc0);
  Serial.print("AIN1: ");
  Serial.println(adc1);
  Serial.print("AIN2: ");
  Serial.println(adc2);
  Serial.print("AIN3: ");
  Serial.println(adc3);
  Serial.println(" ");
  delay(1000);

```

The above block looks familiar but it’s lost, neither in setup() nor in loop() and has either an extra { or a missing }. Again I would urge you to read the links I posted re: the fundamental pieces of a sketch.

```auto
    int chgaReading = analogRead(adc0);
      float chgaVolts = chgaReading * 1.0 / 27.0 ;

      lcd.setCursor(0, 0);
      lcd.print ("+ A");
      lcd.setCursor (1, 0);
      lcd.print(chgaVolts);

      int bavoltReading = analogRead(adc2);
      float bavoltVolts = bavoltReading * 1.0 / 56.0 ;

      lcd.setCursor (9, 0);
      lcd.print (" V");
      lcd.setCursor (10, 0);
      lcd.print (bavoltVolts);
    }

```

And so we get to “your code”, which looks almost fine at 1’st glance. So what’s needed is to intelligently meld this w/the tutorial code. You’re using only 2 channels, the tutorial shows 4 being read. And on that note … what’s this do ?

````auto
int chgaReading = analogRead(adc0);
```Isn't the analogRead() function telling the Arduino to use it's internal ADC to read pin "adc0" (whatever pin that may be) ? Didn't you want to use the external ADC ? How did that external ADC read happen in the tutorial ?

````

adc0 = ads1015.readADC\_SingleEnded(0);

```auto
What's stored in the variable adc0 ?
```

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 15, 2014, 4:19pm UTC](https://community.sparkfun.com/t/coding-problem/28416/15 "2014-03-15T16:19:51Z")

</div>

If you had an understanding of how to use the LiquidCrystal library then note the similarities btw these groups of statements.

```auto
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

lcd.begin(16, 2);

lcd.print("+ A");
lcd.setCursor(2, 0);
lcd.print(chgaC);

```

===============================================

```auto
#include <Adafruit_ADS1015.h>

Adafruit_ADS1015 ads1015;

ads1015.begin();

adc0 = ads1015.readADC_SingleEnded(0);
adc1 = ads1015.readADC_SingleEnded(1);
adc2 = ads1015.readADC_SingleEnded(2);
adc3 = ads1015.readADC_SingleEnded(3);

```

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 17, 2014, 9:29pm UTC](https://community.sparkfun.com/t/coding-problem/28416/16 "2014-03-17T21:29:22Z")

</div>

If you’re still lost and have really given it a try, here’s something close to the final answer. I say close as I don’t know what you have for an external voltage divider for each voltage, and thus don’t know what to use for the ADC gain. I could guess from the comments but I’ve got to leave something for you to do. :mrgreen:

```auto
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <LiquidCrystal.h>

Adafruit_ADS1015 ads1015;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(void)
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println("Hello!");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV)");
  ads1015.begin();
}

void loop(void)
{
  int chgaReading = ads1015.readADC_SingleEnded(0);
  float chgaVolts = chgaReading * 1.0 / 27.0 ;

  lcd.setCursor(0, 0);
  lcd.print ("+ A");
  lcd.setCursor (1, 0);
  lcd.print(chgaVolts);

  int bavoltReading = ads1015.readADC_SingleEnded(2);
  float bavoltVolts = bavoltReading * 1.0 / 56.0 ;

  lcd.setCursor (9, 0);
  lcd.print (" V");
  lcd.setCursor (10, 0);
  lcd.print (bavoltVolts);

  delay(1000);
}

```

I forsee one problem with the way you’ve got the display written (I copied your code). Think about what might happen if you send 48.125 to the LCD followed by 47.8 in the next loop for the same voltage display. What will show up on the LCD ?

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 18, 2014, 4:45am UTC](https://community.sparkfun.com/t/coding-problem/28416/17 "2014-03-18T04:45:04Z")

</div>

Yes, I am still lost.

I started from scratch and copied the (simplified) Adafruit sketch, wiped away the “Hello” stuff and added the basic minimum of my LCD stuff just to get this thing to fly. And yes, I have all 3 “.h” libraries.

```auto
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <LiquidCrystal.h>

Adafruit_ADS1015 ads1015;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(void)
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV)");
  ads1015.begin();
}

void loop(void)
{
  int chgaReading = ads1015.readADC_SingleEnded(0);
  float chgaVolts = chgaReading * 1.0 / 27.0 ;

  lcd.setCursor(0, 0);
  lcd.print ("+ A");
  lcd.setCursor (1, 0);
  lcd.print(chgaVolts);

  int bavoltReading = ads1015.readADC_SingleEnded(2);
  float bavoltVolts = bavoltReading * 1.0 / 56.0 ;

  lcd.setCursor (9, 0);
  lcd.print (" V");
  lcd.setCursor (10, 0);
  lcd.print (bavoltVolts);

  delay(1000);
}

```

Now on my IDE I get error : ‘class Adafruit\_ADS1015’ has no member named ‘begin’ with “ads1015.begin();” highlighted, copied directly from Adafruit, followed by a few more errors. (I see that they also use “ads.begin()” which gives the same error!)

I am not concerned about voltages for now : all I want is to see the UNO, ADS and LCD running, together. Only then will I worry about details I’m familiar with - I’ve been in analog industrial electronics long enough. It’s the micro’s, sketches and C++'s that I can’t quite get through my fingers…

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [March 18, 2014, 5:58am UTC](https://community.sparkfun.com/t/coding-problem/28416/18 "2014-03-18T05:58:47Z")

</div>

If the code I posted just above doesn’t compile then you’re missing a library. Post a screenshot of your installed ADC library.

See if your Arduino IDE lists the installed ADC library as shown below.

(click on to open)

---

<div class="post-metadata">

**Author:** ![Newbie10](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Newbie10](https://community.sparkfun.com/u/Newbie10)\
**Post date:** [March 18, 2014, 2:16pm UTC](https://community.sparkfun.com/t/coding-problem/28416/19 "2014-03-18T14:16:36Z")

</div>

Yup!

You were right. My library (correctly) uploads the “h” file and shows :

#include \<Adafruit\_ADS1015.h\>

as one would expect to be correct but doesn’t include the .cpp, etc, which is clearly the Humpty-Dumpty.

On re-reading the arduino/libraries/download/whatever I see that the full shopping basket is necessary and not just the ads1015.h on its own.

Silly of me, yes, but perhaps not clear enough in the soup for us newbies/dummies.

Also had a spot of trauma as the total file could not be saved to Library directly as per Arduino’s download library-thing - the red block gave me a long story saying that the file name should not contain numbers, fleas and windscreenwipers. I needed to do the alternative and create a file (which I named Adafruit\_ads1015) , move all the necessary files in and THEN pitched it into the cauldron as instructed before it worked.

What’s interesting is that my (new) (correct) library file is now at bottom of the list (_Sketch / Import Library_) in the “Contributed” section and will not upload to a sketch automatically or list in the usual list of libraries - it keeps sucking up the ADS1015 number WITHOUT the shopping basket.

Is this perhaps because I have the existing (naughty) library called “ADS1015” in the main section/list of libraries and that there’s conflict?

PS. Don’t find any way to edit a previous entries/replies in the forum. Is this normal? Or is there a way one can change a previous entry which I don’t know about? minor issue…

---

<div class="post-metadata">

**Author:** ![Valen](https://avatars.discourse-cdn.com/v4/letter/v/9dc877/32.png) [@Valen](https://community.sparkfun.com/u/Valen)\
**Post date:** [March 18, 2014, 2:23pm UTC](https://community.sparkfun.com/t/coding-problem/28416/20 "2014-03-18T14:23:34Z")

</div>

> Newbie10:  
> …
> 
> PS. Don’t find any way to edit a previous entries/replies in the forum. Is this normal? Or is there a way one can change a previous entry which I don’t know about? minor issue…

Odd, can’t see an edit button either on my posts. Some Sparkfunner must have consumed too much on St. Patrick’s Day.

[edit]But on this one I do. Very odd.

[Next page](https://community.sparkfun.com/t/coding-problem/28416.md?page=2)
