Blinking led from the variable number from the display

I'm new here and I want to make led blink as many numbers as written on my tm1637 display. thanks to everyone who writes to me. :slightly_smiling_face:

here is my code

#include <TM1637Display.h>


#define CLK 3
#define DIO 4

int ap1 = A0;
int ap2 = A1;


int buttonPushCounter = 0;  
int buttonState = 0;    
int buttonStatem = 0;
int lastButtonState = 0;    





TM1637Display display = TM1637Display(CLK, DIO);


const uint8_t allON[] = {0xff, 0xff, 0xff, 0xff};


const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};





void setup() {
pinMode(ap1,    INPUT);
pinMode(ap2,  INPUT);


  


}

void loop() {
  
  display.setBrightness(5);


 

 {
buttonState = digitalRead(ap1);

  
  if (buttonState != lastButtonState) 
    
    if (buttonState == HIGH) 
     
      buttonPushCounter++;
      display.showNumberDec(buttonPushCounter);
      delay(100);
    {
buttonStatem = digitalRead(ap2);
    } if  (buttonStatem != lastButtonState) {
    
    if (buttonStatem == HIGH) 
      
      buttonPushCounter--;
      display.showNumberDec(buttonPushCounter);
    }

 
 
}
 }

delete empty lines of code and format sketch with ctrl +t, and post again.


#include <TM1637Display.h>
#define CLK 3
#define DIO 4
int ap1 = A0;
int ap2 = A1;
int atr  =  A2;
int buttonPushCounter = 0;
int buttonState = 0;
int buttonStatem = 0;
int lastButtonState = 0;
TM1637Display display = TM1637Display(CLK, DIO);
const uint8_t allON[] = {0xff, 0xff, 0xff, 0xff};
const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};
void setup() {
  pinMode(ap1,    INPUT);
  pinMode(ap2,  INPUT);
}
void loop() {
  display.setBrightness(5);
  {
    buttonState = digitalRead(ap1);
    if (buttonState != lastButtonState)
      if (buttonState == HIGH)
        buttonPushCounter++;
    display.showNumberDec(buttonPushCounter);
    delay(100);
    {
      buttonStatem = digitalRead(ap2);
    } if  (buttonStatem != lastButtonState) {
      if (buttonStatem == HIGH)
        buttonPushCounter--;
      display.showNumberDec(buttonPushCounter);
    }
  }
}

Hello bgden11

Check and try this untested mods of your sketch:

#include <TM1637Display.h>
#define CLK 3
#define DIO 4
int ap1 = A0;
int ap2 = A1;
int buttonPushCounter = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int buttonStatem = 0;
TM1637Display display = TM1637Display(CLK, DIO);
const uint8_t allON[] = {0xff, 0xff, 0xff, 0xff};
const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};
void setup() {
  pinMode(ap1,    INPUT);
  pinMode(ap2,  INPUT);
  display.setBrightness(5);
}
void loop() {
  buttonState1 = digitalRead(ap1);
  if (buttonState1 != lastButtonState1) {
    lastButtonState1 = buttonState1;
    if (buttonState1 == HIGH) {
      buttonPushCounter++;
      display.showNumberDec(buttonPushCounter);
    }
    delay(20); // debounce button
  }
  buttonState2 = digitalRead(ap2);
  if (buttonState2 != lastButtonState2) {
    lastButtonState2 = buttonState2;
    if (buttonState2 == HIGH) {
      buttonPushCounter--;
      display.showNumberDec(buttonPushCounter);
    }
    delay(20); // debounce button
  }
}
1 Like

thanks for fast answer . I will check now

I think you didn't understand me. I want the led diode to blink as many times as the number shown on my display, that is, if the display says 100, I have to press some button to confirm and the crazy diode starts to blink 100 times! :slightly_smiling_face:

ok, on this way you can forget my mods

Have a nice day and enjoy coding in C++.

He did understand you. But then he read your code and realised there were so many mistakes that needed to be corrected before any additional feature was added.

I know, that's why I'm writing here if someone can correct me

You seem to have some { } randomly placed throughout your code that serve no purpose... other than to confuse. (2 sets).

So you have 2 buttons... 1 to count up, 1 to count down? Suggestion... call the variables associated with up - upButton, upState, UpPrevious... and have a similar set for down. Currently up & down seem to be sharing lastButtonState... and you never update lastButtonState.

You don't need to delay(100). Serves no purpose other than block your code.

OK.. so now for the blinking.

You need to decide how this is going to work. While blinking are the buttons ignored? How quickly should the LED blink? Is there a 3rd button that triggers (confirms) the blinking)

yes, I need to add another button for the confirmation. yes, the buttons are ignored while the LED is flashing and it doesn't matter how fast. for the unnecessary {}, I am new and I take some codes from the internet, I redo them and these {} remain there

Here's an example. The code for the new button is the same as the others. Once pressed it simply loops turning the LED on and off. It uses delay() for the timing, which is OK in this case, but if you want to do other things while the LED is flashing you need a different approach.

Untested.

#include <TM1637Display.h>

#define CLK 3
#define DIO 4

int upPin = A0;
int upState = 0;
int upPrevious = LOW;

int downPin = A1;
int downState = 0;
int downPrevious = LOW;

int confirmPin = A2;
int confirmState = 0;
int confirmPrevious = LOW;

int ledPin = 2;

int buttonCounter = 0;

TM1637Display display = TM1637Display(CLK, DIO);

const uint8_t allON[]  = {0xff, 0xff, 0xff, 0xff};
const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};

void setup()
{
  pinMode(upPin,      INPUT);
  pinMode(downPin,    INPUT);
  pinMode(confirmPin, INPUT);

  pinMode(ledPin,     OUTPUT);
    
  display.setBrightness(5);
}


void loop()
{
  // Check the "Up" button.
  upState = digitalRead(upPin);

  if (upState != upPrevious)
  {
    if (upState == HIGH)
      buttonCounter++;

    upPrevious = upState;  
  }
  

  // Check the "Down" button.
  downState = digitalRead(downPin);

  if  (downState != downPrevious)
  {
    if (downState == HIGH)
      buttonCounter--;

    downPrevious = downState;
  }


  // Display the counter on the screen.
  display.showNumberDec(buttonCounter);


  // Check the "Confirm" button.  If pressed then flash the LED.
  confirmState = digitalRead(confirmPin);

  if  (confirmState != confirmPrevious)
  {
    if (confirmState == HIGH)
    {
      for (uint8_t x = 0; x < buttonCounter; x++)
      {
        digitalWrite(ledPin, HIGH);
        delay(300);
        digitalWrite(ledPin, LOW);
        delay(300);
      }
    }
 
    confirmPrevious = confirmState;
  }


}
1 Like

thank you for the code. this is i needed ! :slightly_smiling_face:

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