Servo.write() not working on certain part of the code

Hello I'm new to Arduino and currently working on a project. It is basically a mailbox that can be lock or unlock then can sanitize the mail or small packages.

The board I am using is Arduino UNO R3 SMD Clone
The servo is MG 996R (2x)

Coding is done using arduino's IDE

The project is simple when 1 choose 1 from the serial monitor it either lock or unlock a door
then if I choose 2 the program will tell a 2nd servo to push a sanitizer so it will spray a package.

The problem is after the If. The write() command doesn't seem to work.

#include <Servo.h>

Servo lock;
Servo sanitizer;
int choice = 0;
int pos = 0; //pos and pos90 is to declare 0 position and 90 position. Wasn't sure should i do this or not
int pos90 = 90; 
boolean locked; //To tell whether the door was locked or not

void setup() {
  lock.attach(9);//Lock servo connected to pin 9
  lock.write(pos); //To make the servo back to the natural position
  sanitizer.attach(8);//Sanitizer servo connected to pin 8
  sanitizer.write(pos); //To make the servo back to the natural position
  Serial.begin(9600);
  Serial.println(F("Smart mail box")); //Menu
  Serial.println(F("1. Lock/Unlock Door"));
  Serial.println(F("2. Semprot disinfectan"));
  Serial.println(F("3. Exit"));
}

void loop() {
  while (Serial.available() == 0) { //Wait until there is an input

  }
  choice = Serial.parseInt(); //Receive an integer input
  if (choice == 1) { //This part is for locking and unlocking. Working fine as intended
    Serial.println("Choice = 1");
    if (locked == false) {
      lock.write(pos90);
      locked = true;
    }
    else if (locked == true) {
      lock.write(pos);
      locked = false;
    }
  }
  else if (choice == 2) { //The sanitizing part. The servo should move to 90 then back 
    Serial.println("Choice = 2");
    sanitizer.write(pos90);
    sanitizer.write(pos);
  }
}

Choice 2 got printed on the serial but no movement for the sanitizer servo

Some thing I have tried is switch out both pin from the lock and the sanitizer servo. But the sanitizer servo work just fine with the code for the lock servo but the lock servo does not move.

Then I tried changing the sanitizer.write(pos90) to lock.write(pos90) and the servo still does not move.

Power is also should be fine since I was able to turn on both servo no problem.

another thing I tried is modifying the first choice to this for debug

  if (choice == 1) { //This part is for locking and unlocking. Working fine as intended
    Serial.println("Choice = 1");
    if (locked == false) {
      lock.write(pos90);
      sanitizer.write(pos90);
      locked = true;
    }
    else if (locked == true) {
      lock.write(pos);
      sanitizer.write(pos);
      locked = false;
    }
  }
  else if (choice == 2) { //The sanitizing part. The servo should move to 90 then back 
    Serial.println("Choice = 2");
    sanitizer.write(pos90);
    sanitizer.write(pos);
  }

Both servo move at the same time to their position according to the program.
So my problem is just with the choice = 2. I really have no clue what happen

I also use switch case before using IF ELSE and still the same problem.

EDIT: Problem is solved. @UKHeliBob explained why. Thank You all for helping!

    sanitizer.write(pos90);
    sanitizer.write(pos);

Neither the servo or the code will wait until the servo gets to 90 degrees before moving to 0 degrees. The effect will be that the servo does not appear to move

how long should you spray?

Hmm so both of them need to separated?[quote="UKHeliBob, post:2, topic:869443, full:true"]

    sanitizer.write(pos90);
    sanitizer.write(pos);

Neither the servo or the code will wait until the servo gets to 90 degrees before moving to 0 degrees. The effect will be that the servo does not appear to move
[/quote]

Just one tap. It will be using a simple hand sanitizer.

how long is a tap ? do a few nano-second count as a tap? :slight_smile: can your servo move that fast?

(@UKHeliBob gave you some explanation)

hmmm I don't really specify how fast I want. I'm just letting the servo move it's own speed from 0 - 90 then back to 0 again. :grinning_face_with_smiling_eyes:

Oh and yea maybe I'm gonna try the for loop to separate the code thanks to @UKHeliBob

Try this and see if its too long, if so shorten the time.

Ah thanks.... So delay also works... Thank you!

Not my idea, @UKHeliBob mentioned it in post #2.

Yeah but you made me aware that delay can be use to separate the code. I was gonna try for loop with pos++ until 90 and then pos--. So thanks!