trouble with timed delays

Hey all, I am trying to wrap my head around the whole timer thing instead of delays and I thought I had it understood when I came up with this. I am trying to turn a servo from 45 to 95 to 135 back to 95 and back to 45 with a .35 second delay.

#include <Servo.h>
long previousMillis = 0;        // will store last time LED was updated
long pause = 350;               //pause delay 
Servo servo;

void setup()
{
  servo.attach(9); // attach servo to pin 9
}
void loop()
{
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis) = pause)
  {
  servo.write(45);  
  }
  if(currentMillis - previousMillis = pause*2)
  {
  servo.write(90);
  }
  if(currentMillis - previousMillis = pause*3)
  {
  servo.write(135);
  }
  if(currentMillis - previousMillis = pause*4)
  {
  previousMillis = currentMillis;
    servo.write(90);
  }
}

I get the error
Skooter_01_servohead.ino: In function 'void loop()':
Skooter_01_servohead:17: error: expected primary-expression before '=' token
Skooter_01_servohead:17: error: expected `;' before ')' token
Skooter_01_servohead:21: error: lvalue required as left operand of assignment
Skooter_01_servohead:25: error: lvalue required as left operand of assignment
Skooter_01_servohead:29: error: lvalue required as left operand of assignment

thanks

This may work:

EDIT: Use == instead of = in the if statements
i.e. if(difference = pause) >>>>>> if(difference == pause)

#include <Servo.h>
long previousMillis = 0;        // will store last time LED was updated
long difference;
long pause = 350;               //pause delay 
Servo servo;

void setup()
{
  servo.attach(9); // attach servo to pin 9
}
void loop()
{
  unsigned long currentMillis = millis();
  difference =currentMillis - previousMillis;
  if(difference == pause)
  {
  servo.write(45);  
  }
  if(difference == pause*2)
  {
  servo.write(90);
  }
  if(difference == pause*3)
  {
  servo.write(135);
  }
  if(difference == pause*4)
  {
  previousMillis = currentMillis;
  }
    servo.write(90);
  }

EDIT: Use == instead of = in the if statements
i.e. if(difference = pause) >>>>>> if(difference == pause)

"if" needs to be followed by "==" and not "=".

And check that you have your brackets properly located and properly matched.

...R

brickparatrooper:
Hey all, I am trying to wrap my head around the whole timer thing instead of delays and I thought I had it understood when I came up with this. I am trying to turn a servo from 45 to 95 to 135 back to 95 and back to 45 with a .35 second delay.

I get the error
Skooter_01_servohead.ino: In function 'void loop()':
Skooter_01_servohead:17: error: expected primary-expression before '=' token
Skooter_01_servohead:17: error: expected `;' before ')' token
Skooter_01_servohead:21: error: lvalue required as left operand of assignment
Skooter_01_servohead:25: error: lvalue required as left operand of assignment
Skooter_01_servohead:29: error: lvalue required as left operand of assignment

It is always useful to Auto Format your code, not just to tidy it up but because it warns of extra left/right brackets and braces. This line was wrong in your codeif(currentMillis - previousMillis) = pause)The display of matching brackets (or not) should indicate that there is a mismatch. Incidentally, I think that jamming the if statement against the opening bracket does not help in seeing errors either.

Thank you all very much!! it was the ==. The ) was accidentally left there after I had stated to mess with the code to figure out what was wrong. Glad it was something simple, I was worried I just wasn't grasping it properly.

That's a little awkward way of doing it, and could be an issue down the road if you are doing things that might make the loop() skip a millis tick. The same thing can be accomplished with a single if statement for checking the time:

const unsigned long pause = 350;
const int numPositions = 4;
const byte positions[numPositions] = { 45, 90, 135, 90 }; 
byte positionIndex = 0;

...

if (difference >= pause)
{
  previousMillis = currentMillis;
  servo.write(positions[positionIndex]);
  positionIndex++;
  if (positionIndex >= numPositions)
    positionIndex = 0;
}

That would give you more scalability and would run even with small delays in the rest of the program.