Arduino Voltmeter turn on display for x seconds

Hi
I’m having trouble with a voltmeter project. I currently have a voltmeter reading 5 volts on a seven segment display. I would like the display to turn on for 3 seconds when i press a button as well as the transistor that powers the circuit which is being read.
So far the code i have turns the display on when i press the button, so i have to hold it down.
I have read about millis function but i have no idea how to integrate it into the IF function. Delay function does not work as it turns off the display…

any help appreciated

int digit1 = 4; //PWM Display pin 12
int digit2 = 5; //PWM Display pin 9
int segA = 6; //Display pin 11
int segB = 7; //Display pin 7
int segC = 8; //Display pin 4
int segD = 9; //Display pin 2
int segE = 10; //Display pin 1
int segF = 11; //Display pin 10
int segG = 12; //Display pin 5
int transistor = 3;


const int DisplayButton = 2; 
int buttonState = 0; 
int potpin = A0; //potentiometer analog pin
int val; //value for analog input
int number;

void setup() {
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(transistor, OUTPUT);

}

void loop() {
  
  buttonState = digitalRead(DisplayButton);

  if (buttonState == HIGH) { 
    
  digitalWrite(transistor, HIGH);   
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 5);
  displayNumber(val);
  }
  
  else {
    delay(10);
    digitalWrite(transistor, LOW);
  }
  
}

void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS 5000

#define DIGIT_ON LOW
#define DIGIT_OFF HIGH

  long beginTime = millis();

  for(int digit = 2 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      break;
    case 2:
      digitalWrite(digit2, DIGIT_ON);
      break;
    }

    //Turn on the right segments for this digit
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    delayMicroseconds(DISPLAY_BRIGHTNESS); //Display this digit for a fraction of a second (between 1us and 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10);
//
// //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);

  }

  while( (millis() - beginTime) < 10) ; //Wait for 20ms to pass before we paint the display again
}

//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

// may be need invert
#define SEGMENT_ON HIGH
#define SEGMENT_OFF LOW

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}

Here is the “without delay” general idea:

long previousMillis = 0;    // last time of update   
long interval = 3000; // For how long you want your display to stay on          

void setup() {
// ..............leave it as it is................  
}

void loop()
{
 
  unsigned long currentMillis = millis(); // stores the time your project is running
  if(currentMillis - previousMillis > interval) { // when, time the project runs - last update > of your interval which is 3 secs, let the display turn on for 3 secs.. so put that part of code here
    previousMillis = currentMillis;  // stores the last time this part of code was executed

  }
}

Hope i helped,
please let us know of your progress :smiley: :smiley:

thanks for the help, this is my loop code. I can only get it to flash every 3 seconds if i hold the button down rather than flash for 3 seconds, what am i doing wrong?

void loop() {
  
  buttonState = digitalRead(DisplayButton);
 // digitalWrite(transistor, LOW); 
//  digitalWrite(buttonState, LOW);
  
  
  
  if (buttonState == HIGH) { 
    
    unsigned long currentMillis = millis(); // stores the time your project is running
    
 if(currentMillis - previousMillis > interval) { // when, time the project runs - last update > of your interval which is 3 secs, let the display turn on for 3 secs.. so put that part of code here
 
  digitalWrite(transistor, HIGH);   
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 5);
  displayNumber(val);
   
    previousMillis = currentMillis;  // stores the last time this part of code was executed
    
  }
  }
  
  
  else {
    delay(10);
    digitalWrite(transistor, LOW);
  }
  
}

m600:
what am I doing wrong?

Not showing your full code for one, and failing to use "Auto Format" to make it fully readable. :smiley:

codes all there in the original post, anyone else know why this might be flashing the number every 3 seconds instead of displaying for three seconds?