"Setup void{" Running twice??? Help!

Hello!

i'm getting an issue where my setup code is running twice then going to the loop. If I seperate out the Setup only and delete the Loop void then it runs correctly once?? Any one know what this could be?? And I'm tweaking the code so all the notes aren't up to date at the moment. Thank you!!

#define button 3

int state = 0;
int old = 0;
int buttonPoll = 0;
int eye = 5;
bool doneFlag = 0;
void setup() {
  // put your setup code here, to run once:

pinMode (eye, OUTPUT);delay (50);
  pinMode (button, INPUT);
   delay (750);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (150);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 125);      //turn eyes to 125 brightness
   delay (175);
   analogWrite (eye, 0);        //turn eyes to off
   delay (10); 
   analogWrite (eye, 25);      
   delay (20);
   analogWrite (eye, 30);      
   delay (20);
   analogWrite (eye, 35);      
   delay (20);
   analogWrite (eye, 40);      
   delay (20);
   analogWrite (eye, 45);      
   delay (20);
   analogWrite (eye, 50);      
   delay (20);
   analogWrite (eye, 55);      
   delay (20);
   analogWrite (eye, 60);      
   delay (20);
   analogWrite (eye, 65);      
   delay (20);
   analogWrite (eye, 70);      
   delay (20);
   analogWrite (eye, 75);      
   delay (20);
   analogWrite (eye, 80);      
   delay (20);
   analogWrite (eye, 85);      
   delay (20);
   analogWrite (eye, 90);      
   delay (20);
   analogWrite (eye, 95);      
   delay (20);
   analogWrite (eye, 100);      
   delay (20);
   analogWrite (eye, 105);      
   delay (20);
   analogWrite (eye, 110);      
   delay (20);
   analogWrite (eye, 115);      
   delay (20);
   analogWrite (eye, 120);      
   delay (20);
   analogWrite (eye, 125);      
   delay (20);
   analogWrite (eye, 130);      
   delay (20);
   analogWrite (eye, 135);      
   delay (20);
   analogWrite (eye, 140);      
   delay (20);
   analogWrite (eye, 145);      
   delay (20);
   analogWrite (eye, 150);      
   delay (20);
   analogWrite (eye, 155);      
   delay (20);
   analogWrite (eye, 160);      
   delay (20);
   analogWrite (eye, 165);      
   delay (20);
   analogWrite (eye, 170);      
   delay (20);
   analogWrite (eye, 175);      
   delay (20);
   analogWrite (eye, 180);      
   delay (20);
   analogWrite (eye, 185);      
   delay (20);
   analogWrite (eye, 190);      
   delay (20);
   analogWrite (eye, 195);      
   delay (20);
   analogWrite (eye, 200);      
   delay (20);
   analogWrite (eye, 205);      
   delay (20);
   analogWrite (eye, 210);      
   delay (20);
   analogWrite (eye, 215);      
   delay (20);
   analogWrite (eye, 220);      
   delay (20);
   analogWrite (eye, 225);      
   delay (20);
   analogWrite (eye, 230);      
   delay (20);
   analogWrite (eye, 235);      
   delay (20);
   analogWrite (eye, 240);      
   delay (20);
   analogWrite (eye, 245);      
   delay (20);
   analogWrite (eye, 250);      
   delay (20);
   analogWrite (eye, 255);   

}

void loop() {
buttonPoll = digitalRead(button);
if(buttonPoll == 1) {
  delay(50);
  buttonPoll = digitalRead(button);
  if(buttonPoll == false) {
    state = old + 1;
  }}
  else{
   delay(100);
  }
switch (state) {
  case 1:    //if state is 2
   doneFlag = false;
   analogWrite(eye, 48);       //eyes at 37%
   old = state;                 //set old stste as current state 2
   break;
  case 2:                       //if state is 3
   analogWrite(eye, 24);       //eyes at 18%
   old = state;                 //set old state as current state 3
   break;
  case 3:                       //if state is 4
   analogWrite(eye, 8);        //eyes at 6%
   old = state;                 //set old state as current state 4
   break;
   case 4:                       //if state is 4
   analogWrite(eye, 0);        //eyes at 6%
   old = state;                 //set old state as current state 4
   break;
  default:                        //if state is not 1,2,3,4
if (doneFlag == false) {
   delay (750);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (150);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 125);      //turn eyes to 125 brightness
   delay (175);
   analogWrite (eye, 0);        //turn eyes to off
   delay (10); 
   analogWrite (eye, 25);      
   delay (20);
   analogWrite (eye, 30);      
   delay (20);
   analogWrite (eye, 35);      
   delay (20);
   analogWrite (eye, 40);      
   delay (20);
   analogWrite (eye, 45);      
   delay (20);
   analogWrite (eye, 50);      
   delay (20);
   analogWrite (eye, 55);      
   delay (20);
   analogWrite (eye, 60);      
   delay (20);
   analogWrite (eye, 65);      
   delay (20);
   analogWrite (eye, 70);      
   delay (20);
   analogWrite (eye, 75);      
   delay (20);
   analogWrite (eye, 80);      
   delay (20);
   analogWrite (eye, 85);      
   delay (20);
   analogWrite (eye, 90);      
   delay (20);
   analogWrite (eye, 95);      
   delay (20);
   analogWrite (eye, 100);      
   delay (20);
   analogWrite (eye, 105);      
   delay (20);
   analogWrite (eye, 110);      
   delay (20);
   analogWrite (eye, 115);      
   delay (20);
   analogWrite (eye, 120);      
   delay (20);
   analogWrite (eye, 125);      
   delay (20);
   analogWrite (eye, 130);      
   delay (20);
   analogWrite (eye, 135);      
   delay (20);
   analogWrite (eye, 140);      
   delay (20);
   analogWrite (eye, 145);      
   delay (20);
   analogWrite (eye, 150);      
   delay (20);
   analogWrite (eye, 155);      
   delay (20);
   analogWrite (eye, 160);      
   delay (20);
   analogWrite (eye, 165);      
   delay (20);
   analogWrite (eye, 170);      
   delay (20);
   analogWrite (eye, 175);      
   delay (20);
   analogWrite (eye, 180);      
   delay (20);
   analogWrite (eye, 185);      
   delay (20);
   analogWrite (eye, 190);      
   delay (20);
   analogWrite (eye, 195);      
   delay (20);
   analogWrite (eye, 200);      
   delay (20);
   analogWrite (eye, 205);      
   delay (20);
   analogWrite (eye, 210);      
   delay (20);
   analogWrite (eye, 215);      
   delay (20);
   analogWrite (eye, 220);      
   delay (20);
   analogWrite (eye, 225);      
   delay (20);
   analogWrite (eye, 230);      
   delay (20);
   analogWrite (eye, 235);      
   delay (20);
   analogWrite (eye, 240);      
   delay (20);
   analogWrite (eye, 245);      
   delay (20);
   analogWrite (eye, 250);      
   delay (20);
   analogWrite (eye, 255);      
   doneFlag = true;
  }
   old = 0;
   break;
 
}
}

How do you know?

Learn how to use 'arrays'.

Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Please edit your post to post the code properly, using code tags, as described in the "How to use this forum" sticky.

If setup is running twice, that'll be because you've had a reset.
Check your wiring.

Please remember to use code tags when posting code.

Indeed, how do you know?

I would suggest that you add a Serial.begin() in the beginning of setup() and a Serial.println("Setup finished") at the end of setup and observe the behaviour in serial monitor.
I would also suggest that you use Serial.println in the cases to follow the what is happening.

What can be happening is that your code runs setup and next runs the default case (state is initialised with 0) so it looks like the code is running twice.

Further I suggest that you're consistent in the use of true/false, 1/0, HIGH/LOW for buttonPoll.

Are you using a pullup or pulldown resistor for the button?

Lastly, write a function to control the 'eye'. It looks like all those analogwrites and delays in setup are exactly the same. The first step would be to move the part that is common in setup and in loop into a function and call that function. The next step would be to write a second function that can take the brightness and the delay as arguments. And the final step would be to use an array of structs (or two arrays) that you can loop through.

If your code really does run setup twice, your board did a reset; see AWOL's advise. What is 'eye'? A led? If so, does it have a resistor to limit the current?

Ok thank you I'll try to use the Serial.Print

And I know it is running twice because my leds donthe set up twice and then I can use my button for the cases.

And I am using a pulldown method with the tact switch.

Thank you guys for the help!
And sorry about not posting codes I'm knew to this, sorry!

Your state variable is initialised with zero. You have a default case in your switch that will handle state zero.

Thank you everyone for you're help! Especially you sterretje!

He helped me clean up my code as well as fix the issue. To stop my program setup from running twice I had to put in a case 0:

Here is the updated code just so it might help others with a similar issue! Thank you again!

#define button 3
int state = 0;
int old = 0;
int buttonPoll = 0;
int eye = 5;
bool doneFlag = 0;
void flicker(){
  delay (10);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (50);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 100);      //turn eyes to 100 brightness
   delay (150);
   analogWrite (eye, 0);        //turn eyes to off
   delay (30);
   analogWrite (eye, 125);      //turn eyes to 125 brightness
   delay (175);
   analogWrite (eye, 0);        //turn eyes to off
   delay (5);
}
void fade(){
  delay (5); 
  for (int brightness = 25; brightness <255; brightness += 5)
  {
    analogWrite (eye, brightness);
    delay (20);
  }
}
void setup() {
  // put your setup code here, to run once:
pinMode (eye, OUTPUT);delay (50);
pinMode (button, INPUT);
flicker();                    //Run Flicker Function
fade();                       //Run Fade Function
}

void loop() {
buttonPoll = digitalRead(button);
if(buttonPoll == 1) {
  delay(50);
  buttonPoll = digitalRead(button);
  if(buttonPoll == false) {
    state = old + 1;
  }}
  else{
   delay(100);
  }
switch (state) {
  case 0:
  analogWrite(eye, 255);
  old = state;
  break;
  case 1:    //if state is 2
   doneFlag = false;            //if state is 1
   analogWrite(eye, 48);        //eyes at 18%
   old = state;                 //set old stste as current state 2
   break;
  case 2:                       //if state is 2
   analogWrite(eye, 24);        //eyes at 9%
   old = state;                 //set old state as current state 3
   break;
  case 3:                       //if state is 3
   analogWrite(eye, 8);         //eyes at 4.5%
   old = state;                 //set old state as current state 4
   break;
   case 4:                      //if state is 4
   analogWrite(eye, 0);         //eyes at 6%
   old = state;                 //set old state as current state 4
   break;
  default:                      //if state is not 1,2,3,4
if (doneFlag == false) {
  flicker();                    //Run Flicker Function
  fade();                       //Run Fade Function
   doneFlag = true;
  }
   old = 0;
   break;

}
}