newbie question

i just got my arduino the other day and have been getting overwhelmed with everything i need to learn, but im enjoying every sec of it. I am primarily interested in controlling leds in diff ways so thats were my learning has been geared. but most of what i find in robotics ect.

now my question. i have payed around with the code some but i think im making it harder than it is. i have made a simple loop of different flash patters on 5 leds. in this loop there is alot of repetition. is there a code that tell the arduino to "repete this 10 times"? that was i dont have a code 15 pages long...

also how can i set up a function select... like a button. when pressed it will change the flash pattern?

if someone could help me out or point me in the right direction of learning i would really appreciate it. thanks!!

int led1 =  12;    // LED connected to digital pin 13
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;

void setup()  
{                
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{
    digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);  
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);                 
  digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
digitalWrite(led1, HIGH);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(150);
digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);  
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);                 
  digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
digitalWrite(led1, HIGH);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(150);
digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  
  
  digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
    digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
  digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
    digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
  digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
  
  
  
   digitalWrite(led1, HIGH);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  delay(100);
    digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, HIGH);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, HIGH);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(100);

That’s “newbie”, I think :slight_smile:

To repeat something N times, the best program construct is a “for” loop:

for (byte i=0; i<N; i++) {
  // code
}

You can find more info in the reference and assorted tutorials.

I think you need to go and click on the word modify in the upper right corner and change the name of the thread. At the very least change that ‘d’ to a ‘b’. :wink:

I think it would be a good idea to check out some examples that set up a knight rider kind of effect with the LEDs. Here is one that gives you 3 different versions of the code that offer the same effect.

Now another method would be to create different functions for each pattern you want. Something like this.

int led1 =  12;    // LED connected to digital pin 13
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;

void setup()  
{                
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{
  pattern1();  // calls pattern #1
  pattern2();  // calls pattern #2

    
}

void pattern1()
{
  // incert code for pattern #1
}

void pattern2()
{
  // incert code for pattern #2
}

You could have as many patterns in there as you want. As it sits right now it would run pattern 1, then 2 and back again. If you incorporate the button tutorial then you can set up 1 or more buttons. Where each button could call up a different pattern to run. Though you may want it it be one press to have it run threw 1 pattern constantly till another button is pressed. It all depends on what you would like to achieve.

Hey, whats wrong with coding an Arduino in the nude. We sure have a lot of prudes around here. ;D

Lefty

lmao sorry i didnt even notice the typo. thanks for the responses. i'll read up on those link and the button tutorial and post my results. thanks again guys.

have been getting overwhelmed with everything i need to learn

I wrote a "course" about Arduino programming, making an effort to "build" the student's understanding a brick at a time, avoiding the problems of needing to know three things at once to take the next step.

It starts with the Very Basic, but soon moves into a higher gear.

It is at...

http://sheepdogguides.com/arduino/FA1main.htm

Hope it helps.

ok so i have done some reading and got it to do what i wanted!! thanks alot for the help guys. i set the leds with 10 diff patters and 1 steady on. the code is too long to post so im only posting the first part, plz let me know if there is any easier/better way. i;m always looking for constructive criticism. thanks again guys!

const int led1 =  12;    // LED connected to digital pin 13
const int led2 = 11;
const int led3 = 10;
const int led4 = 9;
const int led5 = 8;
const int  buttonPin = 2;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(buttonPin, INPUT);
 Serial.begin(9600);


}

// the loop() method runs over and over again,
// as long as the Arduino has power


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
     // save the current state as the last state, 
    //for next time through the loop
    lastButtonState = buttonState;
  }
if (buttonPushCounter >10){
 buttonPushCounter=0 ;}

switch (buttonPushCounter  +1 )

{
  case 1:
  digitalWrite(led1, HIGH);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  break;
  
  case 2:
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);  
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);                 
  digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
digitalWrite(led1, HIGH);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(150);
digitalWrite(led1, LOW);    
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  delay(150);
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(150);
break;

case 3:
digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
  digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100); 
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(100);
   digitalWrite(led1, LOW);    
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);
  digitalWrite(led1, HIGH);  
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(100);