millis() and micros() instead of delay.

Good day !
Need support!
Question 1: Can I use micros() the same way as millis() when I want to delay some process ?

the second question is the code:
I want to remove all delay from this code, tried to do this but failed. Servo motor is not spinning.
(As You can see this is servo control code)

int servo = 9;
int angle;
int puls;
int pot = A3;

void setup()
{
pinMode(servo, OUTPUT);
pinMode(pot, INPUT);
Serial.begin(9600);
}

void servoPuls(int servo, int angle)
{
puls = (angle * 10) + 600;
digitalWrite(servo, HIGH);
delayMicroseconds(puls);
Serial.println(angle);
}
void loop()
{
int val = analogRead(pot);
angle = map(val , 0, 1023, 10, 170);
servoPuls(servo, angle);
delay (20);
}

A pulse is usually defined by two transitions, not one.

Servo motor is not spinning.

I see that you are not using the Servo library. What type of servo do you have ?

Standard servos expect a continuous stream of pulses of variable width. One digitalWrite to HIGH which is never set back LOW again isn't going to do the job.

Steve

Installer:
Question 1: Can I use micros() the same way as millis() when I want to delay some process ?

Yes

the second question is the code:
I want to remove all delay from this code, tried to do this but failed. Servo motor is not spinning.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

And Using millis() for timing. A beginners guide may also be useful.

(As You can see this is servo control code)

Why not use the Servo library?

…R

A bit advanced, but this demonstrates using micros() and millis() in a structure.

//Blink without Delay skeleton example using a structure.
//Times in either milli seconds or micro seconds
//LarryD
 
//LED wiring options
//=============================================
 
//#define PlusEqualsON //uncomment to invert
 
#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON  HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON  LOW
#define ledOFF HIGH
 
#endif
 
//======================================================================
struct timer
{
    //lastMillis = the time this "timer" was (re)started
    //waitMillis = delay time (mS) we are looking for
    //restart    = do we start "this timer" again and again
    //enableFlag = is "this timer" enabled/allowed to be accessed
    //timeType   = true = millis(), false = micros()
    //**********************
    //For each timer object you need:
    //Example:
    //   timer myTimer = //give the timer a name "myTimer"
    //   {
    //     0, 200UL, true, true, true  //lastMillis, waitMillis, restart, enableFlag, timeType
    //   };
    // You have access to:
    // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.timeType, myTimer.CheckTime()
    //**********************
 
    unsigned long lastMillis;
    unsigned long waitMillis;
    bool          restart;
    bool          enableFlag;
    bool          timeType;
 
    unsigned long currentTime;
 
    bool CheckTime() //Delay time expired function "CheckTime()"
    {
      if (timeType == true)
      {
        currentTime = millis();
      }
      else
      {
        currentTime = micros();
      }
 
      //is the time up for this task?
      if (enableFlag == true && currentTime - lastMillis >= waitMillis)
        //Note: if delays of < 2 millis are needed use micros()
      {
        //should this start again?
        if (restart)
        {
          //get ready for the next iteration
          lastMillis = currentTime;
        }
        //time was reached
        return true;
      }
      //time was not reached
      return false;
 
    } //END of CheckTime()
 
}; //END of structure timer
//======================================================================
 
 
//**********************************************************************
//Let's create 6 timer objects and initialize them in this sketch
//**********************************************************************
timer pin13 = //create timer pin13 
{
  0, 200000UL, true, true, false //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
timer pin12 = //create timer pin12
{
  0, 3000UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
timer pin11 = //create timer pin11
{
  0, 10000UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
timer pin10 = //create timer pin10
{
  0, 6000UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
timer Toggle10 = //create timer Toggle10
{
  0, 100000UL, true, true, false //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
timer checkSwitches = //create timer checkSwitches
{
  0, 50UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
//***************************
 
byte lastMySwitchState = 1; //for mySwitch on Pin 2
byte counter           = 0;
 
const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9  =  9;
 
const byte mySwitch = 2;
 
//**********************************************************************
 
void setup()
{
  Serial.begin(9600);
 
  pinMode(Pin13, OUTPUT);
  pinMode(Pin12, OUTPUT);
  pinMode(Pin11, OUTPUT);
  pinMode(Pin10, OUTPUT);
  pinMode(Pin9,  OUTPUT);
 
  digitalWrite(Pin13, LOW);
  digitalWrite(Pin12, LOW);
  digitalWrite(Pin11, LOW);
  digitalWrite(Pin10, LOW);
  digitalWrite(Pin9,  LOW);
 
  pinMode(mySwitch, INPUT_PULLUP);
 
 
} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<
 
 
void loop()
{
  //Below are examples demonstrating different timing situations
 
  //***************************
  //example 1    Toggle Pin13 every 200,000us
  if (pin13.CheckTime())
  {
    //Toggle Pin13
    digitalWrite(Pin13, !digitalRead(Pin13));
 
    //if you only want this section of code to happen once
    //uncomment the next line
    //pin13.enableFlag = false;
  }
 
  //***************************
  //example 2    After 3 seconds, Pin12 goes and stays HIGH
  if (pin12.CheckTime())
  {
    //Pin12 HIGH now
    digitalWrite(Pin12, HIGH);
    //disable timing section of code
    pin12.enableFlag = false;
  }
 
  //***************************
  //example 3    Pin11 is HIGH for 10 seconds, then goes and stays LOW
  if (pin11.enableFlag && !pin11.CheckTime())
  //if (pin11.enableFlag && !pin11.CheckTime())
  {
    digitalWrite(Pin11, HIGH);
  }
  //10 seconds is now up now, leave the Pin11 LOW
  else
  {
    digitalWrite(Pin11, LOW);
    //disable timing section of code
    pin11.enableFlag = false;
  }
 
  //***************************
  //example 4    For 6 seconds, toggle Pin10
  //if (pin10.enableFlag && !pin10.CheckTime())
  if (pin10.enableFlag && !pin10.CheckTime())
  {
    //example 5  Toggling Pin10 every 100,000us (micro seconds)
    if (Toggle10.CheckTime())
    {
      //toggle Pin10
      digitalWrite(Pin10, !digitalRead(Pin10));
    }
  }
  //6 seconds is now up, stop toggling every 100,000us (micro seconds)
  else
  {
    digitalWrite(Pin10, LOW);
    //disable timing section of code
    pin10.enableFlag = false;
  }
 
  //***************************
  //example 6    Is it time to check the switches?
  if (checkSwitches.CheckTime())
  {
    //time to read the switches
    Switches();
  }
 
  //**********************************
  //Put other non-blocking stuff here
  //**********************************
 
} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<
 
 
//======================================================================
//                      F U N C T I O N S
//======================================================================
 
 
//**********************************************************************
//switches are checked every checkSwitches.waitMillis milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches()
{
  boolean thisState; //re-usable for all the switches
 
  //****************************************** mySwitch Pin 2 code
  //check if this switch has changed state
  thisState = digitalRead(mySwitch);
  if (thisState != lastMySwitchState)
  {
    //update the switch state
    lastMySwitchState = thisState;
 
    //This switch position has changed, let's do some stuff
 
    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if (thisState == HIGH)
    {
      //example: LED on Pin9 is Push ON, Push OFF
      digitalWrite(Pin9, !digitalRead(Pin9));
    }
 
    //"LOW condition code"
    //switch goes from HIGH to LOW
    else
    {
      //example: display the current switch push count
      Serial.println(++counter);
    }
  } //END of mySwitch Pin 2 code
 
  //******************************************
  //similar code for other switches goes here
  //******************************************
 
} //END of Switches()
 
 
//======================================================================
//                        E N D  O F  C O D E
//======================================================================