Pages: 1 2 [3]   Go Down
Author Topic: Automatic Servo with Manual Override (delay problems)  (Read 1936 times)
0 Members and 1 Guest are viewing this topic.
California
Online Online
Faraday Member
**
Karma: 92
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Starting in the loop here:

Code:
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:
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:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        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.
Logged

St Johnsbury, VT
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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;
  }
}
Logged


Pages: 1 2 [3]   Go Up
Jump to: