Programming error

hi everyone.. presently i am working on a project and i am facing some errors in my program. if anyone help me to sort out the problem it would be highly appreciated. my code is as below :

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 9, 10, 11, 12,13);

SoftwareSerial s(5,6);
//part of current sensor//
const int currentvalue = A1;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
int sensorValue = 0;
int ACSoffset = 2500;
float volt = 0;
float Amps = 0;
float cutOffLimit = 0;
//part of voltage sensor//
const int voltagevalue=A2;
int voltage=0;
float r1=47000.0;
float r2=33000.0;
//part of power//
const float watt=0;
const float Kwh=0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
//Name Printing on LCD//
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IoT based solar");
lcd.setCursor(0, 1);
lcd.print("power monitoring");
delay(4000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Project By");
lcd.setCursor(0, 1);
lcd.print("Amjad (Leader)");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("2. xray");
lcd.setCursor(0, 1);
lcd.print("3. arhm");
delay(3000);
lcd.clear();
}  
void loop()
{
//part of current measurment//

sensorValue = analogRead(currentvalue);
volt = ((sensorValue / 1024.0) * 5000); // Gets you mV
Amps = ((volt - ACSoffset) / mVperAmp)*1000;
if((Amps) > cutOffLimit )
{
   lcd.setCursor(2, 0);
   lcd.print(Amps,0);
   Serial.println("Amps");
   Serial.print(Amps,2); // print the current with 2 decimal places
}
else
{
  Amps=0;
  lcd.setCursor(2, 0);
  lcd.print(Amps,0);
  Serial.println("No Current");
}
//part of voltage measurement//
voltagevalue = analogRead(A2);
voltage=(voltagevalue*(5.0/1024)*((r1+r2)/r2));
//for power conversion//
watt=(voltage*(Amps/1000));
Kwh=Kwh+(watt/3600)*1000;
//Part of serial communication//
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["voltage"] = voltage;
root["Amps"] = Amps;
root["Kwh"] = Kwh;
root["watt"] = watt;
if(s.available()>0)
{ root.printTo(s); }
// data printing on lcd//
 lcd.setCursor(0, 0);
 lcd.print("I=");
 lcd.setCursor(6, 0);
 lcd.print("mA");
lcd.setCursor(0, 1);
lcd.print("V=");
lcd.setCursor(2, 1);
lcd.print(voltage,2);
lcd.setCursor(6, 1);
lcd.print("V");
lcd.setCursor(10, 0);
lcd.print("W=");
lcd.setCursor(12, 0);
lcd.print(watt,0);
lcd.setCursor(15, 0);
lcd.print("P");
 lcd.setCursor(8, 1);
 lcd.print("mWh=");
 lcd.setCursor(12 , 1);
 lcd.print(Kwh,0);
 lcd.setCursor(15, 1);
 lcd.print("E");
delay(9000);
// data printing on serial monitor//
Serial.print("Amps = "); // shows the voltage measured
Serial.println(Amps,2); // the '2' after voltage allows you to display 2 digits after decimal point
Serial.print("Voltage =");
Serial.println(voltage);
Serial.println(watt,2);
Serial.print("watt = "); // shows the watt measured
Serial.print("Kwh = "); // shows the kwh measured
Serial.println(Kwh,2);
}

I guess it did not occur to you to share the error messages. And you did not feel like reading the forum guidelines either.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Also please read the Read this before posting a programming question ... post. It makes it easier on every one if we don't have to waste time finding out fundamental information.

Thanks for the code tags. How about the error messages?

What Arduino board are you using?

get the liquid crystal that has the back pack, maybe a dollar more. you only need two pins!

If you are using the newest version of the ArduinoJson library, this code will not work. You will need to fix the code or install an older version of the library. The older versions are available via the library manager.

If you are using an older version of the library (prior to version 6.0) the errors are as follows:

const int voltagevalue = A2;
  
   voltagevalue = analogRead(A2); // ****** can't change a constant

You can't expect analogRead() to work before setup completes. You cannot change the value of a constant.

const float watt = 0;
const float Kwh = 0;

   watt = (voltage * (Amps / 1000));  // ****** can't change a constant
   Kwh = Kwh + (watt / 3600) * 1000;  // ****** can't change a constant

Same here, you can't change a constant. Remove the const from the variable declarations.

groundFungus:
I guess it did not occur to you to share the error messages. And you did not feel like reading the forum guidelines either.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Also please read the Read this before posting a programming question ... post. It makes it easier on every one if we don't have to waste time finding out fundamental information.

firstly thanks a ton for your kind response. i am New to this forum and this was my first post. next time i will directly post the error messages. thanks for your guidance.

groundFungus:
Thanks for the code tags. How about the error messages?

What Arduino board are you using?

i am using Arduino Nano board .. below are the error messages:

codeForArduino:13:26: error: 'A1' was not declared in this scope

const int currentvalue = A1;

^

codeForArduino:24:24: error: 'A2' was not declared in this scope

const int voltagevalue=A2;

^
codeForArduino:79:28: error: 'A2' was not declared in this scope

voltagevalue = analogRead(A2);

^

codeForArduino:82:7: error: assignment of read-only variable 'watt'

watt=(voltage*(Amps/1000));

^

codeForArduino:83:5: error: assignment of read-only variable 'Kwh'

Kwh=Kwh+(watt/3600)*1000;

^

'A1' was not declared in this scope

groundFungus:
If you are using the newest version of the ArduinoJson library, this code will not work. You will need to fix the code or install an older version of the library. The older versions are available via the library manager.

If you are using an older version of the library (prior to version 6.0) the errors are as follows:

const int voltagevalue = A2;

voltagevalue = analogRead(A2); // ****** can't change a constant



You can't expect analogRead() to work before setup completes. You cannot change the value of a constant.



const float watt = 0;
const float Kwh = 0;

watt = (voltage * (Amps / 1000));  // ****** can't change a constant
  Kwh = Kwh + (watt / 3600) * 1000;  // ****** can't change a constant



Same here, you can't change a constant. Remove the const from the variable declarations.

thank you so much for your kind responses. yes i was using newest version but when i faced errors i downgrade the version and ArduinoJson library works on that version.
secondly i have removed the word (const) from the highlighted lines but still showing same errors. i further need your help in this regard . thank you

codeForArduino:13:26: error: 'A1' was not declared in this scope

The compiler is telling the truth. A1 is not declared any where in the code

What do you think that A1 represents or what will it hold ?

UKHeliBob:

codeForArduino:13:26: error: 'A1' was not declared in this scope

The compiler is telling the truth. A1 is not declared any where in the code

What do you think that A1 represents or what will it hold ?

thanks for your response. i have declared A1 is my code at line#7 (int currentvalue = A1;) but still showing error. will you please guide me more. thank you

int currentvalue = A1;

does not declare A1

It declares a int variable named currentvalue and tries to assign it the value of variable A1, but A1 has not been declared so it does not have a value so the compiler cannot do what you want so reports the error

UKHeliBob:

int currentvalue = A1;

does not declare A1

It declares a int variable named currentvalue and tries to assign it the value of variable A1, but A1 has not been declared so it does not have a value so the compiler cannot do what you want so reports the error

will you please guide me what changes i have to do in above provided code. i am working on this code for last 16 hours but still failed to overcome errors. thank you

amjo:
i am using Arduino Nano board .. below are the error messages:

codeForArduino:13:26: error: 'A1' was not declared in this scope

const int currentvalue = A1;

^

codeForArduino:24:24: error: 'A2' was not declared in this scope

const int voltagevalue=A2;

^
codeForArduino:79:28: error: 'A2' was not declared in this scope

voltagevalue = analogRead(A2);

^

codeForArduino:82:7: error: assignment of read-only variable 'watt'

watt=(voltage*(Amps/1000));

^

codeForArduino:83:5: error: assignment of read-only variable 'Kwh'

Kwh=Kwh+(watt/3600)*1000;

^

'A1' was not declared in this scope

Are you sure you have Nano selected?

We still haven't seen your code with the fixes so far suggested.

int currentvalue = A1;

What do you want this line of code to do ?

It looks like you are trying to read the value on analogue pin A1. If so then you should be using analogRead(A1);

Please post your full code

UKHeliBob:

int currentvalue = A1;

What do you want this line of code to do ?

It looks like you are trying to read the value on analogue pin A1. If so then you should be using analogRead(A1);

Please post your full code

yes i am connecting current sensor to pin A1 and voltage sensor to pin A2..sir here is the full code
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 9, 10, 11, 12, 13);

SoftwareSerial s(5, 6);

//part of current sensor//
int currentvalue = A1;

int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
int sensorValue = 0;
int ACSoffset = 2500;
float volt = 0;
float Amps = 0;
float cutOffLimit = 0;

//part of voltage sensor//

int voltagevalue = A2;
int voltage = 0;
float r1 = 47000.0;
float r2 = 33000.0;
//part of power//
float watt = 0;
float Kwh = 0;

void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
//Name Printing on LCD//
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IoT based solar");
lcd.setCursor(0, 1);
lcd.print("power monitoring");
delay(4000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Project By");
lcd.setCursor(0, 1);
lcd.print("AMJAD ALI");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("2. ism");
lcd.setCursor(0, 1);
lcd.print("3. AYYAZ");
delay(3000);
lcd.clear();
}
void loop()
{
//part of current measurment//
sensorValue = analogRead(currentvalue);
volt = ((sensorValue / 1024.0) * 5000); // Gets you mV
Amps = ((volt - ACSoffset) / mVperAmp) * 1000;
if ((Amps) > cutOffLimit )
{
lcd.setCursor(2, 0);
lcd.print(Amps, 0);
Serial.println("Amps");
Serial.print(Amps, 2); // print the current with 2 decimal places
}
else
{
Amps = 0;
lcd.setCursor(2, 0);
lcd.print(Amps, 0);
Serial.println("No Current");
}
//part of voltage measurement//
voltagevalue = analogRead(A2);
voltage = (voltagevalue * (5.0 / 1024) * ((r1 + r2) / r2));
//for power conversion//
watt = (voltage * (Amps / 1000));
Kwh = Kwh + (watt / 3600) * 1000;
//Part of serial communication//
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["voltage"] = voltage;
root["Amps"] = Amps;
root["Kwh"] = Kwh;
root["watt"] = watt;
if (s.available() > 0)
{
root.printTo(s);
}
// data printing on lcd//
lcd.setCursor(0, 0);
lcd.print("I=");
lcd.setCursor(6, 0);
lcd.print("mA");
lcd.setCursor(0, 1);
lcd.print("V=");
lcd.setCursor(2, 1);
lcd.print(voltage, 2);
lcd.setCursor(6, 1);
lcd.print("V");
lcd.setCursor(10, 0);
lcd.print("W=");
lcd.setCursor(12, 0);
lcd.print(watt, 0);
lcd.setCursor(15, 0);
lcd.print("P");
lcd.setCursor(8, 1);
lcd.print("mWh=");
lcd.setCursor(12 , 1);
lcd.print(Kwh, 0);
lcd.setCursor(15, 1);
lcd.print("E");
delay(9000);
// data printing on serial monitor//
Serial.print("Amps = "); // shows the voltage measured
Serial.println(Amps, 2); // the '2' after voltage allows you to display 2 digits after decimal point
Serial.print("Voltage =");
Serial.println(voltage);
Serial.println(watt, 2);
Serial.print("watt = "); // shows the watt measured
Serial.print("Kwh = "); // shows the kwh measured
Serial.println(Kwh, 2);
}

Please remember to use code tags.

Please post all the error messages, also in code tags.

A1 should be declared on a Nano, either you have the wrong board selected, or the arduino IDE is corrupted in some way.

Exactly which board do you have selected in the IDE ?

Exactly which Arduino board do you have plugged into the PC ?

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