How can I get 2 second delay on servo after pressing button?

Hello!
I am new to programming. How can i get 2 second delay on servo after I press the button? If i press button servo moves, but how can I make it move after 2 seconds?
Thanks for everyone!
Here is existing code:

#include <Servo.h>

Servo myservo;

#define pinservo 3

#define PinTombol 2

int sudut =0;

int sudutStep =10;

const int minSudut = 0;

const int maxSudut = 90;

const int type =2;

int TekanTombol =0;

void setup() {

Serial.begin(9600);

myservo.attach(pinservo);

pinMode(PinTombol,INPUT_PULLUP);

Serial.println("Tombol Servo ");

myservo.write(sudut);

}

void loop() {

if(digitalRead(PinTombol) == LOW){

TekanTombol = 1;

}

if( TekanTombol ){

sudut = sudut + sudutStep;

if (sudut >= maxSudut) {

sudutStep = -sudutStep;

if(type ==1)

{

TekanTombol =0;

}

}

if (sudut <= minSudut) {

sudutStep = -sudutStep;

if(type ==2)

{

TekanTombol =0;

}

}

myservo.write(sudut);

Serial.print("Bergerak ke: ");

Serial.print(sudut);

Serial.println(" Derajat");

delay(10);

}

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

Hello gonga999

Modify

delay(10);

to

delay(2000);

Have a nice day and enjoy coding in C++.

Your question and your code has the right size to introduce you as a beginner with programming-technique

Your code is executing a certain sequence of steps

  1. Check if the button is pressed
  2. if button is pressed calculate new servo-angle
  3. wait 2 seconds before moving the servo
  4. move servo to new position

if step 4 has finished go on with step 1 again

This could be seen as 4 different modes of operation
mode 1. Check if the button is pressed
mode 2. if button is pressed calculate new servo-angle
mode 3. wait 2 seconds before moving the servo
mode 4. move servo to new position

if mode 4 has finished go on with mode 1 again

in the programming-world the term "state" is established for this kind of operationmodes
So in programming terms
state 1. Check if the button is pressed
state 2. if button is pressed calculate new servo-angle
state 3. wait 2 seconds before moving the servo
state 4. move servo to new position

if state 4 has finished go on with state 1 again

this is how the code in the linked WOKWI-Simulation works

Now I'm curious what questions you have about this demo-code

Here is a variant of the code above which has an additional button for some kind of singlestep-mode
This means at certain lines of the code you have to press the yellow button to make the code proceed the next step (the next state)
This shall make visible how the code is jumping in and jumping out of function myServoStateMachine()

best regards Stefan

Hi again!
I have one question more.
If i press button servo moves and moves automatically back, but how can I make it move 2 seconds after pressing a button. Like servo started at 0* i press button it ends in 90* and comes back to 0*. How can I make it move 2 seconds after pressing button?

To which code does your question refer?
If you try the code I have posted in the WOKWI-simulation it does exactly what you want

best regards Stefan

Thank you!

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