Counting up and down not in order

For this program what I’d like it do is using pins 3,5,7,8,9,10,11,12 (connected to LEDs) have it start by turning on pin number 3 for 50ms and then off for 100ms, then go to the next number. Once it reaches the end it turns back counting down now.

The problem I’m experiencing is that I have to make it smooth and use the pins assigned and because I’m not using pins 4 and 6 it’s causing a little bit of a delay. I’ve tried including if statements so that if the int a (counting integer) is equal to them it should just add one to int a and continue. But the delay is still there.

Just to recap: want to count from low-high and high-low without the delay of using none-sequential pins.

NOTE: I have the program running right now in the setup just it has a little delay between runs

Thanks for your time, attached is a fritzing pic and the code itself

int delayon = 50;
int delayoff = 100;
int a = 3;
void setup() {
  // initialize the digital pins as an output. DON’T’ FORGET TO USE COMMENTS!
  pinMode(7, OUTPUT); // Assigns pin 7 to an output
  pinMode(8, OUTPUT); // Assigns pin 8 to an output
  pinMode(9, OUTPUT); // Assigns pin 9 to an output
  pinMode(10, OUTPUT); // Assigns pin 10 to an output
  pinMode(11, OUTPUT); // Assigns pin 11 to an output
  pinMode(12, OUTPUT); // Assigns pin 12 to an output
  pinMode(3, OUTPUT); // Assigns pin 3 to an output
  pinMode(5, OUTPUT); // Assigns pin 5 to an output
  for ( int a = 3; a < 13 ; a++ ) //Runs a for loop that int a is equal to 3, until it's greater than 13, it adds one to int a
  {
    if ( a == 4 || a == 6) {
      a + 1;
    }
    digitalWrite(a, HIGH); // int a, starting at 3 increases everytime it loops until it's greater than 13, as it increases plus one each time it correspondes to the pin used to send an output of ON or HIGH
    delay(delayon); //Delay for how much it's on
    digitalWrite(a, LOW);
    delay(delayoff);
    if (a == 12) {
      for ( int a = 11; a < 13 ; a-- )
      {
        if ( a == 4 || a == 6) {
          a + 1;
        }
        digitalWrite(a, HIGH);
        delay(delayon);
        digitalWrite(a, LOW);
        delay(delayoff);
      }
    }
  }
}

void loop() {     // Main loop of program

}

P.S. sorry if the post isn’t formatted properly, trying to learn the rules from those stickies

Use an array instead.

const byte myPins[8] = {3,5,7,8,9,10,11,12};

void setup()
{
  for(byte i = 0; i < 8; i++)
  {
     pinMode(myPins[ i ], OUTPUT);
  }
   
  //now turn them ON and OFF
  digitalWrite(myPins[4], HIGH); // turns on pin 9
  delay(1000);
  digitalWrite(myPins[4], LOW); // turns off pin 9
}

void loop()
{

}

I think I understand what you mean, and it works now perfectly which is awesome, thank you very much.

One slight difficulty is it only runs once unless I reset the board or pull its power

  for (byte i = 0; i < 8; i++)
  {
    pinMode(myPins[ i ], OUTPUT);
    delay(delayon);
    digitalWrite(myPins [ i ], HIGH); // turns on pin 9
    delay(delayoff);
    digitalWrite(myPins[ i ], LOW); // turns off pin 9

    if (i == 7) {
      for ( byte i = 8; i > -1 ; i-- )
      {
        pinMode(myPins[ i ], OUTPUT);
        delay(delayon);
        digitalWrite(myPins [ i ], HIGH); // turns on pin 9
        delay(delayoff);
        digitalWrite(myPins[ i ], LOW); // turns off pin 9
      }
    }
    //now turn them ON and OFF
  }

Your first code shows everything in setup() which only runs once. So put the parts that you want to run continuously in loop().

Also you don't need to call pinMode() every single time, so put make a separate for loop and put it in setup() and have everything else inside loop().

There are also better ways to do what you want that don't require a for loop to cycle through the pins. However the one that has pinMode can stay in a for loop.

Sorry I’m just starting at this, so really appreciate your help.

It’s still only looping once with the code like so:

const byte myPins[8] = {3, 5, 7, 8, 9, 10, 11, 12};
int delayon = 50;
int delayoff = 100;

void setup()
{
}
void loop()
{
  for (byte i = 0; i < 8; i++)
  {
    pinMode(myPins[ i ], OUTPUT);
    delay(delayon);
    digitalWrite(myPins [ i ], HIGH); // turns on pin 9
    delay(delayoff);
    digitalWrite(myPins[ i ], LOW); // turns off pin 9

    if (i == 7) {
      for ( byte i = 8; i > -1 ; i-- )
      {
        delay(delayon);
        digitalWrite(myPins [ i ], HIGH); // turns on pin 9
        delay(delayoff);
        digitalWrite(myPins[ i ], LOW); // turns off pin 9
      }
    }
    //now turn them ON and OFF
  }
}

And you said there might be better ways, such as? Thanks again for the help

There’s a number of problems with that. The main one is that you’ve put a for() loop inside another for() loop and used the same name for the loop variable. The way that it has been done, there are now two variables and changing one doesn’t affect the other.

The comments about pin 9 don’t seem to be related to what the code is actually doing now. Always update the comments or remove them entirely when the code changes.

I think the reason that it appears to only run once is that byte is an unsigned type. It can never be negative. Therefore the test to see if it is -1 will not work as expected.

Here’s the minimum changes:

const int NumPins = 8;  //you use "8" in a lot of places in this sketch. Let's give it a name in case you need to change the number of pins in the future.
const int myPins[NumPins] = {3, 5, 7, 8, 9, 10, 11, 12}; //pin numbers are actually ints, even though they would fit into a byte
int delayon = 50;
int delayoff = 100;

void setup()
{
  //initialise all pin output modes here
  for (byte i = 0; i < NumPins; i++)
  {
    pinMode(myPins[ i ], OUTPUT);
  }
}
void loop()
{
  byte i; //declare i so that it can be used in multiple blocks inside the loop function
  for (i = 0; i < NumPins ; i++)
  {
    delay(delayon);
    digitalWrite(myPins [ i ], HIGH); // turns on pin
    delay(delayoff);
    digitalWrite(myPins[ i ], LOW); // turns off pin 
  }
  //A for() loop is only one kind of loop. The do-while is more appropriate to this situation:
  i = NumPins; //start above the last pin
  do
  {
    i--;  //decrement the loop variable at the start of the loop
    delay(delayon);
    digitalWrite(myPins [ i ], HIGH); // turns on pin 
    delay(delayoff);
    digitalWrite(myPins[ i ], LOW); // turns off pin 
  } while (i > 0)  //test the loop condition after executing the block of statements
}

This may not do exactly what you want, since the last pin is flashed twice in the sequence and the first pin will also flash twice - once at the end and once at the beginning. Change the bounds on one or both of the loops as appropriate.

I was thinking more along the lines of this:

@MorganS: I borrowed your code and modded it.

const int NumPins = 8;  //you use "8" in a lot of places in this sketch. Let's give it a name in case you need to change the number of pins in the future.
const int myPins[NumPins] = {3, 5, 7, 8, 9, 10, 11, 12}; //pin numbers are actually ints, even though they would fit into a byte
int delayon = 50;
int delayoff = 100;

void setup()
{
  Serial.begin(9600); // for faster results use 115200 instead, be sure to change the number in the serial monitor too. (number in Lower right corner)
  //initialise all pin output modes here
  for (byte i = 0; i < NumPins; i++)
  {
    pinMode(myPins[ i ], OUTPUT);
  }
}

void loop()
{
  static char Direction = 1; // positive value = count up, negative value = count down  
  static int i = 0; // i is your counter that will either increment or decrement based on the direction 
  
  //Serial.println(i); // uncomment to see the value of i
  delay(delayon);
  digitalWrite(myPins [ i ], HIGH); // turns on pin
  delay(delayoff);
  digitalWrite(myPins[ i ], LOW); // turns off pin
  
  i += Direction;
  if ( (i > NumPins - 1) || (i <= 0) ) // check boundaries, cannot be greater than the number of pins declared and cannot be less than 0
    Direction *= -1; // if either condition occur, change direction
}

I was going to follow up with a version that uses the BlinkWithoutDelay technique and Hazards is halfway there, so here’s my modification of that code:

const int NumPins = 8;  //you use "8" in a lot of places in this sketch. Let's give it a name in case you need to change the number of pins in the future.
const int myPins[NumPins] = {3, 5, 7, 8, 9, 10, 11, 12}; //pin numbers are actually ints, even though they would fit into a byte
const int delayon = 50;
const int delayoff = 100;

//set up global variables to track if the pin is on or off and the time it last changed
enum state {
  on,
  off
};
unsigned long LastTime;

void setup()
{
  Serial.begin(9600); // for faster results use 115200 instead, be sure to change the number in the serial monitor too. (number in Lower right corner)
  //initialise all pin output modes here
  for (byte i = 0; i < NumPins; i++)
  {
    pinMode(myPins[ i ], OUTPUT);
  }
  state = off;  //start in the off state
  LastTime = millis();  //start timing from "now"
}

void loop()
{
  static char Direction = 1; // positive value = count up, negative value = count down  
  static int i = 0; // i is your counter that will either increment or decrement based on the direction 

  if(state == off && millis() - LastTime > delayon) 
  {
    //pin is off and needs to be on
    //Serial.println(i); // uncomment to see the value of i
    digitalWrite(myPins [ i ], HIGH); // turns on pin
    LastTime += delayon; //add fixed amount of time
    state = on;
  }
  
  if(state == on && millis() - LastTime > delayoff) 
  {
    //pin is on and needs to be switched off and advanced to the next pin
    digitalWrite(myPins[ i ], LOW); // turns off pin
    LastTime += delayoff; //add fixed amount of time
    state = off;
  
    i += Direction;
    if ( (i > NumPins - 1) || (i <= 0) ) 
    {
      Direction *= -1;
    }
  }

  //other code can go here, which will work 'simultaneously' with the pins cycling on and off in sequence
  //so long as the code takes less than 'delayon' time to complete.

}

Why all those ints and the magic number ?

const byte myPins[] = {3, 5, 7, 8, 9, 10, 11, 12}; //no need to include the array dimension as the compiler will work it out
const byte NumPins = sizeof(myPins) / sizeof(myPins[0]);  //calculate the number of entries in the array in case it changes later