/*
  ACS712 DC Current Demonstration
  acs712-dc-demo.ino
  Read current using ACS712 Hall Effect sensor
  
  DroneBot Workshop 2021
  https://dronebotworkshop.com
  Edited by HotRF March 2024
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Variables for Measured Voltage and Calculated Current
double Vout = 0;
double Current = 0;

double vSource1=0;
double ampSource1=0;
double vSource2=0;
double ampSource2=0;
double vSource3=0;
double ampSource3=0;
double vBat=0;
double ampBat=0;

// Constants for Scale Factor
// Use one that matches your version of ACS712

//const double scale_factor = 0.185; // 5A
const double scale_factor = 0.1; // 20A
//const double scale_factor = 0.066; // 30A

// Constants for A/D converter resolution
// Arduino has 10-bit ADC, so 1024 possible values
// Reference voltage is 5V if not using AREF external reference
// Zero point is half of Reference Voltage

const double vRef = 5.00;
const double resConvert = 1024;
double resADC = vRef/resConvert;
double zeroPoint = vRef/2;


void setup(){ 
  //Serial.begin(9600);
  tft.begin();
  tft.setRotation(1); 
  tft.setCursor(0,0);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.print("Multi source charger ");
  tft.setTextSize(2);
  tft.print("V.1.2");

  tft.setCursor(0, 30);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("Source1 V:");

  tft.setCursor(0, 60);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("Source2 V:");

  tft.setCursor(0, 90);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("Source3 V:");

  tft.setCursor(0, 120);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("Battery V:");

  tft.setCursor(0, 150);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("Battery temp:");

  tft.setCursor(0, 180);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.println("On is Source :");
}

void loop(){
  
  // Vout is read 1000 Times for precision
  for(int i = 0; i < 1000; i++) {
    Vout = (Vout + (resADC * analogRead(A0))); 
    vSource1 = (vSource1 + (resADC * analogRead(A0))); 
    vSource2 = (vSource2 + (resADC * analogRead(A1)));
    vSource3 = (vSource3 + (resADC * analogRead(A2)));
    vBat = (vBat + (resADC * analogRead(A3)));


   // delay(1);
  }
  // Get Vout in mv
  Vout = Vout /1000;
  vSource1 = vSource1/1000;
   vSource2 = vSource2/1000;
    vSource3 = vSource3/1000;
    vBat = vBat/1000;
  // Convert Vout into Current using Scale Factor
  Current = (Vout - zeroPoint)/ scale_factor; 
  ampSource1 = (vSource1- zeroPoint)/ scale_factor;                  
  ampSource2 = (vSource2- zeroPoint)/ scale_factor;
  ampSource3 = (vSource3- zeroPoint)/ scale_factor;
  ampBat = (vBat- zeroPoint)/ scale_factor;

  tft.setCursor(0, 30); 
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.print("Source1:");
  tft.print(vSource1);
  tft.print(" V ");
  
  //tft.print(" amp:");
  tft.print(ampSource1);
  tft.print(" A");



  tft.setCursor(0, 60);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.print("Source2:");
  tft.print(vSource2);
  tft.print(" V");
  tft.setCursor(0, 90);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.print("Source3:");
  tft.print(vSource3);
  tft.print(" V");
  tft.setCursor(0, 120);
  tft.setTextColor(WHITE, BLUE);
  tft.setTextSize(2);
  tft.print("Battery:");
  tft.print(vBat);
  tft.print(" V ");  
   tft.print(ampSource2);
  tft.print(" A");            

// Print Vout and Current to two Current = ");  
  //Serial.print("Vout = ");           
  //Serial.print(Vout,2); 
  //Serial.print(" Volts");                            
  //Serial.print("\t Current = ");                  
  //Serial.print(Current,2);
  //Serial.println(" Amps");                             

 //delay(1000); 
 
}