/* This is the LCD display*/
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Pinout
int sensorVin = 14;
int sensorVout = 0;
int sensorIin = 16;
int buck_output = 20;
int dump_output = 19;
int sensorIout = 0;// select the input pin for the potentiometer
int ledPin = 18; // select the pin for the LED
// Constants
float Vin_max = 30;
float Vin_lower = 12;
float Iin_max = 14;
float Iin_upper = 3;
float Iin_lower = 0;
float calibrateVinMax = 703;
float calibrateVinLower = 267;
float calibrateVin = calibrateVinMax - calibrateVinLower;
float calibrateIinUpper =165 ;
float calibrateIinLower = 0;
float calibrateIin = calibrateIinUpper- calibrateIinLower;
int sampleSize = 50;
float volts_per_div= (Vin_max-Vin_lower)/calibrateVin;
float amps_per_div= (Iin_upper - Iin_lower)/calibrateIin;
int dt = 250;
int analog_scale = 256;
// Variables
float Iin_sum = 0;
float Vin = 0;
float Vout = 0;
float Iin = 0;
float Iout = 0;
float Pin = 0;
float Pout = 0;
float Ein = 0;
float Eout = 0;
float Pin_previous = 0;
float Vout_last = 0;
float Vin_last = 0;
float Iin_last[50];
float duty_cycle = 0.5;
int analog_duty_cycle = duty_cycle * analog_scale;
float E_prev = 0;
// variable to store the value coming from the sensor
void setup() {
// set up the LCD's number of columns and rows:
//lcd.begin(20, 4);
// Print a message to the LCD.
//lcd.print("hello, world!");
//pinMode(ledPin, OUTPUT);
pinMode(buck_output, OUTPUT);
pinMode(dump_output, OUTPUT);
pinMode(sensorVin, INPUT);
pinMode(sensorIin, INPUT);
analogWriteFrequency(buck_output, 20000);
Serial.begin(9600);
analogWrite(buck_output,analog_duty_cycle);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
// read the value from the sensor:
read_data();
print_data();
regulate();
delay(dt);
}
void read_data() {
Iin_sum=0;
Vin = (float) analogRead(sensorVin)* volts_per_div +1;
Iin_last[0] = (float) analogRead(sensorIin);
for (int i = sampleSize-1; i--; i >= 0) {
if (i > 0) {
Iin_last[i] = Iin_last[i-1];
}
Iin_sum += Iin_last[i];
Serial.println(Iin_last[i]);
}
Iin = Iin_sum / sampleSize * amps_per_div;
Pin = 0;
Pin = Vin * Iin;
//Pout=Vout*Iout;
E_prev = Ein;
Ein += Pin * (float) dt/ 1000;
float i = (float) dt/ 1000;
//Eout+=Pout*dt;
}
void print_data() {
//lcd.setCursor(0, 1);
Serial.println("Vin: " + (String)Vin);
//lcd.setCursor(6, 1);
Serial.print("Vout: " + (String)Vout);
//lcd.setCursor(0, 2);
Serial.print("Iin: " + (String)Iin);
//lcd.setCursor(6, 2);
Serial.print("IinSum: " + (String)Iin_sum);
//lcd.setCursor(0, 3);
Serial.print("Pout: " + (String)Pout);
//lcd.setCursor(6, 3);
Serial.print("Pin: " + (String)Pin);
//lcd.setCursor(0, 4);
Serial.print("Eout: " + (String)Pout);
//lcd.setCursor(6, 4);
Serial.print("Ein: " + (String)Ein);
Serial.println("Duty: " + (String)duty_cycle);
}
void regulate() {
if (((Vin > Vin_max) && (Iin < Iin_max)) || ((Pin > Pin_previous && Vin < Vin_last) || (Pin < Pin_previous && Vin > Vin_last))) {
if (duty_cycle < 1) {
duty_cycle += 0.01;
}
analog_duty_cycle = duty_cycle * analog_scale;
analogWrite(buck_output, analog_duty_cycle);
}
else if (((Vin < Vin_max) && (Iin > Iin_max)) || ( (Pin > Pin_previous && Vin > Vin_last) || (Pin < Pin_previous && Vin < Vin_last))) {
if (duty_cycle > 0) {
duty_cycle -= 0.01;
}
analog_duty_cycle = duty_cycle * analog_scale;
analogWrite(buck_output, analog_duty_cycle);
}
analogWrite(buck_output, analog_duty_cycle);
Vin_last = Vin;
}
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Pinout
int sensorVin = 14;
int sensorVout = 0;
int sensorIin = 16;
int buck_output = 20;
int dump_output = 19;
int sensorIout = 0;// select the input pin for the potentiometer
int ledPin = 18; // select the pin for the LED
// Constants
float Vin_max = 30;
float Vin_lower = 12;
float Iin_max = 14;
float Iin_upper = 3;
float Iin_lower = 0;
float calibrateVinMax = 703;
float calibrateVinLower = 267;
float calibrateVin = calibrateVinMax - calibrateVinLower;
float calibrateIinUpper =165 ;
float calibrateIinLower = 0;
float calibrateIin = calibrateIinUpper- calibrateIinLower;
int sampleSize = 50;
float volts_per_div= (Vin_max-Vin_lower)/calibrateVin;
float amps_per_div= (Iin_upper - Iin_lower)/calibrateIin;
int dt = 250;
int analog_scale = 256;
// Variables
float Iin_sum = 0;
float Vin = 0;
float Vout = 0;
float Iin = 0;
float Iout = 0;
float Pin = 0;
float Pout = 0;
float Ein = 0;
float Eout = 0;
float Pin_previous = 0;
float Vout_last = 0;
float Vin_last = 0;
float Iin_last[50];
float duty_cycle = 0.5;
int analog_duty_cycle = duty_cycle * analog_scale;
float E_prev = 0;
// variable to store the value coming from the sensor
void setup() {
// set up the LCD's number of columns and rows:
//lcd.begin(20, 4);
// Print a message to the LCD.
//lcd.print("hello, world!");
//pinMode(ledPin, OUTPUT);
pinMode(buck_output, OUTPUT);
pinMode(dump_output, OUTPUT);
pinMode(sensorVin, INPUT);
pinMode(sensorIin, INPUT);
analogWriteFrequency(buck_output, 20000);
Serial.begin(9600);
analogWrite(buck_output,analog_duty_cycle);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
// read the value from the sensor:
read_data();
print_data();
regulate();
delay(dt);
}
void read_data() {
Iin_sum=0;
Vin = (float) analogRead(sensorVin)* volts_per_div +1;
Iin_last[0] = (float) analogRead(sensorIin);
for (int i = sampleSize-1; i--; i >= 0) {
if (i > 0) {
Iin_last[i] = Iin_last[i-1];
}
Iin_sum += Iin_last[i];
Serial.println(Iin_last[i]);
}
Iin = Iin_sum / sampleSize * amps_per_div;
Pin = 0;
Pin = Vin * Iin;
//Pout=Vout*Iout;
E_prev = Ein;
Ein += Pin * (float) dt/ 1000;
float i = (float) dt/ 1000;
//Eout+=Pout*dt;
}
void print_data() {
//lcd.setCursor(0, 1);
Serial.println("Vin: " + (String)Vin);
//lcd.setCursor(6, 1);
Serial.print("Vout: " + (String)Vout);
//lcd.setCursor(0, 2);
Serial.print("Iin: " + (String)Iin);
//lcd.setCursor(6, 2);
Serial.print("IinSum: " + (String)Iin_sum);
//lcd.setCursor(0, 3);
Serial.print("Pout: " + (String)Pout);
//lcd.setCursor(6, 3);
Serial.print("Pin: " + (String)Pin);
//lcd.setCursor(0, 4);
Serial.print("Eout: " + (String)Pout);
//lcd.setCursor(6, 4);
Serial.print("Ein: " + (String)Ein);
Serial.println("Duty: " + (String)duty_cycle);
}
void regulate() {
if (((Vin > Vin_max) && (Iin < Iin_max)) || ((Pin > Pin_previous && Vin < Vin_last) || (Pin < Pin_previous && Vin > Vin_last))) {
if (duty_cycle < 1) {
duty_cycle += 0.01;
}
analog_duty_cycle = duty_cycle * analog_scale;
analogWrite(buck_output, analog_duty_cycle);
}
else if (((Vin < Vin_max) && (Iin > Iin_max)) || ( (Pin > Pin_previous && Vin > Vin_last) || (Pin < Pin_previous && Vin < Vin_last))) {
if (duty_cycle > 0) {
duty_cycle -= 0.01;
}
analog_duty_cycle = duty_cycle * analog_scale;
analogWrite(buck_output, analog_duty_cycle);
}
analogWrite(buck_output, analog_duty_cycle);
Vin_last = Vin;
}