Need help, display blink when press button or HC SR04

I'm a new user of arduino. Need help. Because my 7 segment display is blinking because of button and sensor.

#include <HCSR04.h>
#include <SevSeg.h>
#include <Stepper.h> //INCLUSÃO DE BIBLIOTECA

  const int stepsPerRevolution = 64; //NÚMERO DE PASSOS POR VOLTA
  const int botao1 = 7;
  int buttonState1;

  int posicao;
  int leitura;


  SevSeg sevseg; 
  Stepper myStepper(stepsPerRevolution, 8,10,9,11); //INICIALIZA O MOTOR UTILIZANDO OS PINOS DIGITAIS 8, 9, 10, 11
  HCSR04 HCIn (A3,A4);

void setup(){
Serial.begin(9600);
myStepper.setSpeed(300); //VELOCIDADE DO MOTOR

byte numDigits = 2;
byte digitPins[] = {A2, A1};
byte segmentPins[]={12,13,5,4,3,2,1};
bool resistorsOnSegments = true; // 
byte hardwareConfig = COMMON_ANODE; 
bool updateWithDelays = true; 
bool leadingZeros = false; 
bool disableDecPoint = true;
sevseg.setBrightness(100);
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);

pinMode(botao1, INPUT);

posicao=0;
leitura = 11;

}

void loop(){
loopmotor();
buttonState1 = digitalRead(botao1);

delay(10);
loopmotor2();

}
void loopmotor(){
if (buttonState1==HIGH && HCIn.dist()>1000 && posicao==0 && leitura!=0){
leitura--;
posicao=1;
for(int i = 0; i < 10; i++){
myStepper.step(stepsPerRevolution);//GIRA O MOTOR NO SENTIDO ANTI-HORÁRIO

       }

}

else if (HCIn.dist()<15 && posicao==1){
delay(2000);
posicao=0;
for(int i = 0; i < 10; i++){//PARA "i" IGUAL A 0, ENQUANTO "i" MENOR QUE 50 INCREMENTA "i"
myStepper.step(-stepsPerRevolution); //GIRA O MOTOR NO SENTIDO HORÁRIO

      }

}

}
void loopmotor2(){

sevseg.setNumber(leitura);
sevseg.refreshDisplay(); // Must run repeatedly

}

I think that should read "frequently"

Please remember to use code tags when posting code.
Can you tell us what your project has to do with the COVID emergency?

Hello
Well, I think the distributed usage of delay() function will inhibit the desired realtime funtionality of the sketch.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

@amartins80, it's unclear how this relates to Covid19 and hence your topic was moved.

Doesn't everything nowadays relate to Covid19? :grin:


Multiplexing a seven segment display in code is really just a toy. Note that you need to be using 1k resistors on the segments.