#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// set the LCD number of columns and rows
int lcdColumns = 20;
int lcdRows = 4;
// in millivolts
long mpp_pv=52000;
long mpp_co=8300;
long PV=52000;
long BV=12600;
long Boost_voltage=13600;
long float_voltage=13200;
long boost_disc_voltage=13000;
//in milliseconds
long boost_time=600000;
// in milliamps
long CO=0;
//in milliwatts
long PO=0;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
const int freq = 50000;
const int ledChannel = 0;
const int resolution = 8;
const int ledPin = 16; // 16 corresponds to GPIO16
int dutyCycle=0;
//const int ledPin2 = 17; // 17 corresponds to GPIO17
//const int ledPin3 = 5; // 5 corresponds to GPIO5
//int dutyCycle=0;
void setup() {
// put your setup code here, to run once:
Wire.begin();
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
Serial.begin(115200);
Serial.println("Hello, ESP32!");
lcd.setCursor(0, 0);
lcd.println("Hello, ESP32!");
lcd.clear();
xTaskCreatePinnedToCore(task1, "Task 1", 4096, NULL, 2, NULL, 0);
xTaskCreatePinnedToCore(task2, "Task 2", 4096, NULL, 2, NULL, 1);
// configure LED PWM functionalitites
//ledcAttachPin(ledPin2, ledChannel);
//ledcAttachPin(ledPin3, ledChannel);
}
void task1(void*parameters){//send data to lcd
for(;;){
//*
lcd.setCursor(0, 0);
// print message
lcd.print("PV:");
lcd.setCursor(9, 0);
lcd.print("BV:");
//delay(100);
// clears the display to print new message
//lcd.clear();
// set cursor to first column, second row
lcd.setCursor(0,1);
lcd.print("PO:");
lcd.setCursor(0,2);
lcd.print("Status:");
lcd.setCursor(0,3);
lcd.print(dutyCycle);
//delay(100);
//lcd.clear();
//*/
//vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void task2(void*parameters){
ledcAttachPin(ledPin, ledChannel);
ledcSetup(ledChannel, freq, resolution);
for(;;){
PV=map(analogRead(35),0,4095,0,5.0);
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
//Serial.println(PV);
//vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void bulk_mmpt_charge(){
do{
if(BV<PV){
dutyCycle--;
}
else{
dutyCycle++;
}
}while(BV<PV);
}
void boost_charge(){
int delay_timeperrun=boost_time/255;
do{// repeat until total time is achieved
for(int x=delay_timeperrun; x=0;x--)
{
if(BV<Boost_voltage){
dutyCycle++;
}
else{
dutyCycle--;
}
vTaskDelay(1/portTICK_PERIOD_MS);
}
}while(dutyCycle>0);
}
void float_charge()
{
do{
if(BV<float_voltage){
dutyCycle++;
}
else{
dutyCycle--;
}
}while(BV>boost_disc_voltage);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}