Pages: [1]   Go Down
Author Topic: trying to have multiple delays?  (Read 410 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as of right now i have my arduino uno programmed to make the the servo move every 3 min. what i want to do though is after the servo has moved 12 times, i want it to stop for 10 hours and then start up again and does this in an endless loop till the power is unpluged

Code:
#include <Servo.h> //include the servo libary

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 15; pos < 85; pos += 85)  // interverls
{                                 
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(180000);                       // waits 3 minutes for the servo to reach the position
}
for(pos = 85; pos>=15; pos-=85)     // max rotation
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(500);                       // waits .5 second for the servo to reach the position
}
}
« Last Edit: September 17, 2011, 06:33:39 am by bengrvr » Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 223
Posts: 13894
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


First move the movement code from your sketch to a separate function. I called it move().

Then the code in loop() becomes relative simple,

(not tested myself)
Code:
#include <Servo.h> //include the servo libary

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

unsigned long tick = 0;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}



void loop()
{
  for (inti=0; i< 12; i++) move();
  delay(10L * 60L * 60L * 1000L);  // 10 hours but add an L to make it a long !!
}


void move()
{
  for(pos = 15; pos < 85; pos += 85)  // interverls
  {                                 
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(180000);                       // waits 3 minutes for the servo to reach the position
  }
  for(pos = 85; pos>=15; pos-=85)     // max rotation
  {
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(500);                       // waits .5 second for the servo to reach the position
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

theres a problem with the code the highlighted part


 #include <Servo.h> //include the servo libary

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

unsigned long tick = 0;

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}



void loop()
{
  for (inti=0; i< 12; i++) move();
  delay(10L * 60L * 60L * 1000L);  // 10 hours but add an L to make it a long !!
}


void move()
{
  for(pos = 15; pos < 85; pos += 85)  // interverls
  {                                 
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(180000);                       // waits 3 minutes for the servo to reach the position
  }
  for(pos = 85; pos>=15; pos-=85)     // max rotation
  {
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(500);                       // waits .5 second for the servo to reach the position
  }
}
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3734
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's just a typo - he warned you it was not tested- should be int i
Logged

Pages: [1]   Go Up
Jump to: