#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <stdlib.h>
int voltagePin = A3;
int currentPin = A0;
int pwmCurrentPin = 10;
int pwmVoltagePin = 9;
int potPin = A1;
int key1Pin = 8;
int key2Pin = 12;
int key3Pin = 11;
int prevk1=1;
int prevk2=1;
int prevk3=1;
int currk1=1;
int currk2=1;
int currk3=1;
float voltageRef = 0;
float currentRef = 0;
float readVoltage()
{
float total=0;
for(int n=0;n<100;n++)
{
float temp = (float) analogRead(voltagePin);
temp = 1.1 * temp/1024 * 30;
total = total + temp;
delay(1);
}
return total/100;
}
float readCurrent()
{
float total=0;
for(int n=0;n<100;n++)
{
float temp = (float) analogRead(currentPin);
temp = temp/1024 * 5;
temp = (temp-2.51)250.185;
total = total + temp;
delay(1);
}
return total/100;
}
float readPot()
{
float temp = (float) analogRead(potPin);
temp = temp/1024;
return temp;
}
void showCurrentSet(float value)
{
char txt[6];
dtostrf(value,4,1,txt);
lcd.setCursor(11,0);
lcd.print(txt);
}
void showVoltageSet(float value)
{
char txt[6];
dtostrf(value,5,1,txt);
lcd.setCursor(4,0);
lcd.print(txt);
}
void showVoltageAct(float value)
{
char txt[6];
dtostrf(value,5,1,txt);
lcd.setCursor(4,1);
lcd.print(txt);
}
void showCurrentAct(float value)
{
char txt[6];
dtostrf(value,4,1,txt);
lcd.setCursor(11,1);
lcd.print(txt);
}
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("ARDX VarPwrS 30V10A+");
lcd.setCursor(6,1);
lcd.print("Ver.001A");
lcd.setCursor(2,2);
lcd.print("SET:00.00V 0.00A");
lcd.setCursor(2,3);
lcd.print("ACT:00.00V 0.00A");
pinMode(key1Pin, INPUT_PULLUP);
pinMode(key2Pin, INPUT_PULLUP);
pinMode(key3Pin, INPUT_PULLUP);
pinMode(pwmVoltagePin, OUTPUT);
analogWrite(pwmVoltagePin, 0);
pinMode(pwmCurrentPin, OUTPUT);
currentRef = 0.5;
analogWrite(pwmCurrentPin,128 + (currentRef*12.7));
}
void loop()
{
int tempk1 = digitalRead(key1Pin);
int tempk2 = digitalRead(key2Pin);
int tempk3 = digitalRead(key3Pin);
int count = 0;
while (count < 10)
{
currk1 = digitalRead(key1Pin);
currk2 = digitalRead(key2Pin);
currk3 = digitalRead(key3Pin);
if(tempk1 = currk1)
{
tempk1 = currk1;
count = 0;
}
if(tempk2 = currk2)
{
tempk2 = currk2;
count = 0;
}
if(tempk3 = currk3)
{
tempk3 = currk3;
count = 0;
}
count++;
}
if((currk1==0)&&(prevk1==1))
{
currentRef = currentRef + 0.5;
if(currentRef>3) currentRef=3;
showCurrentSet(currentRef);
analogWrite(pwmCurrentPin,128 + (currentRef*11));
}
if((currk2==0)&&(prevk2==1))
{
currentRef = currentRef - 0.5;
if(currentRef<0.5) currentRef=0.5;
showCurrentSet(currentRef);
analogWrite(pwmCurrentPin,128 + (currentRef*12.7));
}
if((currk3==0)&&(prevk3==1))
{
}
prevk1=currk1;
prevk2=currk2;
prevk3=currk3;
showVoltageAct(readVoltage());
showCurrentAct(readCurrent());
showCurrentSet(currentRef);
showVoltageSet(readPot()30);
analogWrite(pwmVoltagePin, 0.91readPot()*255);
}