Hello everyone, i have a little piece of code which i use for a solar charger, but it seems kinda long and...raw. Could it be shorter and cleaner ?
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x4B);
Adafruit_ADS1115 adsa(0x4A);
float V1 = 0.0;
float V2 = 0.0;
float V3 = 0.0;
float V4 = 0.0;
float V5 = 0.0;
float V6 = 0.0;
float en1 = 0.0;
float en2 = 0.0;
void setup(void)
{
pinMode(9, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
ads.begin();
adsa.begin();
}
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;
byte pulseWidth = 0;
void loop(void)
{
int16_t adc1;
adc1 = ads.readADC_SingleEnded(1);
V1 = (adc1 * 0.0001875) * 8;
int16_t adc2;
adc2 = ads.readADC_SingleEnded(2);
V2 = (adc2 * 0.0001875) * 8;
int16_t adc3;
adc3 = ads.readADC_SingleEnded(3);
V3 = (adc3 * 0.0001875) * 7;
int16_t adc4;
adc4 = adsa.readADC_SingleEnded(1);
V4 = (adc4 * 0.0001875) * 10;
int16_t adc5;
adc5 = adsa.readADC_SingleEnded(2);
V5 = (adc5 * 0.0001875) * 10;
int16_t adc6;
adc6 = adsa.readADC_SingleEnded(3);
V6 = (adc6 * 0.0001875) * 20;
currentMillis = millis();
elapsedMillis = (currentMillis - startMillis);
unsigned long SS = (elapsedMillis / 1000) % 60;
unsigned long MM = (elapsedMillis / (60000)) % 60;
unsigned long HH = (elapsedMillis / (3600000));
en1 = en1 += ((V1 * V4) + (V2 * V5)) * 1 / 36000;
en2 = en2 += (V3 * V6) * 1 / 36000;
lcd.setCursor(0, 0);
lcd.print(V1, 2);
lcd.setCursor(5, 0);
lcd.print("V");
lcd.setCursor(7, 0);
lcd.print(V2, 2);
lcd.setCursor(12, 0);
lcd.print("V");
lcd.setCursor(14, 0);
lcd.print(V3, 2);
lcd.setCursor(19, 0);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print(V4, 2);
lcd.setCursor(5, 1);
lcd.print("A");
lcd.setCursor(7, 1);
lcd.print(V5, 2);
lcd.setCursor(12, 1);
lcd.print("A");
lcd.setCursor(14, 1);
lcd.print(V6, 2);
lcd.setCursor(19, 1);
lcd.print("A");
lcd.setCursor(0, 2);
lcd.print(V1 * V4, 1);
lcd.setCursor(5, 2);
lcd.print("W");
lcd.setCursor(7, 2);
lcd.print(V2 * V5, 1);
lcd.setCursor(12, 2);
lcd.print("W");
lcd.setCursor(14, 2);
lcd.print(V3 * V6, 1);
lcd.setCursor(19, 2);
lcd.print("W");
lcd.setCursor(0, 3);
lcd.print(en1 , 1);
lcd.setCursor(7, 3);
lcd.print(en2 , 1);
lcd.setCursor(14, 3);
lcd.print(MM);
lcd.setCursor(17, 3);
lcd.print(":");
lcd.setCursor(19, 3);
lcd.print(" ");
lcd.setCursor(18, 3);
lcd.print(SS);
if (V3 < 28 )
{
if (pulseWidth != 255) pulseWidth++;
}
if (V3 > 28 )
{
if (pulseWidth = 0);
}
analogWrite(9, pulseWidth);
}