Hello, i created a charger, using pwm signal, everything works, but i want to show the values of voltage and current on an 16x2 LCD. I have created a small menu, controlled by 2 button, the first one increment the PWM, and the second one decrement PWM.
What i want to do, i want the voltage and the current to be shown everytime, and when i press the button for more than 2-3 seconds, the PWM control menu to appear and you can control the increment and decrement with the button. When the buttons are inactive, the voltage and current menu should appear. How can i do that?
Here is my code:
#include <LiquidCrystal.h>
//DISPLAY
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//MEASURE VOLTAGE AND CURRENT
float vcc=9.0;
float voltage=0.00;
float current=0;
//BUTTONS
const int BUTTON1 = 13;
int BUTTONstate1 = 0;
const int BUTTON2 = 6;
int BUTTONstate2 = 0;
//pwm init
int pwmOut = 9; //pwm pin 9 digital
int pwm = 25;
int duty = 22;
int currentShow=40;
float capacity=0.1;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(pwmOut, OUTPUT);
}
void loop() {
//buttons control pwm
analogWrite(pwmOut, pwm); // about zero volts
delay(200);
//increment pwm
BUTTONstate1 = digitalRead(BUTTON1);
if (BUTTONstate1 == 1){
pwm=pwm+22;
currentShow=currentShow+40;
capacity=capacity+0.1;
}
//decrement pwm
BUTTONstate2 = digitalRead(BUTTON2);
if (BUTTONstate2 == 1){
pwm=pwm-22;
currentShow=currentShow-40;
capacity=capacity-0.1;
}
//reset pwm
if(pwm>135){
pwm=25;
currentShow=40;
capacity=0.1;
}
//dont let current to take value smaller than 40mA
if(pwm<40){
pwm=25;
currentShow=40;
capacity=0.1;
}
//measuring batteries param.
int valueVoltage = analogRead(A0);
int valueCurrent = analogRead(A1);
voltage = 9-(valueVoltage* (5.0 / 1024.0))*4;
current = valueCurrent * 0.22 ;
//current select pwm
voltageAndCurrentDisplay();
}
//current modify function
void currentSelect(){
lcd.setCursor(0,0);
lcd.print("CURRENT SELECT:");
lcd.setCursor(0,1);
lcd.print("I= ");
lcd.print(currentShow);lcd.print(" mA");
lcd.print(" C=");
lcd.print(capacity);
}
//display voltage and current
void voltageAndCurrentDisplay(){
lcd.setCursor(0,0);
lcd.print("Voltage=");
lcd.print(voltage);lcd.print(" V");
lcd.setCursor(0, 1);
lcd.print("Current=");lcd.print(current);
lcd.print("mA");
}