Pages: [1]   Go Down
Author Topic: trouble with timed delays  (Read 305 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 64
Posts: 2484
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This may work:

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

Code:
#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)
« Last Edit: May 28, 2013, 02:22:06 am by LarryD » Logged

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6155
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

...R
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4018
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: