I need code help for my servo motor

I need help with my code. I am making an rfid door lock.
when the card is placed the door opens and a laser shines off an ldr.
when the door closes the laser shines on the ldr/ photoresistor again and the servo locks the door
the funtion i want to wright is that when the ldr’s output is low and the servo is in the 380 degree position the door should lock

i need help this is what i have done so far
image
but it is not valid

hi there

looking at your code, I assume that you need more experience. Please handle just servo motor separately. Try to learn how to drive stepper motor alone. do the same thing with LDR. You can combine the learning into one project later.

ok but i really need this project only quickly please help me

Please play with the simulation. change the code. Learn again. It is a free Arduino simulator where you can learn Arduino faster

You have to become comfortable with programming. Only then you will be able to complete your project.

1 Like

What are you expecting servo.write(380) to return, especially as the valid range of values that can be written is between 0 and 180 ?

sorry

i am a newb

ok thanks soo much
@okayelectronics

https://www.arduino.cc/en/Reference/ServoWrite

We get that, but what were you expecting/hoping that the servo.write() would do in the if test ?

i wanted to state that if there is light on the ldr and the servo is open(the door is unlocked)
then close the door.

You can test the last position that you wrote to a servo using servo.read() function but you would be better off having a variable holding the state of the door. A boolean variable named doorOpen could have a value of true or false and you can set it to false when you open or close the door so that you can test it elsewhere in the sketch

I still don’t get why you were trying to test for a value of 380 when the servo can never go to position 380

As usual, if you were to post your complete sketch it would help

1 Like

sure

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
const int photo = A0;

#define SERVO_PIN 2
Servo myservo;

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 3
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

void setup() {
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
pinMode(BUZZER, OUTPUT);
noTone(BUZZER);
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(photo, INPUT_PULLUP);
Serial.println(“Arduino RFID door lock”);
myservo.attach(SERVO_PIN);
opendoor();
}

void opendoor(){
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
myservo.write( 380 );
digitalWrite(LED_G, HIGH);
delay(3000);
}

void loop(){
digitalWrite(LED_R, HIGH);
//Look for new cards
if ( !mfrc522.PICC_IsNewCardPresent() ){
return;
}
//Select one of the cards
if ( !mfrc522.PICC_ReadCardSerial() ) {
return;
}

String content= “”;
byte letter;
for( byte i = 0; i < mfrc522.uid.size; i++ ){
content.concat(String(mfrc522.uid.uidByte[i], HEX));
if( i < mfrc522.uid.size-1 ) content+="-";
}
content.toUpperCase();
Serial.println();
Serial.println(“UID tag :’” + content + “’”);

if( content == “D4-73-CA-2A” || “D7-75-2A-7B” || “97-15-35-7B” || “D7-2C-36-7B” || “F7-F4-29-7B” || “67-72-39-7B” || “D7-7A-2C-7B” || “27-17-27-7B” || “F7-DB-37-7B” ){
Serial.println(“Authorized access”);
delay(300);
opendoor();
}

if (digitalRead(photo) == LOW && myservo.position( 380 )) {
delay(500);
myservo.write( 0 );

}

}

hope this helps @UKHeliBob

I edited the code to

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
const int photo = A0;

#define SERVO_PIN 2
Servo myservo;

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 3
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

int closingdoor = 0;

void setup() {
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
pinMode(BUZZER, OUTPUT);
noTone(BUZZER);
pinMode(LED_G, OUTPUT);
pinMode(LED_R, OUTPUT);
pinMode(photo, INPUT_PULLUP);
Serial.println(“Arduino RFID door lock”);
myservo.attach(SERVO_PIN);
opendoor();
}

void opendoor(){
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
tone(BUZZER, 800);
delay(50);
noTone(BUZZER);
delay(50);
myservo.write( 380 );
digitalWrite(LED_G, HIGH);
delay(3000);
closingdoor = 1;
}

void loop(){
digitalWrite(LED_R, HIGH);
//Look for new cards
if ( !mfrc522.PICC_IsNewCardPresent() ){
return;
}
//Select one of the cards
if ( !mfrc522.PICC_ReadCardSerial() ) {
return;
}

String content= “”;
byte letter;
for( byte i = 0; i < mfrc522.uid.size; i++ ){
content.concat(String(mfrc522.uid.uidByte[i], HEX));
if( i < mfrc522.uid.size-1 ) content+="-";
}
content.toUpperCase();
Serial.println();
Serial.println(“UID tag :’” + content + “’”);

if( content == “D4-73-CA-2A” || “D7-75-2A-7B” || “97-15-35-7B” || “D7-2C-36-7B” || “F7-F4-29-7B” || “67-72-39-7B” || “D7-7A-2C-7B” || “27-17-27-7B” || “F7-DB-37-7B” ){
Serial.println(“Authorized access”);
delay(300);
opendoor();
}

if (digitalRead(photo) == LOW && closingdoor = 1) {
delay(500);
myservo.write( 0 );
closingdoor = 0;

}

}

I may look further later but I saw this straight away

  if ( content == “D4 - 73 - CA - 2A” || “D7 - 75 - 2A - 7B” || “97 - 15 - 35 - 7B” || “D7 - 2C - 36 - 7B” || “F7 - F4 - 29 - 7B” || “67 - 72 - 39 - 7B” || “D7 - 7A - 2C - 7B” || “27 - 17 - 27 - 7B” || “F7 - DB - 37 - 7B” )

You can’t do multiple comparisons like that. You need to do

  if ( content == “D4 - 73 - CA - 2A” || content ==  “D7 - 75 - 2A - 7B” || content ==“97 - 15 - 35 - 7B”  etc, etc

and this, of course

  myservo.write( 380 );

and

  if (digitalRead(photo) == LOW && myservo.position( 380 ))

What made you think that the Servo library has a position() function ?

Oh, and because you did not post your code in code tags the forum software has changed the quotation marks in it and it won’t compile if copied

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code and use code tags

sorry error on eighth last line i changed it to closingdoor == 1

thanks

this code works fine without the laser addition

image
image


image
everything works fine but it is not doing what i want it to do
it is opening, the buzzer is ringing, everything is fine, but it is not closing when the laser comes in contact with the ldr.