How to properly use an If statement

I’m very new to programming things, so I’m probably making a simple mistake. I only want a servo to execute the “sweep” example program only when I’m getting an input on pin 2, but it’s always moving no matter what. Here’s my code, how can I fix this?

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position
int tripped = 0;
const int tripwire = 2;

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(tripwire, INPUT);

}

void loop() {
tripped = digitalRead(tripwire);
Serial.print(tripped);
if (tripped == HIGH) {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
delay(15);
}
}

What do you have wired to pin 2 and how to you have it connected?

What does your Serial.print(tripped); line produce on the Serial Monitor?

Right now I have pin 2 hooked up to a button. When it is not pushed the monitor says 0 and when it is pushed the monitor says 1

I’ve fixed it though I’m unsure how, here’s my new code:

/* Sweep
by BARRAGAN http://barraganstudio.com
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position
int tripped = 0;
const int tripwire = 2;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(tripwire, INPUT);
Serial.begin(9600);
}

void loop() {
tripped = digitalRead(tripwire);
Serial.println(tripped);
if (tripped == HIGH) {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);

}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
// waits 15ms for the servo to reach the position
} }
}

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]


   if (tripped == HIGH) {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);
    
  }

You may want to use switch state change detection instead of switch level.


Did you want the servo to sweep one way while the switch is being pressed then the other way when released ?

If so, is that what you are telling it to do ?


Do you know what the ‘else’ clause does ?


FYI

If you have it wired up to ground, when it is not pressed, it is floating and you need to declare the pin as INTPUT_PULLUP so that it will read HIGH when not pressed and LOW when pressed.