Pushbutton Not Activating a Certain Function In My Code

Hi, so I'm making a 5V Solar Powerbank for a YouTube video, the solar and powerbank part works fine, it uses a tiny microcontroller - DFRobot Beetle board (Leonardo cutdown(ATmega32U4)) - Beetle Board - Compatible with Arduino Leonardo - ATmega32U4 - DFRobot to measure the Li-Ion battery's voltage, then display how charged it is on a Battery Capacity Indicator module - Battery Capacity Indicator - DFRobot. That part all works fine. What I'm trying to do is to be able to press a pushbutton once and get the display module going, then press it again to turn it off.
My issue in making this simple feature possible is coding for this, I'm having trouble figuring out what function name to call out to start and stop the batteryDisplay function from the module's TM1651 library.
Pasted below is my full code (including failed my attempts in coding for the feature), though my changes didn't bring the Battery Capacity Indicator module to turn off or on when I pushed the push button.

Full code:

#include "TM1651.h"
#define CLK 10 
#define DIO 9
#define cellPin A0
#define buttonPin 11
int val = 0;
int value = 0; 
int state = 0; 
int old_val = 0; 
TM1651 batteryDisplay(CLK, DIO);

void setup()
{
  Serial.begin(9600);  // Debugging only
  batteryDisplay.init();
  batteryDisplay.set(7);//0 ~ 7 mean to different brightness;
  batteryDisplay.frame(FRAME_ON);//light the frame of the battery display or FRAME_OFF to turn off the frame of the battery display

  pinMode(cellPin, INPUT);
}

void loop()
{
  value = digitalRead(buttonPin); //Reads the value of the button and stores
  if ((value == HIGH) && (old_val == LOW))
  {
    state = 1 - state;  //Check if state has changed
    // delay(10);
  }
  old_val = value; // Val is now old val so its stored

  switch (state)
  {
    case 1:
      _start(); //starting battery display animation
      break;
    case 0:
      _stop(); //stopping battery display animation
      break;
  }


  charging(); //initializing void function

  val = analogRead(cellPin);
  Serial.println(val);
  delay(1000);

}

void charging() //for displaying battery lvl through a charging animation (animation part is disabled here)
{
  if (val <= 550)
  {
    for (uint8_t level = 0; level < 1; level ++)
    {
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }
  if (val >= 600)
  {
    for (uint8_t level = 0; level < 2; level ++) /*the following lines of code see if battery's ADC analog value is greater than or equal to a certain value,
then makes the module display so many bars as a percentage accordingly.*/
    {                                                                     
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }
  if (val >= 650)
  {
    for (uint8_t level = 0; level < 3; level ++)
    {
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }
  if (val >= 700)
  {
    for (uint8_t level = 0; level < 4; level ++)
    {
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }
  if (val >= 750)
  {
    for (uint8_t level = 0; level < 5; level ++)
    {
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }
  if (val >= 800)
  {
    for (uint8_t level = 0; level < 6; level ++)
    {
      batteryDisplay.displayLevel(level);
      delayMicroseconds(5);
    }
  }



}

I usually get around such small things quicker than the problem I'm having now, but this time it really got me stuck, so that's why you're seeing my post on the forum.
If any of you could suggest a quick tip, code modification, or method to make this little feature work-in with my battery display, that would be great.

Any help would be much appreciated, thanks!

How is your switch connected to the IO, if the switch is open, how is the pin potential defined. (input pullup topic)

Please post a complete sketch illustrating the problem, using code tags when you do

My switch is actually in a module-form, containting 3 pins: Digital pin, positive pin, and negative pin. So I believe it's already pulled up with its own resistor, no code input_pullup needed.

Thanks for posting the code but did you miss this request ?

Have you tested it, or looked it up somewhere? Can you provide a link?

Sorry, I'm a beginner at coding, I've never heard of code tags before or how to use them. Can you please still help me despite not having those?

I'm afraid I can't provide you a link to a demo of this project, but I have tested the circuit to find out the hardware is okay, all that's wrong is the part in the code which makes the button activate and then deactivate the LED bar display module.
If you're referring to the push-button module, then here's the link: Gravity: LED Push Button for Arduino/micro:bit - DFRobot

Nobody is going to read that mess until it's fixed. Go to the top of the forum, there are permanent intro threads there to tell you how to format it correctly for the forum. It sounds scary, but it's actually easy. Like this:

some code...

Before you do, don't forget to auto format the code in the IDE, you can use ctrl-T or find it in the menu tabs.

Hi, @max_imagination
Welcome to the forum.

To add code please click this link;

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thanks for this, Tom! :smiley:

Hi,
Also before posting, do a CTRL-T in the IDE to Auto format your code.
I will indent at appropriate spots to make your code easier the read.

Tom... :smiley: :+1: :coffee: :australia:

1 Like