How to properly insert delay() function

Hey, so I am trying to upload a code that makes a servo motor turn 180 degrees then stop. I have got that to work, but I am trying to have it to be delayed one hour before it does. I have looked up a lot and tried on my own but I can't find how to properly insert the delay() function. The code uploads but does not delay. Thanks for any/all help I get with this. Also sorry if I don't have enough info I am taking a class I am only 13, I am kinda new with the Arduino UNO and I feel like this should be a simple fix. Thanks,

This is my code:

#include <Servo.h>

Servo servo_1; // servo controller (multiple can exist)

int servo_pin = 3; // PWM pin for servo control
int pos = 0; // servo starting position

void setup() {
servo_1.attach(servo_pin); // start servo control
}

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
delay(1000);
}
}

Ask your teacher, that is what they are paid for! Also, the code you posted will never move a servo!

I am taking Mark Robers class and I can't ask him thats why I am here and it does move the servo. No joke

@mistalobsta, your topic has beenn moved to a more suitable location on the forum. I'm not sure why you think that this relates to avrdude, bootloaders and the likes.

Can you please sped some time reading How to get the best out of this forum (again?) and apply what you learned about code tags to your opening post,

Sorry My bad mustve clicked the wrong one

Impossible. The servo does not get anything written to it in the posted code. Post the code that actually moves the servo.

Read the forum guidelines. Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

TIMER to move servo after 1 hour.


// Non blocking timing

#define enabled true
#define disabled false

//*********************************************
// declare TIMER variables
bool timingFlag = false;
unsigned long  servoMillis;
// one hour duration
unsigned long  duration = 1 * 60 * 60 * 1000ul;
//*********************************************

void setup()
{
  // do setup stuff

} // END of setup()

void loop()
{
// At some point you need to do the following:
if( timingFlag == disabled && //some kind of test )
{
   // enable the timer
   timingFlag = enabled;

   // restart the TIMER
   servoMillis = millis();
}

//******************************
// If we are timing, has the TIMER expired ?
if (timingFlag == enabled && millis() - servoMillis >= duration)
{
timingFlag = disabled;
// do what you need to do
}

} // END of loop()

Okay then something has to be wrong with mine because it does I know you are saying it is impossible but it is. Idk why people are always harsh on here. My only two topics people are rude when I mess something up

Read post #7

If you feel people are rude please use the flag below each post to bring the offending post to the attention of the moderators. Thanks.

Note that the Servo library can use any digital pin, not just the PWM-capable ones (and you should not fall into the trap of thinking that an Arduino PWM pin is suitable for driving a servo)

Please remember to use code tags when posting code.

Many helpers are extremely experienced and work here for free. Pay them respect and take their advice serious. Jumping their advice makes any helper a bit crossed. They are no young chickens being here to make time pass.
As told before, read the advising first topic, if You have not done that. Then prepare Your question according to that advice. Too often questions are posted but are not prepared at all. Questions similar to "Mama help me" only get rough answers.
All helpers really enjoy pulling the cork out of the bottle, solving the issue but facts are needed, sometimes lots of facts, data etc.

1 Like