-1

[Later Edit] I have a Arduino Uno together with an RFID-RC522, a servo motor and some LED's + I also made a aluminium foil sandwich/switch. Here is the code BEFORE THE ALUMINIUM SWITCH, with everything working perfect:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 10 // SLAVE SELECT la DIGITAL 10
#define RST_PIN 9 // PIN RESET la DIGITAL 9
#define LED_V 5 //LED_Verde la D5
#define LED_R 4 //LED_Rosu la D4
#define BUZZER 2 //BUZZER la D2
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Creaza instanta MFRC522.
Servo bariera; //defineste nume Servo

void setup() 
{
  Serial.begin(9600);   // Incepe comunicatia seriala cu PC-ul
  SPI.begin();      // Initializeaza bus-ul SPI
  mfrc522.PCD_Init();   // Initializeaza MFRC522
  bariera.attach(3); //servo pin
  bariera.write(0); //servo pozitie start
  pinMode(LED_V, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Apropie cartela...[x][][]");
  delay(2500);
  Serial.println("Apropie cartela...[x][x][]");
  delay(3500);
  Serial.println("Apropie cartela...[x][x][x]");
  delay(4500);
  Serial.println("Astept raspuns utilzator!");
  Serial.println();

}
void loop() 
{
  // Cauta cartele noi
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Selecteaza una dintre cartele
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Arata UID pe monitorul serial
  Serial.print("ID Tag/Cartela: ");
  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("Mesaj : ");
  content.toUpperCase();
  if (content.substring(1) == "A0 B9 8D 7C") //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
  {
    Serial.println("Acces autorizat! Bine ati venit!");
    Serial.println();
    delay(500);
    digitalWrite(LED_V, HIGH);
    tone(BUZZER, 350);
    delay(300);
    noTone(BUZZER);
    bariera.write(120);
    delay(5000);
    bariera.write(0);
    digitalWrite(LED_V, LOW);

  }

 else   {
    Serial.println(" Access interzis! La revedere!");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
} 

So if the RFID CARD/TAG is "A0 B9 8D 7C", LED_V lights, short buzzer tone, and servo (here named bariera) goes to 120 degrees.

If the RFID CARD/TAG is NOT "A0 B9 8D 7C", LED_R lights, long buzzer tone and serv does nothing.

I also made an aluminium foil switch (two sheets of aluminium separated by cardboard, when pressed ((digitalRead(IESIRE)== 0) it should: LED_V lights, short buzzer tone, and servo (here named bariera) goes to 120 degrees.

This is the code for the whole thing with the switch included (#define IESIRE 6):

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 10 // SLAVE SELECT la DIGITAL 10
#define RST_PIN 9 // PIN RESET la DIGITAL 9
#define LED_V 5 //LED_Verde la D5
#define LED_R 4 //LED_Rosu la D4
#define BUZZER 2 //BUZZER la D2
#define IESIRE 6 // IESIRE la D6
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Creaza instanta MFRC522.
Servo bariera; //defineste nume Servo

void setup() 
{
  Serial.begin(9600);   // Incepe comunicatia seriala cu PC-ul
  SPI.begin();      // Initializeaza bus-ul SPI
  mfrc522.PCD_Init();   // Initializeaza MFRC522
  bariera.attach(3); //servo pin
  bariera.write(0); //servo pozitie start
  pinMode(LED_V, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(IESIRE, INPUT);
  digitalWrite(IESIRE, HIGH);
  noTone(BUZZER);
  Serial.println("Apropie cartela...[x][][]");
  delay(2500);
  Serial.println("Apropie cartela...[x][x][]");
  delay(3500);
  Serial.println("Apropie cartela...[x][x][x]");
  delay(4500);
  Serial.println("Astept raspuns utilzator!");
  Serial.println();
}
void loop() 
{
  // Cauta cartele noi
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Selecteaza una dintre cartele
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Arata UID pe monitorul serial
  Serial.print("ID Tag/Cartela: ");
  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("Mesaj : ");
  content.toUpperCase();
  if (content.substring(1) == "A0 B9 8D 7C") //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
  {
    Serial.println("Acces autorizat! Bine ati venit!");
    Serial.println();
    delay(500);
    digitalWrite(LED_V, HIGH);
    tone(BUZZER, 350);
    delay(300);
    noTone(BUZZER);
    bariera.write(120);
    delay(5000);
    bariera.write(0);
    digitalWrite(LED_V, LOW);

  }

 else if (content.substring(1) != "A0 B9 8D 7C")    {
    Serial.println(" Access interzis! La revedere!");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);

 }

  else if ((content.substring(1) != "A0 B9 8D 7C") && (digitalRead(IESIRE)==0))     {
    Serial.println("Drum bun!");
    digitalWrite(LED_V, HIGH);
    tone(BUZZER, 350);
    delay(300);
    bariera.write(120);
    delay(5000);
    bariera.write(0);
    digitalWrite(LED_V, LOW);

  }
}

Please ignore the else if statements and stuff, I'm fairly new to coding and I am trying my best. So if the switch is pressed (this translates to digitalRead(IESIRE)==0) )the servo need to go to 120 degrees, regardless of the other statements.

CARD/RFID TAG present > recognised > servo to 120 + led, buzzer
                present > not recognised > led, buzzer
 IF SWITCH is pressed > servo to 120 + led, buzzer REGARDLESS of the CARD stuff.

I hope this makes sense and it's more explicit.

  • Do you have a pullup resistor on your switch? – gre_gor Jun 05 '17 at 14:24
  • I've understand that by using this `digitalWrite(IESIRE, HIGH);` activates the internal pullup resistor on the Arduino Uno, I also had another code with ` pinMode(BuclaInd, INPUT_PULLUP);` and at println BuclaInd I was getting 1 when switch not activated and 0 when switch activated, but it didn't do anything. My problem is the statements when programming, but I don't really get where I do wrong. – VegetableAnt210 Jun 05 '17 at 14:37

1 Answers1

4

Hard to tell what you mean by "the last part", but lets assume you're here:

else if (content.substring(1) != "A0 B9 8D 7C")    {
    /* stuff */
 }
 else if ((content.substring(1) != "A0 B9 8D 7C") && (digitalRead(IESIRE)==0))     {
    /* other stuff */
  }

Lets simplify that:

else if (A)    {
    /* stuff */
 }
 else if (A && B)     {
    /* other stuff */
  }

Can you see it now? If condition A is met the first bit is done and so the else if is ignored.

If the 2nd bit should always be done remove the "else if" so it is always checked. If it is a case of one or the other then put the more specific part (A && B) before the general case.

Also: for performance and dare I say clarity, don't call substr twice - store substr(...) != "..." in a variable and just use the variable. If you give it a sensible name like what != "A0 B9 8D 7C" actually means, your code becomes much clearer! You also avoid the hassle of having to change the string in 2 places should values changes etc.

edit: So from the comments: ANYTIME digitalRead(IESIRE)==0 I want this to happen: Serial.println("Drum bun!"); digitalWrite(LED_V, HIGH); tone(BUZZER, 350); delay(300); bariera.write(120); ... (etc)

You should do this:

else if (content.substring(1) != "A0 B9 8D 7C")    {
    /* stuff */
 }

if (digitalRead(IESIRE)==0)     {
    /* other stuff */
  }

Note it is not an else if and we've removed the redundant check that you don't care about.

Another edit:

I Think this does it. This is a good example of why DRY (Don't Repeat Yourself) is important. You've got one chunk of about 10 lines that is repeated for 2 conditions - it makes it that much harder to see shat is going on. Compare your original code to this simplified version of your code.

if (content.substring(1) == "A0 B9 8D 7C")  { //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
    doA();
  } 
 else if (content.substring(1) != "A0 B9 8D 7C")    {
    do(B);
 }
  else if ((content.substring(1) != "A0 B9 8D 7C") && (digitalRead(IESIRE)==0))     {
    doA();  
  }


 I think this is what you want:


 bool isRightCard = (content.substring(1) == "A0 B9 8D 7C");
 bool buttonPressed = digitalRead(IESIRE)==0;
 if (isRightCard || buttonPressed) //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
  {
    if (buttonPressed)
    {
        Serial.println("Drum bun!");
    }
    else
    {
        Serial.println("Acces autorizat! Bine ati venit!");
    }
    Serial.println();
    delay(500);
    digitalWrite(LED_V, HIGH);
    tone(BUZZER, 350);
    delay(300);
    noTone(BUZZER);
    bariera.write(120);
    delay(5000);
    bariera.write(0);
    digitalWrite(LED_V, LOW);

  }
 else {
    Serial.println(" Access interzis! La revedere!");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);

 }
John3136
  • 28,809
  • 4
  • 51
  • 69
  • "the last part" means the last else if statement, sorry for not being clear enough. I don't understand because I am already using the example in your code in my code else if (A && B) { /* other stuff */ } And it's not working. I want "the last part" to be executed regardless of the state(s) before it. In my example it was one of the tries. So, ANYTIME digitalRead(IESIRE)==0 I want this to happen: Serial.println("Drum bun!"); digitalWrite(LED_V, HIGH); tone(BUZZER, 350); delay(300); bariera.write(120); ... (etc) – VegetableAnt210 Jun 05 '17 at 03:53
  • If you want it done "regardless of the state(s) before it" then why is it in an `else if` which specifically says "Only do this if you haven't done one of the other possibilities" ? Double check the answer: `If the 2nd bit should always be done ...` If that doesn't address the problem then I'm not sure what you actually want to do. – John3136 Jun 05 '17 at 03:55
  • Because I'm probably doing mostly everything wrong. Before adding the switch, I had a IF and ELSE (to verify content.substring(1) to be == or !=. Once I added the switch, I messed up the code with no luck what so ever. – VegetableAnt210 Jun 05 '17 at 03:58
  • Thank you for trying to help, but no luck. I tried nested IF's and many other statements with no luck... What am I missing? cannot type the whole code in comments now... `if (content.substring(1) == ' xx xx xx xx' { blah blah { if (digitalRead(IESIRE)==0);` – VegetableAnt210 Jun 05 '17 at 09:16
  • 1
    You need to edit your question and make it clear what you are trying to do. From what I understand I would not expected nested ifs to help. I'll make and edit after reading your comments again. – John3136 Jun 05 '17 at 11:04
  • Did that, please check first post. – VegetableAnt210 Jun 05 '17 at 14:03