RFID with Limit Switch

Hi friends, i need help about programming here my project is using RFID to Active Motor Servo and waiting until limit switch pressed then Motor servo turn back, i need to use Limit swicth for change the color of led. Here is my primary code :

void loop () 
{
  
  int successRead;  
  do 
  {
    successRead = getID(); 
  }
  
  while (!successRead); 
  
  if (isMaster(readCard)) // if card match 
  { 
    Serial.println("Go in");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
    digitalWrite(b, LOW); // LED Blue change to...
    digitalWrite(g, HIGH); // LED Green on
    beep3(buzz); // buzz on
    c=90;      //servo angle
    d=40;       //delay servo 
     
     for (a=0;a<=c;a+=5)
      {
        servo1.write(a); 
        delay(d);
      }
   
 //LIMIT SWITCH --<<< Here is my problem 
    val = digitalRead(inLimit);    
    Serial.println(val);    
      if(val == HIGH){
       Serial.println(val);
       Serial.println("test");
         for (a=c;a>0;a-=5) // servo turn back 
          {
            servo1.write(a); 
            delay(d);
          }
        digitalWrite(b, HIGH); // LED Blue again
        digitalWrite(g, LOW); // Green off  
      }   
  }
 
  else // if card not match
  {
    Serial.println("Warning");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
    digitalWrite(r, HIGH); // LED red active,
    digitalWrite(b, LOW);
    beep2(buzz); // Buzzer long,
    digitalWrite(b, HIGH); // then LED blue On again
    digitalWrite(r, LOW); // LED Red off 
  }
}

My problem is in execution for Limit Switch , when i give my

  1. RFID card matched
  2. LED from BLUE change to GREEN ON
  3. then LED GREEN ON and waiting until the Limit Switch is pressed
  4. When i press Limit switch thats nothing change (i mean still in LED GREEN ON)
  5. I try to press limit switch again still nothing changes.

Thats my problem, maybe anyone can help me about my script. Thanks for attention. Sry about my english is not good.

Hi,
Welcome to the forum.

Please post your complete code please.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please post your complete code please.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Here is my complete code Mr. Tom ,

#include <EEPROM.h> // Library EEPROM
#include <SPI.h> // Library SPI 
#include <MFRC522.h> // Library MFRC522
#include <Servo.h> // Library Servo

boolean match = false;
boolean programMode = false;
byte storedCard[4];
byte readCard[4];
byte masterCard[4] = {0x47,0x32,0x13,0x3B}; // UID Card with : 47 32 13 3B
byte masterCard2[4]={0x72,0xA1,0xB6,0xED};
byte gantungan[4]={0x39,0xCD,0x5A,0x9E};
int r = 4; // Pin Red  LED RGB
int g = 3; // Pin Green  LED RGB
int b = 2; // Pin Blue LED RGB
int a,c,d;
int buzz = 5; // Pin data  buzzer
Servo servo1; // Servo name "servo1"
int inLimit = 7; //limit switch pin
int val = 0;

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600); 
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("===== KEY AUTOMATIC WITH RFID =====");
  Serial.println("");
  Serial.println("Scan your CARD !");
  Serial.println("");
  pinMode(buzz, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(inLimit, INPUT);
  servo1.attach(6);
  digitalWrite(b, HIGH);
  servo1.write(20);
}

void loop () 
{
  
  int successRead;  
  do 
  {
    successRead = getID(); 
  }
  
  while (!successRead); 
  
  if (isMaster(readCard)) // card = match?
  { 
    Serial.println("Go in");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
    digitalWrite(b, LOW); // LED blue OFF
    digitalWrite(g, HIGH); // LED Green ON
    beep3(buzz); // Buzzer active
    c=90;      //servo angle
    d=40;       //speed servo
     
     for (a=0;a<=c;a+=5)
      {
        servo1.write(a); 
        delay(d);
      }
    delay(5000); // delay 5s
    //LIMIT SWITCH <<<<<<<<<<<<<< Here is my problem <<<<
    val = digitalRead(inLimit);    
    Serial.println(val);    
      if(val == HIGH){
       Serial.println(val);
       Serial.println("test");
         for (a=c;a>0;a-=5)// motor servo turn back
          {
            servo1.write(a); 
            delay(d);
          }
        digitalWrite(b, HIGH); // LED Blue is ON again
        digitalWrite(g, LOW);  
      }   
  }
 
  else // not match CARD :
  {
    Serial.println("Warning !!");
    Serial.println("");
    Serial.println("===================================================");
    Serial.println("");
    digitalWrite(r, HIGH); // LED red ON,
    digitalWrite(b, LOW);
    beep2(buzz); // Buzzer is active
    digitalWrite(b, HIGH); // LED Blue ON again.
    digitalWrite(r, LOW);
  }
}

int getID() 
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  { 
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return 0;
  }
  
  Serial.print("ID NUMBER : ");
    for (byte i = 0; i < mfrc522.uid.size; i++) {  
      readCard[i] = mfrc522.uid.uidByte[i];
      Serial.print(readCard[i], HEX);
    }
  Serial.println("");
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
}

void readID( int number ) {
  int start = (number * 4 ) - 3;
    for ( int i = 0; i < 4; i++ ) {
      storedCard[i] = EEPROM.read(start+i);
    }
}

boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != NULL ) {
    match = true; 
      for ( int k = 0; k < 4; k++ ) { 
        if ( a[k] != b[k] )
        match = false;
      }
  }
  if ( match ) { 
    return true; 
  }
  else  {
    return false; 
  }
}

boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  else if ( checkTwo( test, masterCard2 ) )
    return true;
  else if ( checkTwo( test, gantungan ) )
    return true;
  else
    return false;
}

void beep2(int x) // long beepbeep
{
  digitalWrite(x, HIGH);
  delay(1000);
  digitalWrite(x, LOW);
  delay(500);
  digitalWrite(x, HIGH);
  delay(1000);
  digitalWrite(x, LOW);
  delay(500);
  digitalWrite(x, HIGH);
  delay(1000);
  digitalWrite(x, LOW);
  delay(500);
  digitalWrite(x, HIGH);
  delay(1000);
  digitalWrite(x, LOW);
  delay(500);
}

void beep3(int x) // short beep
{
  digitalWrite(x, HIGH);
  delay(50);
  digitalWrite(x, LOW);
  delay(50);
  digitalWrite(x, HIGH);
  delay(50);
  digitalWrite(x, LOW);
  delay(50);
  digitalWrite(x, HIGH);
  delay(50);
  digitalWrite(x, LOW);
  delay(50);
  digitalWrite(x, HIGH);
  delay(50);
  digitalWrite(x, LOW);
  delay(50);
}

Wait i forget about my hand drawn circuit ,sorry Mr. Tom

Here is my schematic guys
https://postimg.org/image/6jzp7dszn/

Hi,
OPs Diag

Hmmm...
Tom... :o

Hi,
Its not the easiest to read, and you have no current limit resistors on the LEDs.

A picture of a hand drawn schematic would be better, keeping gnd wire along the bottom of the diagram and 5V across the top.
The layout should not have diagonal wires.
Can you tell us your electronics, programming, arduino, hardware experience?

Tom.... :slight_smile:

TomGeorge:
Hi,
Its not the easiest to read, and you have no current limit resistors on the LEDs.

A picture of a hand drawn schematic would be better, keeping gnd wire along the bottom of the diagram and 5V across the top.
The layout should not have diagonal wires.
Can you tell us your electronics, programming, arduino, hardware experience?

Tom.... :slight_smile:

Hehe sorry i will repair that, i make that instanly with fritzing so sorry. I first time using arduino uno , and newbie electronics, programming and hardware .
Yeah i forget about resistor in my LEDs RGB , but LED is using for indicator RFID is read for right CARD.

    //LIMIT SWITCH <<<<<<<<<<<<<< Here is my problem <<<<

That shows WHERE the problem is. It says nothing about WHAT the problem is.

What IS the problem?

The resistor with the limit switch is useless. It is not pulling the pin UP to HIGH, or DOWN to LOW.

You appear to have pin D2 connected to pin D4, but the code appears to be trying to use them independently.