Code Implementation

Hi, I am building a pan camera system using a servo motor. I am using the servo library's sweep and knob codes but will like to do something a little different but having difficulties with the codes.

I would like to use a switch to start the sweep function but also include a little piece of code that tells the arduino that the user needs to turn of the switch in order to start the knob function.

Both sweep and knob control will be working on the same servo motor. If theres anyone that can help with the general idea of what the code will look like. Thanks

The first step will be using “BlinkWithoutDelay” techniques to re-write this part of the Sweep example to not use delay():

  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>=1; 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 
  }

After that, it’s easy.

Thanks that makes sense but the part i'm having problems with is making the arduino not run the knob function when the sweep is being run.

E.g If a user puts the switch on, the servo starts moving back and forth from 0 to 180 continously but if the user turns the knob the motor doesn't move with the pot. The knob function only works when the sweep switch is on off.

Do i use something like an "if statement"?

Maybe saying if switch = 1 then the knob code can't come on and if switch = 0 then the knob code can work if used?

kelvin8: Do i use something like an "if statement"?

Something very much like an "if statement" called and "if,else statement". :)

if (digitalRead(switchPin)) {
    // Sweep code
}
else {
   // Knob code
}

This takes advantage of the fact that LOW is defined as 0 and therefore the same as 'false' and HIGH is defined as 1 and, since 1 is not 0, evaluates to 'true'. The 'if' part of the statement executes if the expression is true and the 'else' part of the statement executes if the expression is false.