Batteries charger using PWM and LCD Display

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");
}

Hello
Post your sketch well formated and in code tags, please.

this may help you recognize short and long button presses

// recognizing short and long button presses

#undef MyHW
#ifdef MyHW
byte pinsBut [] = { A1, A2, A3 };
#else
byte pinsBut [] = { 6, 13 };
#endif

#define N_BUT   sizeof(pinsBut)

enum { Off = HIGH, On = LOW };

byte          butState  [N_BUT];
byte          butReport [N_BUT];

unsigned long butMsec  [N_BUT];
unsigned long ButLong  = 2000;

unsigned long msec;

enum { NoPress, Press, PressLong };

// -----------------------------------------------------------------------------
int
chkButton (
    int  butID)
{
    byte but = digitalRead (pinsBut [butID]);

    if (butState [butID] != but)  {
        butState [butID] = but;
        delay (10);     // debounce

        if (LOW == but)  {
            butMsec   [butID] = msec;
            butReport [butID] = false;
        }
        else if (! butReport [butID])
            return Press;
    }

    else if (LOW == but)  {
        if (! butReport [butID])
            if ( (msec - butMsec [butID]) > ButLong)  {
                butReport [butID] = true;
                return PressLong;
            }
    }

    return NoPress;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    msec = millis ();

    switch (chkButton (0))  {
    case Press:
        Serial.println ("  button-0 pressed");
        break;

    case PressLong:
        Serial.println ("  button-0 pressed-long");
        break;
    }

    switch (chkButton (1))  {
    case Press:
        Serial.println ("  button-1 pressed");
        break;

    case PressLong:
        Serial.println ("  button-1 pressed-long");
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }
}

Thank you so much, i solved the problem, i used a slide switch, to switch from menu 1( voltage and current), to menu 2.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.