Im new to the programming world. Im trying to figure out if there is a way to make a certain section of my code loop a certain number of times with out copying and pasting it that amount of times. Can someone please help???? Here is the code ive written:
delay(550);
digitalWrite(1, LOW); // set the LED 1 on
delay(550);
digitalwrite(1, HIGH); // set the LED 1 off
digitalWrite(2, LOW); // set the LED 2 on
delay(230);
digitalWrite(2, HIGH); // set the LED 2 off
digitalWrite(3, LOW); // set the LED 3 on
delay(230);
digitalWrite(3, HIGH); // set the LED 3 off
digitalWrite(4, LOW); // set the LED 4 on
delay(550);
digitalWrite(4, HIGH); // set the LED 4 off
delay(550);
digitalWrite(5, LOW); // set the LED 5 on
delay(550);
digitalWrite(5, HIGH); // set the LED 5 off
digitalWrite(6, LOW); // set the LED 6 on
delay(550);
digitalWrite(6, HIGH); // set the LED 6 off
I would like to loop the LEDs 8 times then move on to the next section of my code.
That is fairly easily done using an array, a for loop and a counter.
Also, use the code tags to paste your code or the powers that be will chastise you. It is the icon 7th from the right above the smiley faces, hover over it and it will say "code".
Read over the "for loop" and "array's" in the reference section then if you have questions post them here.
Basically make an array of pins to "roll" through then count the number of times it rolls over and stop at 8.
You already have a for loop to initialise the pins. The other stuff is exactly the same:
for (int i=1; i<SOME VALUE; i++)
{
delay(550);
digitalWrite(i, LOW); // set the LED on
delay(550);
digitalwrite(i, HIGH); // set the LED off
}
More generally, if you don't have consecutive pins, you put the pin numbers in an array (just a bunch of consecutive memory), then use the contents of the array element as the pin number
int pinArray[] = {5, 4, 7, 8, 2, 3 }
for (int i=0; i<sizeof(pinArray)/sizeof(pinArray[0]; i++)
{
delay(550);
digitalWrite(pinArray[I], LOW); // set the LED on
delay(550);
digitalwrite(pinArray[I], HIGH); // set the LED off
}