Hi all
I'm having problems compiling my sketch.
Error: 'lcd' does not name a type
I install the library using Sketch|Include Library|Add .ZIP Library, yes I resterted the IDE after install. IDE ver 1.6.9
I was using a 1602 LCD keypad shield and the project was working, then changed to a 2004 I2C LCD
Hardware:
UNO
Datalogger Shield
Sensor shield
2004 I2C LCD
My code:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27, 20, 4);
float VoltsPerAmp = 0.040;
float ACSoffsetAmpIn = 2.500;
const int AmpSensorVoltage = 5.0;
int Res = 1023;
int chipSelect = 10;
File dataFile;
boolean bHaveFile = true;
boolean bTerminate = false;
const int VoltInputPin = A1;
float VoltIn = 0;
float VoltInValue = 0;
float VoltsIn = 0;
const float R1 = 30000;
const float R2 = 7500;
int AmpInputPin = A2;
float AmpIn = 0;
float AmpInValue = 0;
float AmpsIn = 0;
double AmpInVoltage = 0;
int AmpOutPin = A3;
float AmpOut = 0;
float AmpOutValue = 0;
float AmpsOut = 0;
double AmpOutVoltage = 0;
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2);
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1125300L / result;
return result;
}
void setup() {
Serial.begin(9600);
analogReference(DEFAULT);
Wire.begin();
RTC.begin();
lcd.init();
lcd.backlight();
Serial.println();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// output, even if you don't use it:
pinMode(chipSelect, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
bTerminate = true;
return;
}
Serial.println("card initialized.");
if (SD.exists("datalog.txt")) {
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
Serial.print("Writing to datalog.txt...");
} else {
Serial.println("error opening datalog.txt");
bHaveFile = false;
}
} else {
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
Serial.print("Writing to datalog.txt...");
dataFile.print("Date, Time, Battery Volts, Amps IN, Amps OUT");
} else {
Serial.println("error opening datalog.txt");
bHaveFile = false;
}
}
pinMode(VoltInputPin, INPUT); // Set the mode of the pin used to measure battery voltage
pinMode(AmpInputPin, INPUT); // Set the mode of the pin used to measure wind turbine charge current
pinMode(AmpOutPin, INPUT); // Set the mode of the pin used to measure the current drawn for the inverter
}
void loop() {
if (bTerminate) {
return;
}
DateTime now = RTC.now();
unsigned int ADCValue;
double Voltage;
double Vcc;
Vcc = readVcc() / 1000.0;
ADCValue = analogRead(VoltInputPin);
Voltage = (ADCValue / Res) * Vcc;
VoltInValue = analogRead(VoltInputPin);
VoltIn = (VoltInValue * Vcc) / Res;
VoltsIn = VoltIn / (R2 / (R1 + R2));
AmpInValue = analogRead(AmpInputPin); //Raw data reading
AmpInVoltage = (AmpInValue / Res) * 5;
ACSoffsetAmpIn = (AmpSensorVoltage / 2);
//AmpsIn = (AmpInVoltage - ACSoffsetAmpIn) / VoltsPerAmp;
AmpsIn = (AmpInValue - 540) * Vcc / Res / 0.04 - 0.04;
AmpOutValue = analogRead(AmpOutPin); // read the Amps input
AmpOutVoltage = (AmpOutValue / Res) * 5;
ACSoffsetAmpIn = (AmpSensorVoltage / 2);
//AmpsIn = (AmpInVoltage - ACSoffsetAmpIn) / VoltsPerAmp;
AmpsOut = (AmpOutValue - 540) * Vcc / Res / 0.04 - 0.04;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volts: ");
if (VoltsIn < 10) {
lcd.print(0);
}
lcd.print(VoltsIn);
lcd.setCursor(0, 1);
lcd.print("Amps IN: ");
lcd.print(AmpsIn);
lcd.setCursor(0, 2);
lcd.print("Amps OUT: ");
lcd.print(Ampsout);
lcd.setCursor(0, 3);
if (now.day() < 10) {
lcd.print("0");
}
lcd.print(now.day(), DEC);
lcd.print('-');
if (now.month() < 10) {
lcd.print("0");
}
lcd.print(now.month(), DEC);
lcd.print('-');
lcd.print(now.year(), DEC);
lcd.print(" ");
if (now.hour() < 10) {
lcd.print("0");
}
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
if (bHaveFile) {
dataFile.println();
if (now.day() < 10) {
dataFile.print("0");
}
dataFile.print(now.day(), DEC);
dataFile.print("/");
if (now.month() < 10) {
dataFile.print("0");
}
dataFile.print(now.month(), DEC);
dataFile.print("/");
dataFile.print(now.year(), DEC);
dataFile.print(", ");
if (now.hour() < 10) {
dataFile.print("0");
}
dataFile.print(now.hour(), DEC);
dataFile.print(":");
if (now.minute() < 10) {
dataFile.print("0");
}
dataFile.print(now.minute(), DEC);
dataFile.print(", ");
dataFile.print(VoltsIn);
dataFile.print(", ");
dataFile.print(AmpsIn);
dataFile.print(", ");
dataFile.print(AmpsOut);
dataFile.flush();
}
delay(500);
}
Many thanks in advance for any guidance.
Regards
Fred