Unable to define Analog input (With correct board selected)

I am having some trouble defining an anlog pin in a project.

I am using a 10k pot to simulate PH from a probe so I can work through testing outputs. I have everything working ok with the PH robe, Temp probe and LCD in one sketch. I also have the 10k pot input and simulated PH figure working fine in another sketch.

I am unable setup an anolog pin in the setch that has the PH probe and LCD code and libraries included. I definately have the correct board selected. Code pasted below. Board is a Mega 2560 r3 using IDE version 1.8.12

If I comment out the reference A0 the code will compile. The code for that analog input compiles fine in a sketch on its on. Am I missing something simple?

Appreciate your help.

[code]


// PH Sim & Test code
int sensorPin = A0;
int sensorValue = 0;
int sensorPH = 1;
float decPH = 1.1;
float phLow = 6.5;
float phHi = 8.5;

//----------------------------------------------------------



// LCD Code

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif



U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R2, /* clock=*/ 21, /* data=*/ 20, /* CS=*/ 53, /* reset=*/ 8);

// End LCD Code



// TEMP SENSOR CODE
#include <OneWire.h>

int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

OneWire ds(DS18S20_Pin);  // on digital pin 2
//TEMP SENSOR CODE END


#include "DFRobot_PH.h"
#include <EEPROM.h>

#define PH_PIN A1
float voltage, phValue, temperature = 25;
DFRobot_PH ph;

void setup()
{
  Serial.begin(115200);
  ph.begin();
  u8g2.begin(); //Start LCD Screen
}

void loop()
{
  static unsigned long timepoint = millis();
  if (millis() - timepoint > 1000U) {            //time interval: 1s
    timepoint = millis();
    temperature = readTemperature();         // readtemperature sensor to execute temperature compensation
    voltage = analogRead(PH_PIN) / 1024.0 * 5000; // read the voltage
    phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
    Serial.print("temperature:");
    Serial.print(temperature, 1);
    Serial.print("^C  pH:");
    Serial.println(phValue, 2);
  }
  ph.calibration(voltage, temperature);          // calibration

  // LCD COde
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_glasstown_nbp_tf);
  u8g2.drawStr(0, 10, "Caloundra PH Treatment Tank");
  u8g2.drawStr(0, 25, "R PH:");
  u8g2.setCursor(28, 25);
  u8g2.print(phValue, 2);
  u8g2.drawStr(60, 25, "Sim PH:");
  u8g2.setCursor(100, 25);
  u8g2.print(decPH);
  u8g2.drawStr(0, 40, "Temp:");
  u8g2.setCursor(38, 40);
  u8g2.print(temperature, 1);
  u8g2.drawStr(0, 55, "Status:");
  u8g2.drawStr(38, 55, "Ciculating");

  u8g2.sendBuffer();          // transfer internal memory to the display
  //delay(1000);  //Commented out delay
  //End LCD Code--------------------------------------------------------------------------


  // Simulated PH code
  sensorPH = map(sensorValue, 0, 1023, 5, 18);
  decPH = sensorPH / 10;
  sensorValue = analogRead(sensorPin);

  // End of Simulated PH Code------------------------------------------------------------
}




float readTemperature()
{

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    //no more sensors on chain, reset search
    ds.reset_search();
    return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
    Serial.println("CRC is not valid!");
    return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
    Serial.print("Device is not recognized");
    return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;

  return TemperatureSum;

}

[/code]

Error message included below


```

PH___Temp_LCD:4:17: error: 'A0' was not declared in this scope

 int sensorPin = A0;

                 ^~

C:\Users\mrobe\OneDrive\Documents\Arduino\PH Meter\PH___Temp_LCD\PH___Temp_LCD.ino:4:17: note: suggested alternative: 'PA0'

 int sensorPin = A0;

                 ^~

                 PA0

exit status 1
'A0' was not declared in this scope


```

Remove this line:

#include <Arduino.h>

You don't need it and it is messing up the preprocessor

Also it is customary to put all of your includes at the top of the file, then defines (unless you need to define or undef before an include), constants, then global variables. It makes the code easier to read.

1 Like

Compiled and ran perfectly.

Thank you for your help and your feedback.

I will have tidy up and get things in a better sequence.

Thanks again.

Mick

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.