Go Down

Topic: Automatic Servo with Manual Override (delay problems) (Read 2737 times) previous topic - next topic

Arrch

Starting in the loop here:

Code: [Select]
unsigned long StartVoidLoopTime = millis();

Let's say the result from millis() is 2 seconds. The button is pressed for say 8 seconds, so you're in this loop for that long:

Code: [Select]
while (digitalRead(4))
        {
          myservo.write(pos);
          delay(delaytime);
          pos +=1;
          LastTimeButtonWasPressed = millis();
        }


That means the last call to millis() will be 10 seconds (2 form when you started the loop + 8 in the loop).

Code: [Select]
if(StartVoidLoopTime - LastTimeButtonWasPressed >= interval)  //if the total time - the start time is greater than 3 sec

So what is 2 - 10 seconds? In unsigned subtraction, it's a really big number, which will most certainly be bigger than 3 seconds. You should be subtracting LastTimeButtonWasPressed from the current time, not some time you set an arbitrarily amount of time ago.

PaulS

Code: [Select]
        if (StartVoidLoopTime - LastTimeButtonWasPressed >= interval);  //if the total time - the start time is greater than 3 sec
That semicolon on the end forms the body of the if statement. A ; by itself is valid code - it is a no-op statement. Therefore, if the condition is true, nothing happens. There being no else statement with this if statement, nothing happens if the condition is not true. The whole point of the if statement - with that semicolon on the end - is what? It achieves nothing.

gschoppe

Here is a modified version of your code that I reformatted and made some significant changes to.  I have not tested it, but it should be significantly easier to read, understand, and troubleshoot:

Code: [Select]

#include <Servo.h>

Servo myservo;
const int leftButton  = 2;     //variable to represent the pin that the left button is on
const int rightButton = 4;     //variable to represent the pin that the right button is on
int caseNum = 0;               //case number to tell the servo which case to run
int pos = 0;                   //position
int delayTime = 20;            // increasing this slows down the servo movement
unsigned long lastPress = 0;   // last time update
long interval = 3000UL;        // interval at which to do something (milliseconds)

void setup() {   
  myservo.attach(9);           // attaches the servo on pin 9 to the servo object
  pinMode(leftButton, INPUT ); // the buttons are the inputs
  pinMode(rightButton, INPUT);

}

void loop() {
  if (digitalRead(leftButton)) {         //check if left button pressed
    caseNum = 2;                         //if so it switches the caseNum
  } else if (digitalRead(rightButton)) { //checks right button (else skips if left is pressed)
    caseNum = 3;
  }
 
  switch (caseNum) {
    case 0:
      while( pos < 180 ) {   // pan right until 180 degrees
        myservo.write(pos);  // Move to next position
        delay(delayTime);    // Short pause to allow it to move
        pos++;               // increment
        if (digitalRead(leftButton) || digitalRead(rightButton))  // checks to see if either button is down
          break;             // break out of subloop to check inputs
      }
      caseNum = 1;  // if no buttons pressed, default to pan left
      break;
    case 1:
      while(pos > 0) {       // pan right until 0 degrees
        myservo.write(pos);  // Move to next position
        delay(delayTime);    // Short pause to allow it to move
        if (digitalRead(leftButton) || digitalRead(rightButton))  // checks to see if either button is down
          break;             // break out of subloop to check inputs
      }
      caseNum = 0;  // if no buttons pressed, default to pan right
      break;
    case 2:
      while (digitalRead(leftButton)) { //while the button is being pressed
        pos-=1;                         //position becomes one less
        if( pos < 0 ) pos = 0;          // only necessary if you want to restrict motion to 180
        myservo.write(pos);             //rotates servo to its current positon
        delay(delayTime);
        lastPress = millis();
      }
      if(millis() - lastPress >= interval)  // if it has been interval since last press
        caseNum = 0;                        // return to expected pan sequence
      break;     
    case 3:
      while (digitalRead(4)) {
          pos+=1;                         // position becomes one greater
          if( pos > 180 ) pos = 180;      // only necessary if you want to restrict motion to 180
          myservo.write(pos);
          delay(delayTime);
          lastPress = millis();
      }
      if ( millis() - lastPress >= interval)  // if it has been interval since last press
        caseNum = 1;
      break;
    default:
      caseNum = 0;
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy