user54017: I am a beginner in Arduino programming. This is my program for 5 LEDs and 1 Button. The LEDs are supposed to light before I press the button. But the LEDs start immediately, even without pressing the button. How is it possible?
int sw7=7;
int timer=500;
int start=0;
int stopp=100;
void setup()
{
  for (int opin=1;opin<6;opin++)
  {
    pinMode(opin,OUTPUT);
    pinMode(sw7,INPUT);
  }
}

void loop()
{
  if (digitalRead (sw7)==LOW)
  {
    while (start<=stopp);
  }
  {
    for (int opin=1;opin<=5;opin++)
    {
      digitalWrite(opin,HIGH);
      delay(timer);
      digitalWrite(opin,LOW);
     } 
  }
}


user2967: Several things:
You need to explicitly set your output pins to LOW in your setup method.
The code to set the sw7 to input should not be inside the loop.
You should probably connect your switch between sw7 and ground and use INPUT_PULLUP mode (in INPUT_PULLUP mode the line is connected to +5V through a "pull-up resistor" so that it reads HIGH until you press the button, and then it reads LOW. If you don't use either an input pull-up or pull-down resistor then the input tends to "float" and give bad readings.)
You say "The LEDs are supposed to light before I press the button. But the LEDs start immediately, even without pressing the button."
I'm not sure what you mean. What do you want to happen, exactly? Do you want the LEDs to be lit steadily, and then turn off or flash when you press the button?
The code below will turn off all the LEDs initially, then toggle between off and flashing on each (debounced) button press. Switch the value of idleLEDState to HIGH if you want the LEDs to be lit when they are not flashing.
#define flashDelay 50 //Delay between on and off state for LEDs
#define switchBounceDelay 100


uint8_t idleLEDState = LOW; //Change to HIGH 
                            //if you want the LEDs to be lit when not flashing
bool flashLEDs = false;
bool ledsOn = false; 
unsigned long nextFlashTime = 0;
unsigned long nextSwitchCheckTime = 0;

void setup() {
  pinMode(sw7, INPUT_PULLUP); //Connect the switch between sw7 and ground
  for (int opin=1;opin<6;opin++)
  {
    pinMode(opin,OUTPUT);
    digitalWrite(opin, idleLEDState); 
  }
}

void loop() {

  //If it's time to check the switch, and it's being pulled LOW, toggle flashLEDs
  if (mills() > nextSwitchCheckTime && digitalRead(sw7) == LOW) {
    flashLEDs = !flashLEDs;
    nextSwitchCheckTime = millis() + switchBounceDelay;
  }

  if (!flashLEDs) {
    //IF we're not supposed to flash the LEDs, set them to their idle state 
    for (int opin=1;opin<6;opin++) {
        digitalWrite(opin, idleLEDState); 
     }
  } else if millis() > nextFlashTime {
    nextTime = millis() + flashDelay;
    ledsOn = !ledsOn;
    for (int opin=1;opin<6;opin++) {
        uint8_t newState = ledsOn ? HIGH : LOW;
        digitalWrite(opin, newState); 
     }
}

I banged out the code above in the Stack Exchange editor. It likely contains a few typos. Please exert some effort to fix it and adapt it to your needs before posting back "Your code didn't work. I got error XYZ."

54017: <p>I am a beginner in Arduino programming. This is my program for 5 LEDs and 1 Button. The LEDs are supposed to light before I press the button. But the LEDs start immediately, even without pressing the button. How is it possible?</p>

<pre><code>int sw7=7;
int timer=500;
int start=0;
int stopp=100;
void setup()
{
  for (int opin=1;opin&lt;6;opin++)
  {
    pinMode(opin,OUTPUT);
    pinMode(sw7,INPUT);
  }
}

void loop()
{
  if (digitalRead (sw7)==LOW)
  {
    while (start&lt;=stopp);
  }
  {
    for (int opin=1;opin&lt;=5;opin++)
    {
      digitalWrite(opin,HIGH);
      delay(timer);
      digitalWrite(opin,LOW);
     } 
  }
}
</code></pre>
 2967: <p>Several things:</p>

<p>You need to explicitly set your output pins to <code>LOW</code> in your setup method.</p>

<p>The code to set the sw7 to input should not be inside the loop.</p>

<p>You should probably connect your switch between sw7 and ground and use <code>INPUT_PULLUP</code> mode (in <code>INPUT_PULLUP</code> mode the line is connected to +5V through a "pull-up resistor" so that it reads HIGH until you press the button, and then it reads LOW. If you don't use either an input pull-up or pull-down resistor then the input tends to "float" and give bad readings.)</p>

<p>You say "The LEDs are supposed to light before I press the button. But the LEDs start immediately, even without pressing the button."</p>

<p>I'm not sure what you mean. What do you want to happen, exactly? Do you want the LEDs to be lit steadily, and then turn off or flash when you press the button?</p>

<p>The code below will turn off all the LEDs initially, then toggle between off and flashing on each (debounced) button press. Switch the value of <code>idleLEDState</code> to HIGH if you want the LEDs to be lit when they are not flashing.</p>

<pre><code>#define flashDelay 50 //Delay between on and off state for LEDs
#define switchBounceDelay 100


uint8_t idleLEDState = LOW; //Change to HIGH 
                            //if you want the LEDs to be lit when not flashing
bool flashLEDs = false;
bool ledsOn = false; 
unsigned long nextFlashTime = 0;
unsigned long nextSwitchCheckTime = 0;

void setup() {
  pinMode(sw7, INPUT_PULLUP); //Connect the switch between sw7 and ground
  for (int opin=1;opin&lt;6;opin++)
  {
    pinMode(opin,OUTPUT);
    digitalWrite(opin, idleLEDState); 
  }
}

void loop() {

  //If it's time to check the switch, and it's being pulled LOW, toggle flashLEDs
  if (mills() &gt; nextSwitchCheckTime &amp;&amp; digitalRead(sw7) == LOW) {
    flashLEDs = !flashLEDs;
    nextSwitchCheckTime = millis() + switchBounceDelay;
  }

  if (!flashLEDs) {
    //IF we're not supposed to flash the LEDs, set them to their idle state 
    for (int opin=1;opin&lt;6;opin++) {
        digitalWrite(opin, idleLEDState); 
     }
  } else if millis() &gt; nextFlashTime {
    nextTime = millis() + flashDelay;
    ledsOn = !ledsOn;
    for (int opin=1;opin&lt;6;opin++) {
        uint8_t newState = ledsOn ? HIGH : LOW;
        digitalWrite(opin, newState); 
     }
}
</code></pre>

<p>I banged out the code above in the Stack Exchange editor. It likely contains a few typos. Please exert some effort to fix it and adapt it to your needs before posting back "Your code didn't work. I got error XYZ."</p>
