8 port relay module, MEGA 2560, Port A, code doesn't stop cycling in void setup

I’m turning on 8 relays at the same time by activating pins 22-29 on an Arduino MEGA 2560 using the port A commands below.

Even when cycling my code for 2 times, and having the usb port plugged into my PC, the code doesn’t stop running.

Am I not turning off my port properly in the void loop?

unsigned long TOn = 4000; // delay time to turn relay on/off (60,000 ms = 1 minute)
unsigned long TOff = 2000;

void setup () {
  Serial.begin(9600);

  DDRA = B11111111; // set PORTA (digital 22-29) to outputs

  int maxnumber = 2; // set cycle count

  for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
  {
    PORTA = B00000000;

    delay(TOn); //relay on time

    PORTA = B11111111;

    delay(TOff); //relay off time
  }
}

void loop() {
  PORTA = B11111111;
}

the code doesn't stop running.

How do you KNOW that?

Why is it important that all 8 relays turn on or off at exactly the same time? Seems to me that if you really wanted that, you'd only need one relay.

I think the FOR loop is missing the increment count++.

I’m doing it for mainly future applications

https://www.arduino.cc/en/Reference/PortManipulation

Some reasons:

You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.

Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;

If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!

It was the count++ increment that was missing. :blush: Thanks!

This code below is working:

unsigned long TOn = 4000; // delay time to turn relay on/off (60,000 ms = 1 minute)
unsigned long TOff = 2000;

void setup () {
  Serial.begin(9600);

  DDRA = B11111111; // set PORTA (digital 22-29) to outputs

  int maxnumber = 4; // set cycle count

  for (int count = 0; count < maxnumber; count++) //Counter will stop counting after certain amount of cycles
  {
    PORTA = B00000000;

    delay(TOn); //relay on time

    PORTA = B11111111;

    delay(TOff); //relay off time
  }
}

void loop() {}