PWM motor driver 99% duty cycle for 2 seconds then variable from then on, how?

I have the code to drive 2 independent PWM channels using an ATMEGA2560 board. Each channel is controlled by a pot, analog read then analog write output. Using a 7-segment library two dual digit displays show the duty cycle from 10-99%. It works perfectly for my needs, except for the first 2 seconds after the board has booted up I require the two PWM outputs to be 99% duty cycle and the displays to show 99%, no matter what position the pots are set to, after 2 seconds the outputs and displays will revert to how the pots are set. Anybody any ideas?

[code]
#include <SevSeg.h>
#include "SevSeg.h"
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 11;  // PWM A output pin
const int pwmPinB = 12;  // PWM B output pin
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms

void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {9, 10};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {2, 3, 4, 5, 6, 7, 8};        // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
  pinMode(pwmPinA, OUTPUT); 
  pinMode(pwmPinB, OUTPUT);
  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
  
  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode 
  
  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);
  
  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

}

void loop() {
  unsigned long currentTime = millis();

  // Non-blocking timer to sample the duty cycle
  if (currentTime - lastUpdateTime >= updateInterval) {
    lastUpdateTime = currentTime;
    
    int analogValA = analogRead(inputPinA);
    int dutyCycleA = map(analogValA, 0, 1023, 26, 253);

    int analogValB = analogRead(inputPinB);
    int dutyCycleB = map(analogValB, 0, 1023, 26, 253);
     
    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
    
    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
     
    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB); 
  }

  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
  sevsegA.refreshDisplay(); 
  sevsegB.refreshDisplay();
}
[/code]

Put the code to output 99% duty cycle for 2 seconds in setup() and the code for variable output in loop()

Redundant.

If you want to keep the working code in loop(), use "states" and change states when time is right. You can add more timed states just by adding another "state timer."

byte state = 0;                      // start in state zero
unsigned long timer, timeout = 2000; // make a two-seconds timer

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (millis() - timer >= timeout) // after two seconds...
    state = 1;                     // ... change states

  if (state == 0) {       // starting state
    Serial.print("99% "); // duty cycle
    delay(250);
  }

  if (state == 1) {                                      // another state
    Serial.println(map(analogRead(A0), 0, 1023, 0, 99)); // map pot to duty cycle
    delay(250);
  }
}

The way I would do it since it is a one time thing with reset is set it up in setup(), delay(2000) then let it fall into your loop.

@alanbrock

add a timed while loop in setup() that keeps refreshing the display.

#include "SevSeg.h"

// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 11;  // PWM A output pin
const int pwmPinB = 12;  // PWM B output pin
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms

uint32_t start;


void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {9, 10};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {2, 3, 4, 5, 6, 7, 8};        // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
  pinMode(pwmPinA, OUTPUT); 
  pinMode(pwmPinB, OUTPUT);
  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
  
  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode 
  
  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);
  
  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

  //  first 2 seconds.
  start = millis();
  analogWrite(pwmPinA, 99);
  analogWrite(pwmPinB, 99);
  while (millis - start < 2000)
  {
    sevsegA.setNumber(99);
    sevsegB.setNumber(99); 
  }

}

void loop() {
  unsigned long currentTime = millis();

  // Non-blocking timer to sample the duty cycle
  if (currentTime - lastUpdateTime >= updateInterval) {
    lastUpdateTime = currentTime;

    int analogValA = analogRead(inputPinA);
    int dutyCycleA = map(analogValA, 0, 1023, 26, 253);
    int dutyPercentA = map(analogValA, 0, 1023, 10, 99);
    analogWrite(pwmPinA, dutyCycleA);
    sevsegA.setNumber(dutyPercentA);

    int analogValB = analogRead(inputPinB);
    int dutyCycleB = map(analogValB, 0, 1023, 26, 253);
    int dutyPercentB = map(analogValA, 0, 1023, 10, 99);
    analogWrite(pwmPinB, dutyCycleB);
    sevsegB.setNumber(dutyPercentB);
  }

  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
  sevsegA.refreshDisplay(); 
  sevsegB.refreshDisplay();
}

State machines as @xfpd suggested is even a better solution however might be overkill

you might update the values if the potmeter changes, that would make code slightly more reactive.


#include "SevSeg.h"
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 11;  // PWM A output pin
const int pwmPinB = 12;  // PWM B output pin
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms

uint32_t start;
int previousA = 0;
int previousB = 0;


void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {9, 10};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {2, 3, 4, 5, 6, 7, 8};        // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,

  pinMode(pwmPinA, OUTPUT);
  pinMode(pwmPinB, OUTPUT);

  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins

  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode

  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);

  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

  //  first 2 seconds.
  start = millis();
  analogWrite(pwmPinA, 99);
  analogWrite(pwmPinB, 99);
  while (millis - start < 2000)
  {
    sevsegA.setNumber(99);
    sevsegB.setNumber(99);
  }

}

void loop() 
{
  int analogValA = analogRead(inputPinA);
  if (analogValA != previousA)
  {
    previousA = analogValA;
    analogWrite(pwmPinA, map(analogValA, 0, 1023, 26, 253));
    sevsegA.setNumber(map(analogValA, 0, 1023, 10, 99));
  }

  int analogValB = analogRead(inputPinB);
  if (analogValB != previousB)
  {
    previousB = analogValB;
    analogWrite(pwmPinB, map(analogValB, 0, 1023, 26, 253));
    sevsegB.setNumber(map(analogValB, 0, 1023, 10, 99));
  }

  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
  sevsegA.refreshDisplay();
  sevsegB.refreshDisplay();
}

@alanbrock

Question 1:

Your display uses 100 steps, while the PWM uses 255 steps.
So it is possible that the PWM changes but the display does not.
Don't you want your PWM also in 100 steps?

Question 2:

The PWM is never 0, so you cannot stop the PWM. Is this by choice?
(In other words, why? what is driven by the PWM?)

Here is my code rewritten using xfpd's suggestion, which works well. I get 99 on both displays and a 99% duty cycle for 2 seconds then it reverts to whatever the pots are set to. The range of duty cycles is from 10% to 99%, showing 10 to 99 on the displays.

[code]
#include <SevSeg.h>
// nov7c
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 4;  // PWM A output pin 980Hz
const int pwmPinB = 13;  // PWM B output pin 980Hz
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms
byte state = 0;                      // start in state zero
unsigned long timer, timeout = 2000; // make a two-seconds timer

void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {29, 30};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {22, 23, 24, 25, 26, 27, 28}; // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
  pinMode(pwmPinA, OUTPUT); 
  pinMode(pwmPinB, OUTPUT);
  Serial.begin(115200);
  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
  
  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode 
  
  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);
  
  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

}

void loop()   {
  if (millis() - timer >= timeout) // after two seconds...
    state = 1;                     // ... change states

  if (state == 0) {       // starting state
    Serial.print("99% "); // duty cycle
        
    int dutyCycleA = 253;
    int dutyCycleB = 253;
    
    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
    
    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
     
    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB); 
        
  }
   if (state == 1){
    unsigned long currentTime = millis();

                    // Non-blocking timer to sample the duty cycle
   if (currentTime - lastUpdateTime >= updateInterval) {
    lastUpdateTime = currentTime;
    
    int analogValA = analogRead(inputPinA);
    int dutyCycleA = map(analogValA, 0, 1023, 26, 253);
    
    int analogValB = analogRead(inputPinB);
    int dutyCycleB = map(analogValB, 0, 1023, 26, 253);
     
    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
    
    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
     
    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB); 
  }
  }
  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
    sevsegA.refreshDisplay(); 
    sevsegB.refreshDisplay();

}
[/code]

There is one bug whilst the board is booting up i get both displays flashing 99 for about a quarter of a second, then 2 or 3 seconds later it shows 99 for 2 seconds on so on.

Haven't had time yet to read rob's post

Tried both of your sketches. Don't get any display and pwm is fixed at about 60% constantly.

update on bug, if I use reset button on board don't get the issue so don't think it will be a problem. Think the problem of not getting a display with sevseg code in setup rather than loop may be something to do with sevseg.refreshDisplay being in the loop?

should be while (millis() - start < 2000)

forgot a pair of brackets.

Use this completely empty function as your loop() function.

void loop() {
  // empty
}

Do you still get the flashing 99? Are your pots turned to 99?

Maybe you need to force the user to turn the pots to zero before sending the PWM signal to the motors inside the setup() or loop() functions... Read the pots, and only move to the next state (99 for two seconds) when the pots read zero.

I don't see any "flashing" function... I think your sevsegs are underpowered.

You should immediately call the following .refreshDisplay() after calling .setNumber():

 sevsegA.refreshDisplay(); 
 sevsegB.refreshDisplay();

[quote="xfpd, post:11, topic:1466745"]
I don't see any "flashing" function... I think your sevsegs are underpowered.
[/quote]

What I meant was to be accurate, as soon as board was powered up both displays showed 99 for a fraction of a second (while TX/RX and RESET LEDs were flashing), then remained off until program started.

With Rob's sketches I corrected millis() and the refreshDisplay() to the setup section and they both work now.
Have added my final sketch below and Rob's second sketch.
There is a bug with all 3 sketches and that is with pin 13. I need 980Hz PWM so I am using pins 4 and 13, unfortunately when board is booting up pin 13 is following the reset LED i.e. it goes high, low, high, low and stays low until the program starts then it is 99% PWM. Have tried pin 12 and there is no problem but it is 490Hz. Had this problem with a UNO R3 and pin 13, don't know how to get around this.

[code]
#include <SevSeg.h>
// nov7c
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 4;  // PWM A output pin 980Hz
const int pwmPinB = 13;  // PWM B output pin 980Hz
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms
byte state = 0;                      // start in state zero
unsigned long timer, timeout = 2000; // make a two-seconds timer

void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {29, 30};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {22, 23, 24, 25, 26, 27, 28}; // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
  pinMode(pwmPinA, OUTPUT); 
  pinMode(pwmPinB, OUTPUT);
  Serial.begin(115200);
  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
  
  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode 
  
  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);
  
  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

}

void loop()   {
  if (millis() - timer >= timeout) // after two seconds...
    state = 1;                     // ... change states

  if (state == 0) {       // starting state
    Serial.print("99% "); // duty cycle
        
    int dutyCycleA = 253;
    int dutyCycleB = 253;
    
    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
    
    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
     
    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB); 
        
  }
   if (state == 1){
    unsigned long currentTime = millis();

                    // Non-blocking timer to sample the duty cycle
   if (currentTime - lastUpdateTime >= updateInterval) {
    lastUpdateTime = currentTime;
    
    int analogValA = analogRead(inputPinA);
    int dutyCycleA = map(analogValA, 0, 1023, 26, 253);
    
    int analogValB = analogRead(inputPinB);
    int dutyCycleB = map(analogValB, 0, 1023, 26, 253);
     
    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage
    
    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage
     
    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB); 
  }
  }
  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
    sevsegA.refreshDisplay(); 
    sevsegB.refreshDisplay();

}
[/code]

Rob's


#include "SevSeg.h"
// nov7e
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 4;  // PWM A output pin
const int pwmPinB = 13;  // PWM B output pin
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms

uint32_t start;
int previousA = 0;
int previousB = 0;


void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {29, 30};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {22, 23, 24, 25, 26, 27, 28};        // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,

  pinMode(pwmPinA, OUTPUT);
  pinMode(pwmPinB, OUTPUT);

  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins

  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
                                      // common cathode

  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);

  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

  //  first 2 seconds.
  start = millis();
  analogWrite(pwmPinA, 253);
  analogWrite(pwmPinB, 253);
  while (millis() - start < 2000)
  {
    sevsegA.setNumber(99);
    sevsegB.setNumber(99);
    sevsegA.refreshDisplay();
    sevsegB.refreshDisplay();
  }

}

void loop() 
{
  int analogValA = analogRead(inputPinA);
  if (analogValA != previousA)
  {
    previousA = analogValA;
    analogWrite(pwmPinA, map(analogValA, 0, 1023, 26, 253));
    sevsegA.setNumber(map(analogValA, 0, 1023, 10, 99));
  }

  int analogValB = analogRead(inputPinB);
  if (analogValB != previousB)
  {
    previousB = analogValB;
    analogWrite(pwmPinB, map(analogValB, 0, 1023, 26, 253));
    sevsegB.setNumber(map(analogValB, 0, 1023, 10, 99));
  }

  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
  sevsegA.refreshDisplay();
  sevsegB.refreshDisplay();
}

This might work... to keep the SEVSEG from lighting momentarily...

  1. In setup()... configure the A0 (D14) and A1 (D15) for LOW output FIRST:
digitalWrite(14, LOW); // DIO pin number for A0
digitalWrite(15, LOW); // DIO pin number for A1
  1. Then, configure A0 (D14) and A1 (D15) pins for OUTPUT...
pinMode(14, OUTPUT); // the "DIO" pin number of A0
pinMode(15, OUTPUT); // the "DIO" pin number of A1
  1. Then... either (a) put a delay(); after step 2 to allow the "fraction of a second" pass and then insert the next two lines, or (b) let the code fall through to the loop() where you put these next two lines before the two-seconds timeout...
pinMode(14, INPUT); // A0 as normal AIN
pinMode(15, INPUT); // A1 as normal AIN

An example result would be...

#include <SevSeg.h>
// nov7c
// Using ATMEGA2560
SevSeg sevsegA; // Create display A controller object
SevSeg sevsegB; // Create display B controller object
const int inputPinA = A0; // Pin reading potentiometer A analog voltage
const int inputPinB = A1; // Pin reading potentiometer B analog voltage
const int pwmPinA = 4;  // PWM A output pin 980Hz
const int pwmPinB = 13;  // PWM B output pin 980Hz
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 100; // Update duty cycle value every 100ms
byte state = 0;                      // start in state zero
unsigned long timer, timeout = 2000; // make a two-seconds timer

void setup() {
  byte numDigitsA = 2;  // number of digits display A
  byte numDigitsB = 2;  // number of digits display B
  byte digitPinsA[] = {29, 30};  // Pins for Digits 1, 2,
  byte digitPinsB[] = {39, 40}; // Pins for Digits 3, 4.
  byte segmentPinsA[] = {22, 23, 24, 25, 26, 27, 28}; // Pins for segments A, B, C, D, E, F, G,
  byte segmentPinsB[] = {32, 33, 34, 35, 36, 37, 38}; // Pins for segments A, B, C, D, E, F, G,
  pinMode(pwmPinA, OUTPUT);
  pinMode(pwmPinB, OUTPUT);

  Serial.begin(115200);
  bool resistorsOnSegments = true; // 'true' if resistors are on segment pins

  byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE if your display is
  // common cathode

  bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
  bool leadingZeros = false;     // Set to true if you want '0050%' instead of '  50%'
  bool disableDecPoint = true;   // We don't need decimal points for simple percentages

  // Initialise the 7-segment display A
  sevsegA.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
                updateWithDelays, leadingZeros, disableDecPoint);
  sevsegA.setBrightness(90);

  // Initialise the 7-segment display B
  sevsegB.begin(hardwareConfig, numDigitsB, digitPinsB, segmentPinsB, resistorsOnSegments,
                updateWithDelays, leadingZeros, disableDecPoint);
  sevsegB.setBrightness(90);

  digitalWrite(14, LOW); // DIO pin number for A0
  digitalWrite(15, LOW); // DIO pin number for A1
  pinMode(14, OUTPUT); // the "DIO" pin number of A0
  pinMode(15, OUTPUT); // the "DIO" pin number of A1
  delay(1000);
  pinMode(14, INPUT); // A0 as normal AIN
  pinMode(15, INPUT); // A1 as normal AIN
}

void loop()   {
  if (millis() - timer >= timeout) // after two seconds...
    state = 1;                     // ... change states

  if (state == 0) {       // starting state
    // Serial.print("99% "); // duty cycle

    int dutyCycleA = 253;
    int dutyCycleB = 253;

    analogWrite(pwmPinA, dutyCycleA);
    int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage

    analogWrite(pwmPinB, dutyCycleB);
    int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage

    sevsegA.setNumber(dutyPercentA);
    sevsegB.setNumber(dutyPercentB);

  }
  if (state == 1) {
    unsigned long currentTime = millis();

    // Non-blocking timer to sample the duty cycle
    if (currentTime - lastUpdateTime >= updateInterval) {
      lastUpdateTime = currentTime;

      int analogValA = analogRead(inputPinA);
      int dutyCycleA = map(analogValA, 0, 1023, 26, 253);

      int analogValB = analogRead(inputPinB);
      int dutyCycleB = map(analogValB, 0, 1023, 26, 253);

      analogWrite(pwmPinA, dutyCycleA);
      int dutyPercentA = (dutyCycleA * 100 + 128) / 255; // calculate duty cycle A percentage

      analogWrite(pwmPinB, dutyCycleB);
      int dutyPercentB = (dutyCycleB * 100 + 128) / 255; // calculate duty cycle B percentage

      sevsegA.setNumber(dutyPercentA);
      sevsegB.setNumber(dutyPercentB);
    }
  }
  // CRITICAL: This must run continuously in the loop to keep the display illuminated!
  sevsegA.refreshDisplay();
  sevsegB.refreshDisplay();

}
diagram.json for wokwi
{
  "version": 1,
  "author": "xfpd",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-mega", "id": "mega", "top": 0.6, "left": -3.6, "attrs": {} },
    {
      "type": "wokwi-7segment",
      "id": "sevseg1",
      "top": 121.02,
      "left": 518.96,
      "attrs": { "digits": "2", "common": "anode", "colon": "" }
    },
    {
      "type": "wokwi-7segment",
      "id": "sevseg2",
      "top": 121.02,
      "left": 403.76,
      "attrs": { "digits": "2", "common": "anode", "colon": "" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": 200.3, "left": 278.2, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot2", "top": 200.3, "left": 76.6, "attrs": {} }
  ],
  "connections": [
    [ "mega:29", "sevseg2:DIG1", "green", [ "v1.75", "h13" ] ],
    [ "mega:30", "sevseg2:DIG2", "green", [ "v1.6", "h13.4" ] ],
    [ "mega:40", "sevseg1:DIG2", "green", [ "v1.25", "h36.2" ] ],
    [ "mega:39", "sevseg1:DIG1", "green", [ "v1.15", "h7" ] ],
    [ "mega:22", "sevseg2:A", "green", [ "v1.1", "h26.6" ] ],
    [ "mega:23", "sevseg2:B", "green", [ "v1.1", "h16.6" ] ],
    [ "mega:24", "sevseg2:C", "green", [ "v0.95", "h36.2", "v172.8", "h38.4" ] ],
    [ "mega:25", "sevseg2:D", "green", [ "v0.95", "h26.2", "v192", "h67.2" ] ],
    [ "mega:26", "sevseg2:E", "green", [ "v1.05", "h36.2", "v172.8", "h57.6" ] ],
    [ "mega:27", "sevseg2:F", "green", [ "v1.05", "h103", "v144" ] ],
    [ "mega:28", "sevseg2:G", "green", [ "v1.15", "h36.2", "v182.4", "h76.8" ] ],
    [ "mega:32", "sevseg1:A", "green", [ "v1.1", "h113" ] ],
    [ "mega:33", "sevseg1:B", "green", [ "v1.1", "h179.8" ] ],
    [ "mega:34", "sevseg1:C", "green", [ "v1.2", "h151.4", "v124.8", "h38.4" ] ],
    [ "mega:35", "sevseg1:D", "green", [ "v1.2", "h141.4", "v144", "h67.2" ] ],
    [ "mega:36", "sevseg1:E", "green", [ "v1.3", "h151.4", "v124.8", "h57.6" ] ],
    [ "mega:37", "sevseg1:F", "green", [ "v1.3", "h199" ] ],
    [ "mega:38", "sevseg1:G", "green", [ "v1.15", "h151.4", "v134.4", "h76.8" ] ],
    [ "mega:5V", "pot2:VCC", "red", [ "v93.3", "h-16.9" ] ],
    [ "mega:5V", "pot1:VCC", "red", [ "v93.3", "h165.5" ] ],
    [ "mega:GND.3", "pot2:GND", "black", [ "v102.9", "h-55.35" ] ],
    [ "mega:GND.3", "pot1:GND", "black", [ "v102.9", "h146.25" ] ],
    [ "mega:A0", "pot2:SIG", "green", [ "v112.5", "h-51.3" ] ],
    [ "mega:A1", "pot1:SIG", "green", [ "v112.5", "h112" ] ]
  ],
  "dependencies": {}
}

The momentary show of 99 is no longer an issue, it stopped a short while back, forgot to mention it sorry. Just have the problem with pin 13.
Even with a simple sketch i.e.turn on an LED with a switch, the LED (on pin13) will do 2 flashes when the board is powering up, pin 13 is also designated as SCK as well as digital I/O pin, which is probably why, UNO is the same.
What is WOKWI ?

wokwi.com is a site that allows you to simulate Arduino projects

The details of the project hardware are held in a .json file. If you go to the site and start a new project using a Mega board you can replace the barebones .json file that it creates with the one posted by @xfpd to create the hardware simulation and paste sketches into the sketch window to be run