Restart code and cleanup code with led project.

Hello everyone !

Im a totally noob and beginner in all this, with very old programming skills. So bear with me and hopefully youll learn me some good ways to solve things :)

This is my project: I got five leds and a piezo element, I want a button on the board aswell. The leds are devided into four yellow leds and one red, all yellow leds are on HIGH at first and then starting to go slowly to LOW. When the last yellow led goes out, then the red led takes over with HIGH and then starts blinking after X amount of time until they all goes out. The piezo element just buzz when a led goes out.

My want and need of help is to learn how to maybe easy ways of cleaning the code, maybe have a variable or something to store info in ? But the request and need is to have a button to restart the Void Setup code. So whenever I press the button the leds restart/the code goes back to its first line so to say. - The button on the arduinoboard "reset" works as my want as a button to the breadboard.. so how to mimic that in code ?

int ledPin [] =  {2,3,4,5,6};
int speakerOut = 7;


void setup() {
pinMode (7, OUTPUT);
pinMode(2,OUTPUT); 
pinMode(3,OUTPUT); 
pinMode(4,OUTPUT); 
pinMode(5,OUTPUT); 
pinMode(6,OUTPUT); 
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, HIGH);
delay(1000);
digitalWrite (5, LOW);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(1000);
digitalWrite (4, LOW);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(1000);
digitalWrite (3, LOW);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(1000);
digitalWrite (2, LOW);
digitalWrite (6, HIGH); 
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(2000);
digitalWrite (6, LOW);
delay(200);
digitalWrite (6, HIGH);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(200);
digitalWrite (6, LOW);
delay(200);
digitalWrite (6, HIGH);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(200);
digitalWrite (6, LOW);
delay(200);
digitalWrite (6, HIGH);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(200);
digitalWrite (6, LOW);
delay(200);
digitalWrite (6, HIGH);
   digitalWrite (7,HIGH);
   delay(100);
   digitalWrite (7,LOW);
      delay(100);
      digitalWrite (7,HIGH);
         delay(100);
         digitalWrite (7,LOW);
               delay(100);
      digitalWrite (7,HIGH);
delay(200);
digitalWrite (6, LOW);


         digitalWrite (7,LOW);
delay(200);
}

void loop()
{

}

most important thing you must learn is how to use array’s - http://www.arduino.cc/en/Reference/Array -

and especially the for loop - www.arduino.cc/en/Reference/For -

here a sample snippet that shows some for loop usage

for (int pin=2; pin <8; pin++) 
{
  digitalWrite (pin , HIGH);
}
delay(1000);

for (int pin=2; pin <8; pin++) 
{
  digitalWrite (pin , LOW);
  delay(250):
}

for (int pin=2; pin <8; pin++) 
{
  digitalWrite (pin , HIGH);
}
delay(1000);

for (int pin=2; pin <8; pin++) 
{
  digitalWrite (pin , HIGH);
  delay(i*100):
}

BTW, use CTRL-T before copying code, it auto-layouts which makes it better readable

Thanks for the info about it.

I dont want it to loop. The ledproject is only run once and thats okay. But I want it to be able at any time to restart from the beginning. So no need on the loop part at all. As it is; it works with the awful copy/paste digitalWrite command. The four (nr.2.3.4.5) led are all lighted up and slowly one by one turned off and at the end the red led (nr.6)has a little flash too. The codetext isnt the fun the look at, so will look into the arrays and see if I cant get variables and store info and just have a variable called instead.

But is there any easy way/command to restart a VoidSetup so restart as the "reset" button onboard ? It must be able to restart in the middle of the program and just jump back to the start of the script.. a button that reset the code so to say.

setup()
{
  // setup thingies ...
}

loop()
{
   doAllstuff();
   while (digitalRead(myButton) == LOW);  // assuming LOW is not pressed
}

doAllStuff()
{
  // light the leds in the wanted pattern
}

I dont want it to loop.

Tuff - that is what micro controllers do. What you have to do is to write your code so it does what you want. That includes only doing something once until something makes it do it again.

Hint, you use variables and if statements so that the bit you don't want to do every time through the loop only gets done under the conditions you want it done.

But is there any easy way/command to restart a VoidSetup so restart as the "reset" button onboard ?

No it leads you into the wrong way of thinking.

It must be able to restart in the middle of the program

Now that is just silly, see I told you it would lead you into the wrong way of thinking.