[go: up one dir, main page]

0% found this document useful (0 votes)
51 views4 pages

Lester Test

This code is sampling voltage and current values over time from sensors. It is displaying the date, time, and calculated apparent power on an LCD. The date, time, voltage, current, and power values are saved periodically to a CSV file on an SD card for logging and analysis purposes. Key functions include sampling the sensor values, calculating effective voltage, current and power, preparing the data as strings, displaying to the LCD, and saving to the SD card file.

Uploaded by

Topher Pante
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Lester Test

This code is sampling voltage and current values over time from sensors. It is displaying the date, time, and calculated apparent power on an LCD. The date, time, voltage, current, and power values are saved periodically to a CSV file on an SD card for logging and analysis purposes. Key functions include sampling the sensor values, calculating effective voltage, current and power, preparing the data as strings, displaying to the LCD, and saving to the SD card file.

Uploaded by

Topher Pante
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <RtcDateTime.

h>
#include <RtcDS3231.h>
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);

File myFile;
RtcDS3231<TwoWire> Rtc(Wire);

char dateBuff[25];
char timeBuff[25];

float voltage;
float effectiveVoltage;
float current;
float effectiveCurrent;
float instApparentPower;

int voltageValues[100];
int currentValues[100];
int voltageSensorValue;
int currentSensorValue;
int maxInstVoltage;
int maxInstCurrent;

String dateString;
String timeString;
String voltString;
String ampsString;
String powString;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

Wire.begin();
if (!SD.begin(53)) {
Serial.println("SD CARD ERROR!");
while (true);
}
if (!SD.exists("PowMonit.csv")) {
File myFile = SD.open("PowMonit.csv", FILE_WRITE);
if (myFile) {
myFile.print("Date");
myFile.print(",");
myFile.print("Time");
myFile.print(",");
myFile.print("Voltage(V)");
myFile.print(",");
myFile.print("Current(A)");
myFile.print(",");
myFile.print("Apparent Power(VA)");
myFile.println();
myFile.close();
Serial.println("Excel Spreadsheet Ready...");
}
} else Serial.println("File Already Exists. Ready to Accept Data.");

lcd.begin(20, 4);
lcd.setBacklight(255);
Rtc.Begin();

lcd.setCursor(1, 1);
lcd.print("DATE:");
// lcd.setCursor(18, 1);
// lcd.print("V");
lcd.setCursor(1, 2);
lcd.print("TIME:");
// lcd.setCursor(18, 2);
// lcd.print("A");
lcd.setCursor(1, 3);
lcd.print("A POWER:");
lcd.setCursor(18, 3);
lcd.print("VA");
}

void loop() {
// put your main code here, to run repeatedly:
RtcDateTime now = Rtc.GetDateTime();
printDate(now);
dateString = (char*)dateBuff;
timeString = (char*)timeBuff;
dateString.trim();
timeString.trim();

sampling();
prepareData();
displayLCD();

delay(1000);//Time interval for saving

saveData() ;
}

void sampling() {
maxInstVoltage = 0;
maxInstCurrent = 0;

for (byte i = 0; i < 100; i++ ) {


voltageSensorValue = 220;
currentSensorValue = analogRead(A1);

voltageValues[i] = voltageSensorValue;
currentValues[i] = currentSensorValue;

delayMicroseconds(166);
}
Serial.println(voltageSensorValue);
Serial.println(currentSensorValue);
for (byte i = 0; i < 100; i++ ) {
if (voltageValues[i] > maxInstVoltage) {
maxInstVoltage = voltageValues[i];
}
if (currentValues[i] > maxInstCurrent) {
maxInstCurrent = currentValues[i];
}
}
Serial.println(maxInstVoltage);
Serial.println(maxInstCurrent);

/*if (maxInstVoltage > 512) {


voltage = float((maxInstVoltage - 512)); //replace values if necessary
effectiveVoltage = voltage * (230 / 103);
} else effectiveVoltage = 0;
*/
if (maxInstCurrent > 515) {
current = float((maxInstCurrent - 515)); //replace values if necessary
effectiveCurrent = current * (4 / 75);
} else effectiveCurrent = 0;

effectiveVoltage = voltageSensorValue;
instApparentPower = effectiveVoltage * effectiveCurrent;

void prepareData() {
voltString = String(effectiveVoltage);
ampsString = String(effectiveCurrent);
powString = String(instApparentPower);
voltString.trim();
ampsString.trim();
powString.trim();

void displayLCD() {

lcd.setCursor(7, 1);
lcd.print(dateString);

lcd.setCursor(7, 2);
lcd.print(timeString);

lcd.setCursor(10, 3);
lcd.print(powString);

void saveData() {
Serial.println(dateString);
Serial.println(timeString);
Serial.println(voltString);
Serial.println(ampsString);
Serial.println(powString);

File myFile = SD.open("PowMonit.csv", FILE_WRITE);


if (myFile) {
myFile.print(dateString);
myFile.print(",");
myFile.print(timeString);
myFile.print(",");
myFile.print(voltString);
myFile.print(",");
myFile.print(ampsString);
myFile.print(",");
myFile.print(powString);
myFile.println();
myFile.close();

Serial.println("Data successfully saved");


} else {
Serial.println("Error Writing on SD Card!");
}
}

void printDate(const RtcDateTime & dt) {


snprintf(dateBuff, sizeof(dateBuff), "%02d/%02d/%04d", dt.Month(), dt.Day(),
dt.Year());
snprintf(timeBuff, sizeof(timeBuff), "%02d:%02d:%02d", dt.Hour(), dt.Minute(),
dt.Second());
}

You might also like