Go Down

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

landis

#5
Apr 26, 2013, 02:01 am Last Edit: Apr 26, 2013, 02:03 am by landis Reason: 1
So now that I want my function to know when the last time I pushed the button, I should put it under the my while "button is being pressed loop" So then once the button stops being pressed it takes the time at that point.
Something like this?
Top Code:
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 LastTimeButtonWasPressed; // last time update
int endTime = 2000; // interval at which to do something (milliseconds)

void setup()
...

Case 2 Code:
Code: [Select]
   
case 2:
    while (digitalRead(leftbutton))  //while the button is being pressed
    {
        myservo.write(pos);  //rotates servo to its current positon
        delay(delaytime);
        pos-=1;  //position becomes one less
    }
LastTimeButtonWasPressed = millis();
        myservo.write(pos);  //after the button stops being pressed the servo is at pos
        if(currentmillis - LastTimeButtonWasPressed >= endTime)  //if the total time - the start time is greater than 3 sec
        {         
          //casenum = 1;  //this is where the code goes wrong, it seems like it reads this if statement
                        //and immedietly say thinks its true
          //previousmillis = currentmillis;
          //interval = millis() +;
          break;
        }

The comments are there because I am not sure what to do with that code.

With the current code it is not pausing at all.

Arrch

Try moving the assignment statement inside the while loop. Outside of it, its still not being affected by the button state.

landis

#7
Apr 26, 2013, 04:11 am Last Edit: Apr 26, 2013, 04:14 am by landis Reason: 1
by putting the statement inside the while loop like so
Code: [Select]
case 2:
    while (digitalRead(leftbutton))  //while the button is being pressed
    {
        myservo.write(pos);  //rotates servo to its current positon
        delay(delaytime);
        pos-=1;  //position becomes one less
        LastTimeButtonWasPressed = millis();
    }
    myservo.write(pos);  //after the button stops being pressed the servo is at pos


It still does not delay.

However, I was playing around with this code and when I did this
Code: [Select]
case 2:
    while (digitalRead(leftbutton))  //while the button is being pressed
    {
        myservo.write(pos);  //rotates servo to its current positon
        delay(delaytime);
        pos-=1;  //position becomes one less
    }
        myservo.write(pos);  //after the button stops being pressed the servo is at pos
        previousmillis = millis();
       
        if(millis() - 5000 >= 3000UL)  //if the total time - the start time is greater than 3 sec
        {         
          //casenum = 1;  //this is where the code goes wrong, it seems like it reads this if statement
                        //and immedietly say thinks its true
          //previousmillis = currentmillis;
          //interval = interval + millis();
          break;
        }

The code paused. But only for around 5 seconds.
There is also a time slot where it will delay and then it will just stand still after I press the button
and I am trying to get the significance of this, but I just can't figure it out

Arrch

#8
Apr 26, 2013, 04:23 am Last Edit: Apr 26, 2013, 04:25 am by Arrch Reason: 1
In the first piece of code, where is the if statement that determines how long it has been since LastTimeButtonWasPressed?

In the second piece of code, simply algebra would give you

Code: [Select]

if (millis() >= 8000)


Which says "if it has been 8 seconds since the processor was turned on or reset..."

I don't see how that's useful...

landis

for the first code
Code: [Select]
while (digitalRead(leftbutton))  //while the button is being pressed
    {
        myservo.write(pos);  //rotates servo to its current positon
        delay(delaytime);
        pos-=1;  //position becomes one less
    }
        myservo.write(pos);  //after the button stops being pressed the servo is at pos
        previousmillis = millis();
       
        if(millis() >= 8000UL)  //if the total time - the start time is greater than 3 sec
        {         
          //casenum = 1;  //this is where the code goes wrong, it seems like it reads this if statement
                        //and immedietly say thinks its true
          //previousmillis = currentmillis;
          //interval = interval + millis();
          break;
        }


but for the second part, I thought the same thing, but with the -5000 on the left side of the inequality  delays the panning for 5secs which doesn't make since to me

Go Up