trying to get delay for servo

Hi there,

I am trying to get the servo to delay for 1 second after moving for 5 seconds.
how do I modify this code to do this?

#include <Servo.h>


Servo myservo;

//set potpin to analog pin A0
int potpin = 0;
long pausetime = 1000;
int val;

void setup()
{
  myservo.attach(9);
}
void loop()
{
  long currentMillis = millis();
  long previousMillis = 0;
  long interval = 1000;
  
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    
    SpeedAndDirection();
  }
  
}

void SpeedAndDirection(){
  
  //read value from A0(potpin)
  val = analogRead(potpin);

  //set val readings(ohms) to servo action(postion) 
  val = map(val, 0, 1000, 1000, 2000); 
  
  //using val, write position to servo
  myservo.writeMicroseconds(val);

  //Delay.
  delay(pausetime);
  
}

You have previous millis as a local variable that gets wiped every time round the loop. Make it either a static or a global variable type.

hey grumphy mike!

thank for replying me. how do i get about doing that?

Hint: "potpin" is a global.

what is "global"?

In scope everywhere.

nerdkidleo: what is "global"?

http://arduino.cc/en/Reference/scope

ok i tried previousMillis to global but its still not giving me the delay i want.

#include <Servo.h>


Servo myservo;

//set potpin to analog pin A0
int potpin = 0;
long pausetime = 1000;
int val;

long previousMillis = 0;

void setup()
{
  myservo.attach(9);
}
void loop()
{
  long currentMillis = millis();
  
  long interval = 1000;
  
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    
    SpeedAndDirection();
  }
  
}

void SpeedAndDirection(){
  
  //read value from A0(potpin)
  val = analogRead(potpin);

  //set val readings(ohms) to servo action(postion) 
  val = map(val, 0, 1000, 1000, 2000); 
  
  //using val, write position to servo
  myservo.writeMicroseconds(val);

  
}

but its still not giving me the delay i want.

Well it does what you wrote, that is the SpeedAndDirection function gets called every second. So what ever is your problem it is not the delay part.