Code Before

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

#include <SPI.

h>

#include <SD.h>

#include <LiquidCrystal.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#include "ACS712.h"

double sensorValue1 = 0;

int crosscount = 0;

int climb_flag = 0;

int val[100];

int max_v = 0;

double VmaxD = 0;

double VeffD = 0;

double Veff = 0;

ACS712 sensor(ACS712_30A, A0);

const int chipSelect = 4;

void setup() {

int zero = sensor.calibrate();


Serial.begin(9600);

lcd.init();

lcd.backlight();

lcd.setCursor(0,0);

// lcd.setCursor(0, 0);

lcd.print("Arduino Based ");

lcd.setCursor(0, 1);

lcd.print("Voltage Current");

delay(500);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Voltage Current");

lcd.setCursor(3, 1);

lcd.print("Sensor");

delay(3000);

lcd.clear();

// Open serial communications and wait for port to open:

while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:

if (!SD.begin(chipSelect)) {

Serial.println("Card failed, or not present");

// don't do anything more:

return;

Serial.println("card initialized.");

void loop() {

float I = abs(sensor.getCurrentAC());

voltage_setup();

Serial.print("Voltage: ");

lcd.setCursor(10,1);

lcd.print("I:");

lcd.setCursor(12,1);

lcd.print(I);

// lcd.setCursor(14,0);

// lcd.print("A");

if(Veff>35)

{
Serial.println(Veff+35.75);

Serial.println(",");

float VeffLCD = Veff+35.75;

lcd.setCursor(0,1);

lcd.print("V: ");

lcd.setCursor(3,1);

lcd.print(VeffLCD);

else{

Serial.println("0.00");

Serial.println(",");

lcd.setCursor(0,1);

lcd.print("V: 0.00");

if (I<0.55)

Serial.println(String("I = ") + " 0.00");

lcd.setCursor(10,1);

lcd.print("I:");

lcd.setCursor(12,1);

lcd.print(I);

else

Serial.println(String("I = ") + abs(I) + " A");


lcd.setCursor(10,1);

lcd.print("I:");

lcd.setCursor(12,1);

lcd.print(I);

delay(100);

float AV = (Veff*I);

lcd.setCursor(0,0);

lcd.print("Power = ");

lcd.setCursor(7,0);

lcd.print(AV);

// Wait a second before the new measurement

delay(100);

// make a string for assembling the data to log:

String dataString = "";

// read three sensors and append to the string:

for (int analogPin = 0; analogPin < 3; analogPin++) {

int sensor = analogRead(analogPin);

dataString += String(sensor);

if (analogPin < 2) {

dataString += ",";

}
// open the file. note that only one file can be open at a time,

// so you have to close this one before opening another.

File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:

if (dataFile) {

dataFile.println(AV);

dataFile.close();

// print to the serial port too:

Serial.println(AV);

// if the file isn't open, pop up an error:

else {

Serial.println("error opening datalog.txt");

VmaxD = 0;

delay(100);

void voltage_setup()

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


sensorValue1 = analogRead(A1);

if (analogRead(A1) > 511) {

val[i] = sensorValue1;

else {

val[i] = 0;

delay(1);

max_v = 0;

for ( int i = 0; i < 100; i++ )

if ( val[i] > max_v )

max_v = val[i];

val[i] = 0;

if (max_v != 0) {

VmaxD = max_v;

VeffD = VmaxD / sqrt(2);

Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;

else {

Veff = 0;

}
}

You might also like