Hello, I have been working on a door opener. The servo will open the latch with either a push button, remote or key card, a green led and buzzer comes on with an oled displaying, "Door open come in!".
This sketch was working fine. I had to dismantle it and put it back together again and it stopped working. I found out that the servo was the issue. It keeps vibrating on its own and it seems to be shorting out the IR receiver and RFID.
When I remove everything and just plug in the servo on its own, it keeps vibrating on its own. I am pluggin in groung to ground, vcc to 5V and pin 3. Here is my code.
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);
#define SS_PIN 53 // Uno pin 10 Nano pin D10 of RFID
#define RST_PIN 5 // Uno pin 9 Nano pin D9 of RFID
#define SERVO_PIN 3 //servo pin 3
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
#define LED_G 7 //led green pin 7
#define LED_R 6 //led red pin 6
#define BUZZER 4 //buzzer pin 4
#define pushButtonPin 8 //push button pin 8
int buttonPushed = 0;
decode_results results;
Servo myservo;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int IRpin = 11; //IR receiver pin 11
IRrecv irrecv(IRpin);
void setup()
{
Serial.begin(9600); // Initiate a serial communication
irrecv.enableIRIn();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
myservo.attach(SERVO_PIN);
pinMode (SDA, OUTPUT);
pinMode (LED_G, OUTPUT);
pinMode (LED_R, OUTPUT);
pinMode (BUZZER, OUTPUT);
pinMode(pushButtonPin, INPUT_PULLUP);
}
void loop()
{
//the followiing is the oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.display();
display.clearDisplay();
//start of IR SKETCH
if (irrecv.decode(&results))
{
irrecv.resume();
}
if (results.value == 3249146358) //remote control button
{
delay(500);
digitalWrite (LED_G, HIGH); //led turns on
digitalWrite(BUZZER, HIGH); //buzzer turns on
delay(500); // for half a second
digitalWrite(BUZZER, LOW); //buzzer turns off
myservo.write( 0 ); //servo goes to 0 degrees
display.setTextSize(2); //oled display's message
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Door open come in!");
display.display();
display.clearDisplay();
delay(7500); //for 7 and a half seconds
myservo.write( 100 ); //servo goes to 100 degrees
digitalWrite (LED_G, LOW); //led green turns off
}
/* start of pushbutton sketch
Everytime you press pushbutton it will turn on green led and buzzer, then servo
will go from 0 to 100 degrees for 7 and a half seconds. Then the push button
will go from 1 to 0 state (on/off).*/
if (digitalRead(pushButtonPin) == LOW) {
buttonPushed = 1;
}
if ( buttonPushed ) {
digitalWrite (LED_G, HIGH);
digitalWrite(BUZZER, HIGH);
delay(500);
digitalWrite(BUZZER, LOW);
myservo.write(0);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Door open come in!");
display.display();
display.clearDisplay();
delay(7500);
myservo.write(100);
digitalWrite (LED_G, LOW);
buttonPushed = 0;
}
//start of RFID SKETCH
// Look for new cards
if
( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("Put your card to the reader...");
Serial.println();
//Show UID on serial monitor
Serial.print("UID tag :");
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "5A 77 E2 81", "F9 31 75 9C") //change here the UID of the card
{
Serial.println("Authorized access");
Serial.println();
delay(500);
digitalWrite(LED_G, HIGH);
digitalWrite(BUZZER, HIGH);
delay(300);
digitalWrite(BUZZER, LOW);
myservo.write( 0 );
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Door open come in!");
display.display();
display.clearDisplay();
delay(7500);
myservo.write( 100 );
digitalWrite(LED_G, LOW);
}
else {
Serial.println(" Access denied");
digitalWrite(LED_R, HIGH);
digitalWrite (BUZZER, HIGH);
delay(DENIED_DELAY);
digitalWrite(LED_R, LOW);
digitalWrite(BUZZER, LOW);
}
}
When I try the example sketch for the servo it works perfectly.
//www.elegoo.com
//2018.12.19
#include <Servo.h>
/* After including the corresponding libraries,
we can use the "class" like "Servo" created by the developer for us.
We can use the functions and variables created in the libraries by creating
objects like the following "myservo" to refer to the members in ".".*/
Servo myservo;
//it created an object called myservo.
/* you can see Servo as a complex date type(Including functions and various data types)
and see myservo as variables. */
void setup(){
/*"attach" and "write" are both functions,
and they are members contained in the complex structure of "Servo".
We can only use them if we create the object "myservo" for the complex structure of "Servo".
*/
myservo.attach(3);//connect pin 9 with the control line(the middle line of Servo)
myservo.write(90);// move servos to center position -> 90°
}
void loop(){
myservo.write(90);// move servos to center position -> 90°
delay(1000);
myservo.write(60);// move servos to center position -> 60°
delay(1000);
myservo.write(90);// move servos to center position -> 90°
delay(1000);
myservo.write(150);// move servos to center position -> 120°
delay(1000);
}
Keep in mind that I am just plugging in the servo on its own before adding the led, buzzer, RFID and IR reciever.
I tried using a different arduino board and its the same issue. I am using an Elegoo Mega2560.
Any ideas?

