int switch1_in=12;
int switch1=0;
int relay1_out=13;
int Vgnd_in=A0;
int Vbatt_in=A1;
int V00_in=A2;
int V01_in=A3;
int delaytime=1000;
float Rxx1=1000.;
float Rxx2=4700.;
float Rxx3=25;
float Vgnd;
float Vbatt0;
float Vbatt;
float Vbattgnd;
float V000;
float V00;
float V010;
float V01;
float Ibattgnd;
float I00gnd;
float I01gnd;
float I01;
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
// put your setup code here, to run once:
pinMode(switch1_in, INPUT_PULLUP);
pinMode(relay1_out, OUTPUT);
pinMode(Vgnd_in, INPUT);
pinMode(V00_in, INPUT);
pinMode(V01_in, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
// read switch position and actuate relay
switch1=digitalRead(switch1_in);
digitalWrite(relay1_out,switch1);
// read GND voltage
Vgnd=analogRead(Vgnd_in)*(5./1023.);
// Vgnd=0.;
// read battery voltage
Vbatt0=analogRead(Vbatt_in)*(5./1023.);
// Vbatt0=1.579;
Ibattgnd=(Vbatt0-Vgnd)/Rxx1;
Vbatt=-Vgnd+Vbatt0+(Ibattgnd*Rxx2);
// read 00 voltage
V000=analogRead(V00_in)*(5./1023.);
// V000=Vbatt0*switch1;
I00gnd=(V000-Vgnd)/Rxx1;
V00=-Vgnd+V000+(I00gnd*Rxx2);
// read 01 voltage and current
V010=analogRead(V01_in)*(5./1023.);
// V010=1.263*switch1;
I01gnd=(V010-Vgnd)/Rxx1;
V01=-Vgnd+V010+(I01gnd*Rxx2);
I01=(V00-V01)/Rxx3*1000.;
Serial.print("Switch: ");
if (switch1==1) {
Serial.print("ON ");
}
if (switch1==0) {
Serial.print("OFF");
}
Serial.print(" Vgnd: ");
Serial.print(Vgnd);
Serial.print(" V");
Serial.print(" V000: ");
Serial.print(V000);
Serial.print(" V");
Serial.print(" V00: ");
Serial.print(V00);
Serial.print(" V");
Serial.print(" V010: ");
Serial.print(V010);
Serial.print(" V");
Serial.print(" V01: ");
Serial.print(V01);
Serial.print(" V");
Serial.print(" I01: ");
Serial.print(I01);
Serial.println(" mA");
lcd.setCursor(0, 0);
lcd.print("Switch:");
lcd.setCursor(8, 0);
if (switch1==1) {
lcd.print("ON ");
}
if (switch1==0) {
lcd.print("OFF");
}
lcd.setCursor(0, 1);
lcd.print("V00:");
lcd.setCursor(8, 1);
lcd.print(V00);
lcd.setCursor(14, 1);
lcd.print("V");
lcd.setCursor(0, 2);
lcd.print("V01:");
lcd.setCursor(8, 2);
lcd.print(V01);
lcd.setCursor(14, 2);
lcd.print("V");
lcd.setCursor(0, 3);
lcd.print("I01:");
lcd.setCursor(8, 3);
lcd.print(I01);
lcd.setCursor(14, 3);
lcd.print("mA");
delay(delaytime);
}