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!