How to run this function twice ?

Hey
I am working on a basic led project. This is as follows: There are 8 leds, led1 goes 2x back and forth and stays on. Then led2 goes 2x back and forth and stays on with led1. And finally, all the leds are on. Only my program lets led1 go back and forth 2x , but the rest only 1x. How can I fix this?

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;

int Begin = 1;

void setup () {
  pinMode(ledPin1 ,OUTPUT);
  pinMode(ledPin2 ,OUTPUT);
  pinMode(ledPin3 ,OUTPUT);
  pinMode(ledPin4 ,OUTPUT);
  pinMode(ledPin5 ,OUTPUT);
  pinMode(ledPin6 ,OUTPUT);
  pinMode(ledPin7 ,OUTPUT);
  pinMode(ledPin8 ,OUTPUT);
  
  Begin = 1;
}

void loop() {


for(int thisPin = Begin; thisPin <9; thisPin++){

digitalWrite(thisPin, HIGH);
delay(100);
digitalWrite(thisPin,LOW);

}


for(int thisPin = 9; thisPin >Begin; thisPin--){
  digitalWrite(thisPin, HIGH);
  delay(100);
  digitalWrite(thisPin, LOW); 
}

digitalWrite(Begin,HIGH);
Begin = Begin +1 ;

}

LEDV5.ino (729 Bytes)

for(int thisPin = BeginWhat is attached to pin 1?

When you find yourself adding a numeric suffix to variable names, it's time for an array, more often than not.

Nothing actually

Then why mess with it?

Pin 1 is a Serial pin so messing with it will mess with the ability to do serial communication.

I've changed it to pin 2

So, post your code.

int Begin = 2;
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
int pinCount = 8;


void setup () {
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }


  Begin = 1;
}

void loop() {


  for (int thisPin = Begin; thisPin < 9; thisPin++) {

    digitalWrite(thisPin, HIGH);
    delay(100);
    digitalWrite(thisPin, LOW);

  }


  for (int thisPin = 9; thisPin > Begin; thisPin--) {
    digitalWrite(thisPin, HIGH);
    delay(100);
    digitalWrite(thisPin, LOW);
  }

  digitalWrite(Begin, HIGH);
  Begin = Begin + 1 ;

}

You changed it?

What limits the value of Begin?

AWOL:
What limits the value of Begin?

I don't get it ? And yes replaced it with arrays.

What stops the value of Begin going out of range?

And now you overflow the array :wink: Element 8 does not exist in you array :slight_smile:

Now you use an array, you index in that array, starting at 0, ending at 7.

And now try to do that everywhere :slight_smile:

AWOL:
What stops the value of Begin going out of range?

I really don't know.

You initialise Begin to 2, then in setup() you set it to 1.
But you're using it to index an array (array indices start at zero). Once you go off either end of the array, all bets about what the code will do are off.

I did what you've said in my first version i posted . But still can't make it work.

PROTRADER500:
I did what you've said in my first version i posted .

Where?

But you made mistakes alone the way... Without fixing that you never end with a working version. So please try to fix it and every time you edit the code, repost it :slight_smile:

Thanks this code made it work !

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;

int Begin = 2;

void setup () {
  pinMode(ledPin1 , OUTPUT);
  pinMode(ledPin2 , OUTPUT);
  pinMode(ledPin3 , OUTPUT);
  pinMode(ledPin4 , OUTPUT);
  pinMode(ledPin5 , OUTPUT);
  pinMode(ledPin6 , OUTPUT);
  pinMode(ledPin7 , OUTPUT);
  pinMode(ledPin8 , OUTPUT);

  Begin = 2;

}

void loop() {

  for (int thisPin = Begin; thisPin < 9; thisPin++) {

    digitalWrite(thisPin, HIGH);
    delay(200);
    digitalWrite(thisPin, LOW);
  }


  for (int thisPin = 9; thisPin > Begin; thisPin--) {
    digitalWrite(thisPin, HIGH);
    delay(200);
    digitalWrite(thisPin, LOW);
  }

  for (int thisPin = Begin; thisPin < 9; thisPin++) {

    digitalWrite(thisPin, HIGH);
    delay(200);
    digitalWrite(thisPin, LOW);
  }


  for (int thisPin = 9; thisPin > Begin; thisPin--) {
    digitalWrite(thisPin, HIGH);
    delay(200);
    digitalWrite(thisPin, LOW);
  }
  digitalWrite(Begin, HIGH);

  digitalWrite(Begin, HIGH);
  Begin = Begin + 1 ;

}