Hello,first time posting here so not sure if this is the right category excuse me if it is not.
I am trying to figure out a way to build a simple little "robot" that will move it's hand powered by an SG90 once every 10 minutes for 60 degrees and then back into its original position.
I am very new with arduino this will be my third project with it so I would like to know if anyone has done something similar and could share a schematic or code for this.
This will do what you want but like camsysca stated it is an awkward way of doing it. It pauses your program and will not allow anthing else to be performed until the 10 mins is over. Using millis() will allow you to do other things while you wait your 10 mins.
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(60);
delay(2000);
myservo.write(0);
delay(2000);
}
There's no overflow issue with delay, it's just a limiting and awkward way to go about it. Look instead at the examples using millis() for non-blocking delays.
C
@cola_bh
What do you think about the following idea?
Let there be running a Timer in the background. This Timer will notify you that 10 min time has elapsed and then you turn the Servo by 600. Wait for a while (say 100 ms) and then bring the Servo at 00 position.
Pseudo/Text Codes: 1. Create 10-min Timer or borrow from someone of this Forum or from IDE. 2. Check if 10 min has elapsed (blocking same as delay() function). OR
Let the Timer notify you that the 10 min time has elapsed (non-blocking based on interrupt strategy).
4. Turn the Servo by 60 degrees. 5. Wait for a while and turn back the Servo to original position. 6. Repeat the process for Step-2.
Solution Hints: 1. Borrowing from IDE
#include<Servo.h>
Servo myServo; //the object of type Servo
unsigned long prMillis = 0; //prMillis = present millis
prMillis = millis(); //collect the present time in ms from MillisCounter of IDE
void setup()
{
Serial.begin(9600);
myServo.attach(8); //Servo's signal is at DPin-8
}
void loop()
{
if(millis() - prMillis >= 10*60*1000ul) //ul stands for unsigned long
{
myServo.write(60);
//insert codes to wait for a while)
myServo.write(0); //back to original position
prMillis = millis(); //reload for next iteration
}
}
If you are only wanting your program to do one thing and that is to wait 10 mins and then move a servo then no it dosnt matter . But if you want to acomplish other things during that 10 min time frame then yes it matters.