Servo Programming Help

Hello, I am making a programmable airsoft target with a servo motor. I’ve included my code, as you can see, I initialize 2 buttons, but am only using 1. The first button initiates the While loop and loops the target program, what I would like to do is have the second button break this loop, can anyone give me input on how to do that? Initiating an IF statement in the loop then using the break command doesn’t seem to work. The code I have so far works with Button 1, I know my setup is correct because the buttons both work with other types of codes. I am new to Arduino Programming so let me know If I’m an idiot or making a trivial mistake. Thanks :slight_smile:

Here is my code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int buttonState = 0; // variable for reading the pushbutton status

int buttonState2 = 0; // variable for reading the pushbutton status

const int buttonPin = 2; // the number of the pushbutton pin

const int buttonPin2 = 3; // the number of the pushbutton pin

void setup() {

myservo.attach( 8 ); // attaches the servo on pin 8 to the servo object

pinMode(buttonPin, INPUT_PULLUP);

pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {

buttonState = digitalRead(buttonPin);

buttonState2 = digitalRead(buttonPin2);

while (buttonState == LOW) {

myservo.write(173); // tell target to go to position not viewable

randomSeed(millis()); //specifies unit for time function

delay(random(3000,10000)); //delays a random time between 3 and 10 seconds

myservo.write(97); // target goes to viewable position

delay(4000); // target stays in position for certain amount of time

}

Get rid of the delay()s and use millis() for timing. During the delay() nothing else happens, including reading inputs.

Look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

 while (buttonState == LOW) {
 
 
    myservo.write(173);              // tell target to go to position not viewable
   
    randomSeed(millis()); //specifies unit for time function

  delay(random(3000,10000)); //delays a random time between 3 and 10 seconds

  myservo.write(97); // target goes to viewable position

  delay(4000); // target stays in position for certain amount of time


}

If you ever do press the switch, you are f**ked, as this while loop has NO way of ending.

The randomSeed() function should be called ONCE, in setup(), NOT in loop(), and not over and over.

If you want to break out of this while loop if a switch is (or becomes) pressed, feel free to actually read the state of the switch in the loop.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile: