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):
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.
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.
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.
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?
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!
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.
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).