pushbutton to start a loop

hi i need your help ,i have a UNO . My circuit is very simple , 3 Leds on diferent output (3,4,5my cercuit is working but ) i want a pushbutton to start the loop that i created , so i puted a pushbutton , i attached one side to the power ans the other side to digital pin 2 on the arduino i puted the 10kohm resistor from the ground to the switch pin that conect to the arduino , is there any simple way to add code to do this

here is my code

void setup() {


pinMode(3 ,OUTPUT);
pinMode(4 ,OUTPUT);
pinMode(5 ,OUTPUT);
pinMode(2 ,INPUT);
}

void loop() {

  delay(500);
  digitalWrite(3 ,1);
  delay(100);
 digitalWrite(3 ,0);
  delay(100);
  digitalWrite(5 ,1);
  delay(100);
  digitalWrite(5 ,0);
  delay(150);
  digitalWrite(4 ,1);
  delay(100);
  digitalWrite(4 ,0);
    delay(100);
  digitalWrite(4 ,1);
  
  delay(100);
  digitalWrite(4 ,0);
}

use "while" condition

Could you help me using while condition in my code ?

clamb3rt: Could you help me using while condition in my code ?

In setup()

   while (! digitalRead(2) ) { }

Explanation: This reads, in English, like this: While the result of reading the digital value of pin 2 is a 0, do nothing, then read the pin again... and again... etc.

The ! is the NOT operator. DigitalRead() returns a 0 or a 1. 0 is seen by the compiler as false, and 1 is seen as true. The two braces {...} enclose code to be executed when the while condition is met.

sorry im verry noob so my code is above and a pic of my circuit whats wrong with it

void setup() {
  
while (! digitalRead(2) ) { }

pinMode(3 ,OUTPUT);
pinMode(4 ,OUTPUT);
pinMode(5 ,OUTPUT);
pinMode(2 ,INPUT);
}

void loop() {

  delay(500);
  digitalWrite(3 ,1);
  delay(100);
 digitalWrite(3 ,0);
  delay(100);
  digitalWrite(5 ,1);
  delay(100);
  digitalWrite(5 ,0);
  delay(150);
  digitalWrite(4 ,1);
  delay(100);
  digitalWrite(4 ,0);
    delay(100);
  digitalWrite(4 ,1);
  
  delay(100);
  digitalWrite(4 ,0);
}

Uploaded with ImageShack.us

a while loop is OK, but you don't need it ;)

look at this example : http://arduino.cc/en/Tutorial/Button

but you could spare a resistor by connecting the button between the Input and GND, and using the internal pull-up resistor in this case, the input will be LOW if the button is pushed, so you must change the code accordingly :

int buttonstate = 0;
void setup() {
pinMode(3 ,OUTPUT);
pinMode(4 ,OUTPUT);
pinMode(5 ,OUTPUT);
pinMode(2 ,INPUT_PULLUP);   // activates the internal pull-up resistor 
}

void loop() {
   
 buttonState = digitalRead(2);  // check if button is pushed

    if(buttonstate==LOW)
    {
         delay(500);
         digitalWrite(3 ,1);
         delay(100);
         digitalWrite(3 ,0);
         delay(100);
         digitalWrite(5 ,1);
         delay(100);
         digitalWrite(5 ,0);
         delay(150);
         digitalWrite(4 ,1);
         delay(100);
         digitalWrite(4 ,0);
         delay(100);
         digitalWrite(4 ,1);  
         delay(100);
         digitalWrite(4 ,0);
   }
   else
   {
    //other instructions if needed, executed if button is not pushed 
   }
// here : other instructions if needed, always executed 
}

when i stop pushing the botton the loop finish and stop the loop check again the state and so i need to repush the button to restart the loop

Yes, that's what it is supposed to do ;)

If you want it to start when button pressed, and stop when pressed again, then you need to toggle the variable :

boolean buttonstate = false;
void setup() {
pinMode(3 ,OUTPUT);
pinMode(4 ,OUTPUT);
pinMode(5 ,OUTPUT);
pinMode(2 ,INPUT_PULLUP);   // activates the internal pull-up resistor 
}

void loop() {
   
 if(digitalRead(2)==LOW)  // check if button is pushed
    buttonstate = !buttonstate;    //reverse buttonstate value

    if(buttonstate==true)
    {
         delay(500);
         digitalWrite(3 ,1);
         delay(100);
         digitalWrite(3 ,0);
         delay(100);
         digitalWrite(5 ,1);
         delay(100);
         digitalWrite(5 ,0);
         delay(150);
         digitalWrite(4 ,1);
         delay(100);
         digitalWrite(4 ,0);
         delay(100);
         digitalWrite(4 ,1);  
         delay(100);
         digitalWrite(4 ,0);
   }
   else
   {
    //other instructions if needed, executed if button is not pushed 
   }
// here : other instructions if needed, always executed 
}

now, it should start when you press the button and run until you press it again . but I'm afraid you'll need to debounce the signal ..... have a look at the "debounce" example http://arduino.cc/en/Tutorial/Debounce

edit : Debounce example also checks that the state of the button has changed - It is important because, if it doesn't and the button is pressed longer than loop duration (here, the total duration of delay() calls = about 1,2s) , buttonstate will be reversed ;)

Hello friend can anyone HELP me please…
I want to run stepper motor as entered number of rotation each time from keypad shield lcd.
Cycle should start after pressing switch.
I have below items with me
-stepper motor of 35kgcm
Stepper driver
-keypad shield lcd display for ( input of desired number of rotation and speed)
-arduino R3
I tried much more to take function as i want by researching in internet and youtube but nowhere i can find program as i need.
So please if anyone can help me in this…
I will be very much thank full to them.
Thank you
You can also suggest me on my contact
Mo :- +917046500500
Chirag Patel