Go Down

Topic: trouble with timed delays (Read 412 times) previous topic - next topic

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.

Code: [Select]
#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

LarryD

#1
May 28, 2013, 08:35 am Last Edit: May 28, 2013, 09:22 am by LarryD Reason: 1
This may work:

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

Code: [Select]
#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)
The way you have it in your schematic isn't the same as how you have it wired up!

Robin2

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

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

...R

UKHeliBob


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 code
Code: [Select]
if(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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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.

Arrch

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:

Code: [Select]
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.

Go Up