How to have servo move frantically every so often? [fixed]

As a hands on guy and graphical programmer (MaxMSP) I find Arduino code very hard, and my brain shuts down upon googling any help files or reading tutorials.

My issue is as follows. I've repurposed the "sweep" code to be very frenetic. I like this, but I would like the seizure to stop and start for two reasons 1) it looks more dramatic, and 2) it will save the little servo from burning out after only 8 minutes of use.

Here is the code to make the mask shake (ignore the notes, those are from original example):


/* Sweep
by BARRAGAN http://barraganstudio.com
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 90; // 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 = 60; pos <= 140; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(pos = 130; pos>=50; pos-=10) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); }

}

How do I make this "go" for 30 seconds and stop for 15 seconds, looping to infinity? I'm fully aware of the elementary nature of the request, as stated before, my DNA is just not wired to comprehend text based code. I literally thought to use a 555 timer and reed relay to i/o the signal to servo from Arduino PWM pin, but I'd rather use less components if I'm already using a good micro controller.

Thanks!

IF you can't comprehend text based code, then I cannot help. Any help will be text related.
Paul

Text based advice like in the topic "How to use this Forum" is highly recommended in order to get the best support.

If you are having a servo problem, most are due to inadequate power supply (like trying to power the servo from the Arduino 5V, which can destroy the Arduino), or forgetting to connect the grounds.

4xAA battery packs work for 1 or 2 small servos. Don't forget to connect the grounds.

Thanks for the reply without sarcasm. I'm expecting 90% to be snarky, tis the nature of the field.

The servo is working just fine. I'm just looking to do time based stuff with the motion I've got going.

It was not clear from your post (especially the title) whether the servo was functioning correctly.

To run segments of code for given amounts of time, periodically, use the millis() function. A good tutorial on using the method to time blinks of an LED can be found here.

Be sure to use unsigned long variables for all times, and to obey the rule to subtract before testing expiration of a timed interval.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

jremington:
It was not clear from your post (especially the title) whether the servo was functioning correctly.

To run segments of code for given amounts of time, periodically, use the millis() function. A good tutorial on using the method to time blinks of an LED can be found here.

Be sure to use unsigned long variables for all times, and to obey the rule to subtract before testing expiration of a timed interval.
Brilliant, thank you so much!

I got it to work! Thanks for everyone that was helpful. FYI I found this tutorial on millis() that really boosted me Arduino Sketch with millis() instead of delay() - YouTube

*the "seizure" movements of the servo explained in my original post were engineered on purpose, not due to faulty motor.

Hi,
Good to hear,
Can you, for the benefit of anyone else with this problem post your working code, please?

Thanks... Tom... :slight_smile:

Yes, I can tomorrow! Again, the servo itself was never faulty - I just could not wrap my head around how to time the motor's motion on and off every twenty seconds or so.

To avoid misleading people in the future, please make sure the title of your post is clear.

You can edit that title, just like you can the body of the post.

Thanks for the tip - done!

const unsigned long eventInterval = 60000;
unsigned long previousTime = 0;

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 90;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 
{
  unsigned long currentTime = millis();
  
    for(pos = 30; pos <= 150; pos += 10)   
  {                                   
    myservo.write(pos);              
    delay(5);                       
  } 
  for(pos = 140; pos>=31; pos-= 10)     
  {                                
    myservo.write(pos);               
    delay(5);    }
    if (currentTime - previousTime >= eventInterval)
    {
      previousTime = currentTime;
      myservo.write(90);
      delay(20000);
    }
}

this code successfully puts my servo into a forty second frenzy every twenty seconds haha. I realize there may be a more elegant way of doing this without delay - maybe using a transistor to turn the servo on and off using another arduino pin, but this literally all I need my Arduino to do. Thanks for the help, glad I go it going (now back to MaxMSP, please anything but code lolll).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.