Go Down

Topic: Speed of an Arduino Mega slower than Arduino Uno? (Read 1 time) previous topic - next topic

Santepeter85

Hi,
I'm using a 7-segment display to have a 60 minutes countdown clock in a game. If the player makes an error, they get a time penalty (straftijd) where the time will run down a few minutes extra fast (3 minutes in a few seconds).
I've made a code which is exactly the same as the code to run the clock on normal speed (with delay 1000 per second). Now i have reduced and/or even removed the delay during the timepenalty. The original code was on an arduino Uno. Now i am running it on a Mega but the countdown clock isn't going as fast.... It takes about 10 seconds to give a 3 minutes time panelty. On an Uno it takes about 3 seconds)

I've isolated the code for the timepenalty to run in on an Mega and it is going to slow.
Any idea why this is slower than on an Uno?
And any idea how to make this faster (about 3 or 4 times faster atleast)

Code: [Select]

#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"

const byte PIN_CLK = 9;
const byte PIN_DIO = 13;
SevenSegmentExtended display(PIN_CLK, PIN_DIO);

void setup() {
  display.begin();
  display.setBacklight(100);
}

void  loop() {
  byte straftijd = 180;
  byte hours = 60;
  byte minutes = 00;
    for ( ; straftijd > 0 ;) {
      for ( ; hours > 0 ; ) {
        for ( ; minutes > 0; minutes-- && straftijd--) {
          display.printTime(hours, minutes, true);
          if (straftijd == 0) {
            return;
          }
        };
        if (straftijd > 0) {
          minutes = 00;
          display.printTime(hours, minutes, true);  //print seconde 00
          hours--;
          minutes = 59;
      }
    }
  }
}

lastchancename

At first glance, you are reinitializing straftijd *every* time around loop()

These declarations and initialisers ideally occur either as globals, and/or initialised in setup().  Remember to maintain scope...

Then read/manipulate/reset the value only when needed withinin your loop.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Santepeter85

thanks for your help. In this test code the 'straftijd' is reset every loop. In the original code it is set to different values.
The problem is that the counting down is going to slow. Even when there is no delay in the code, it takes more time to count down on a Mega than on an Uno.

Any advise to make this faster?

econjack

My experience is that most graphics libraries run slower on a Mega than an Uno. Because the same source code runs slower on the Mega, my guess is that it has something to do with difference in data sizes (e.g., Instruction Pointer) the Mega uses to cope with its increased memory resources. I know there are people here who can give you a more definitive answer.

johnwasser


Code: [Select]
        for ( ; minutes > 0; minutes-- && straftijd--) {

This is a good place to use the comma operator:
Code: [Select]
        for ( ; minutes > 0; minutes-- , straftijd--) {
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

westfw

The mega will generally be slightly slower than an uno because things like the call and return instructions take extra time and memory to deal with the extra byte in code addresses.  But it shouldn't be 3x slower!

Go Up