# Programming 4-digit seven segment display using interrupts only

I have to program an autonomous bot (using an ATmega2560). It has a 4-digit seven segment display attached to it. I have to make the bot traverse through arena while continuously displaying the time in seconds on the seven segment display.

I can't use the code to display on seven segment display in my main() function.

Any help?

• First, I think the most common meaning of "ssd" is solid state drive. Please spell out what you mean. Second, how are you driving the display? Communication with a driver chip or are you powering each segment individually? If comms to a driver chip, what's the protocol? Third, why can't you update the display in your main code? Feb 21, 2016 at 14:02
• Welcome to robotics Shivendra Singh Vishen. On stack exchange, questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Please edit your question to add this information. Take a look at How to Ask and tour for more information on how stack exchange works. For advice on how to write a good question, see the Robotics question checklist. Feb 22, 2016 at 10:36

While this question may seem to be better suited for SE Arduino, I shall, nevertheless, propose a solution. Employ a ready written Arduino library, as this would save a lot of groundwork. Granted, as you mention that you have a main(), then one can assume that you are not using the Arduino IDE. However, the library (with the .cpp file and .h file) can still be used.

Interestingly, I went through the pain of coding up a seven segment display last year, on the Arduino, albeit only a two digit display, an SL-1255 (see Dual Seven Segment display madness). I even came up with an all singing and dancing Arduino library, SevenSegmentSL1255.

However, I do not recommend you using it, as it does not use interrupts and, much to my chagrin, I found another, more fully featured, seven segment display Arduino library, SevenSeg, after having completed mine. The SevenSeg library will work with four digit displays and it also handles interrupt driven displays.

From section 4.4 Using Interrupt Timers of the PDF,

Running the printing functions in an endless loop to perform multiplexing is not always an ideal way to do things. Outputting information to a 7-segment display is not a computationally intensive task, but due to the delay used for multiplexing, the microcontroller just sits there and waits for most of the time. Sure, you can insert commands taking little time in a loop together with the printing functions, but if they are slightly time-consuming, the display will halt or flicker. For this purpose it is possible to use SevenSegalong with interrupt timers. Then, you can do whatever you want inside loop(){...}, and simply run a high-level printing function only when you want to change what's on the display. The microcontroller will automatically be interrupted just briefly to update the display as needed. You can still change the refresh rate and the duty cycle like normal, the SevenSeg-library will take care of configuring the timers for you.

An example sketch (again taken from section 4.4 of the PDF) is as follows:

#include <SevenSeg.h>
SevenSeg disp(11,7,3,5,6,10,2);
const int numOfDigits =4;
int digitPins[numOfDigits]={12,9,8,13};

void setup () {
disp.setDigitPins(numOfDigits ,digitPins);
disp.setTimer(2); disp.startTimer();
}

void loop() {
for(int i=1;i<=10;i++){
disp.write(i);
delay(1000); // Or other time-consuming tasks
}
}

ISR(TIMER2_COMPA_vect) {
disp.interruptAction();
}


Obviously, you will need to modify the sketch for your particular purposes, notably either calling setup() and loop() from your main(), or incorporating the display updates from the loop() into your main().

However, please make particular note of the Interrupt Service Routine (ISR), as this is what you require: ISR(TIMER2_COMPA_vect) {disp.interruptAction();}. This will ensure that you have a flicker free display, while the processor is performing processor intensive tasks, when controlling your bot.