Saving space by defining in a one line

     digitalWrite(LedPin7, 0);
     digitalWrite(LedPin8, 0);
     digitalWrite(LedPin9, 0);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);

In this above type of script, is it possible to define all LedPins a "zero" in a single line just to save a more little space ?

1 Like

Sure. Put the pin numbers in an array and loop through the array with a "for" loop.

Can you please elaborate a bit ? thanks !

You can use a for loop from the first to the last pin number, provided that the pins have contiguous numbers. Else you can use the for loop with an array of pin numbers. Or you use the pins of a port and clear the entire port, what also will produce the shortest and fastest code.

That's interesting

Could you please have me small script on it

Just example with only required lines & I ll take care of rest !!!

What "space" are you trying to "save" and why do you think you need to save it? I get the impression that you may be trying to solve a nonexistent problem.

Can you make an array of bytes with the actual pin numbers in the array?

Hi, try this way:
PORTD = 0x7F & PORTD; // sets Arduino pins 7 outputs,0
PORTB = 0x80 & PORTB; // sets Arduino pins 8 to 12 outputs,0

1 Like

Is it something like this ?

const int pins[6] = {7, 8, 9, 10, 11, 12};

O yeah its working . . Superb

what about this one

digitalWrite(LedPin6, 0);
     digitalWrite(LedPin8, 0);
     digitalWrite(LedPin9, 0);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);

Actually the requirement is like this :-

     digitalWrite(LedPin6, 1);
     digitalWrite(LedPin7, 0);
     digitalWrite(LedPin8, 0);
     digitalWrite(LedPin9, 0);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);
     digitalWrite(LedPin6, 0);
     digitalWrite(LedPin7, 1);
     digitalWrite(LedPin8, 0);
     digitalWrite(LedPin9, 0);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);
     digitalWrite(LedPin6, 0);
     digitalWrite(LedPin7, 0);
     digitalWrite(LedPin8, 1);
     digitalWrite(LedPin9, 0);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);
digitalWrite(LedPin6, 0);
     digitalWrite(LedPin7, 0);
     digitalWrite(LedPin8, 0);
     digitalWrite(LedPin9, 1);
     digitalWrite(LedPin10, 0);
     digitalWrite(LedPin11, 0);
     digitalWrite(LedPin12, 0);

and so on up to the (Pin12, 1)

Please Help !!!!!!!

Yes, the LED pin numbers are first placed in an array.

Make arrays for each of the on/off LED state patterns.

When you want to update a LED pattern, iterate thru the pin array while accessing the pattern array required.

Hi,
try this way:
The delay can be removed if you like;

void loop() {

  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  delay(1000);
  bitSet(PORTD, 6);     // sets Arduino pins 6 outputs,1
  delay(1000);
  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  bitSet(PORTD, 7);     // sets Arduino pins 7 outputs,1
  delay(1000);
  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  bitSet(PORTB, 0);     // sets Arduino pins 8 outputs,1
  delay(1000);

  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 1);     // sets Arduino pins 9 outputs,1
  delay(1000);
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 2);     // sets Arduino pins 10 outputs,1
  delay(1000);
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 3);     // sets Arduino pins 11 outputs,1
  delay(1000);
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 4);     // sets Arduino pins 12 outputs,1
  delay(1000);
}

without delay:

void loop() {
  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTD, 6);     // sets Arduino pins 6 outputs,1
  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  bitSet(PORTD, 7);     // sets Arduino pins 7 outputs,1
  PORTD = 0x3F & PORTD; // sets Arduino pins 6 and 7 outputs,0
  bitSet(PORTB, 0);     // sets Arduino pins 8 outputs,1
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 1);     // sets Arduino pins 9 outputs,1
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 2);     // sets Arduino pins 10 outputs,1
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 3);     // sets Arduino pins 11 outputs,1
  PORTB = 0xE0 & PORTB; // sets Arduino pins 8 to 12 outputs,0
  bitSet(PORTB, 4);     // sets Arduino pins 12 outputs,1
}

Or try this way:

void loop() {
  for (int i = 0; i < 7; i++)
  {
      digitalWrite(i + 6, HIGH);
      digitalWrite(i + 5, LOW);
      delay(1000);
    if (i == 6)
       digitalWrite(i + 6, LOW);
  }
}

In this PORT sequence

All are not working

After count 2 or from count 3 i.e. Pin 8 , the output LED gives a very dimmed light output

Sequence is going great but output level is very very low from Pin 8 to Pin 12

Post the code you talking about.

a7

consider

#undef MyHW
#ifdef MyHW
const byte PinsLed [] = { 10, 11, 12, 13 };
#else
const byte PinsLed [] = { 6, 7, 8, 9, 10, 11, 12 };
#endif

#define Nled   sizeof(PinsLed)

unsigned idx;

// -----------------------------------------------------------------------------
// the loop function runs over and over again forever
void loop ()
{
    Serial.println (idx);
    for (unsigned n = 0; n < Nled; n++)  {
        digitalWrite (PinsLed [n], idx == n);
    }
    if (Nled <= ++idx)
        idx = 0;
    delay (250);
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    for (unsigned n = 0; n < Nled; n++)  {
        pinMode (PinsLed [n], OUTPUT);
    }
}

From this last one For loop the LEDs are working automatically like a chaser unit

With this code LEDs are working like chaser sequence in automatic mode
No controls with push buttons

where did you mention buttons?

No Sorry actually what I need here is just to replace the long long script to short one

& rest I ll apply of my own as per my requirement

But not the self control mode

Just want to replace 4 4 lines to single or a couple of lines
like I mentioned in post #11