Go Down

Topic: trying to have multiple delays? (Read 459 times) previous topic - next topic

bengrvr

Sep 17, 2011, 01:29 pm Last Edit: Sep 17, 2011, 01:33 pm by bengrvr Reason: 1
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: [Select]
#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
}
}

robtillaart


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

#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
  }
}
Rob Tillaart

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

bengrvr

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
  }
}

wildbill

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy