#Arduino Help

1 messages · Page 9 of 1

wary cloak
#

Because it would get stuck in authentication idk

#

Or in any case

#

False could be trigger by whatever else

#

Not only the case I say

old mango
#

Enum would probably be good if true/false is insufficient

wary cloak
#

Like having any other password

wary cloak
#

I was just saying what's next when we make what we currently have work

old mango
#

Dunno what the actual values that you'd need would be but something vaguely like

enum RFIDPasswordStatus {
  UsingCurrentPassword,
  UsingFactoryPassword,
  GenericErrorCase,
}
#

Anyway, I'm fairly tired so that's it out me for now; good luck with your work today!

wary cloak
#

Thank you

#

I will try the read at home and from there we'll see

wary cloak
#

Ok I'm finally free @hallow hinge @coral geyser are you available?

wary cloak
#

@coral geyser or now?

#

@old mango are you here?

#

Because I'm with the code now

#

And now my problems are different from before

#

I technically advanced tho

old mango
#

Aight wassup

wary cloak
#

So

#

I changed two things

#
void modoMaestro(){
  char password[] = {'c','a','d','3','6','5','\0'};

  Serial.println("Ingrese la contraseña");

  char user_password[7];
 
  waitForPassword(password, user_password);

  byte verificacion = strcmp(password,user_password); 

#

That

#

The buffer being 7

#

And

#
byte bloqueTrailer = sector * 4 + 3;
    byte buffer[18];
    byte size = sizeof(buffer);

    Serial.print(" chau variables");

    mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, &keyA, &(mfrc522.uid));
    mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, &keyB, &(mfrc522.uid));

    Serial.print(" chau autenticacion");

    mfrc522.MIFARE_Read(bloqueTrailer, buffer, &size);

    Serial.print(" chau read");

    if (nuevaKeyA.keyByte != nullptr)
    for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
      buffer[i] = nuevaKeyA.keyByte[i];

      Serial.print(" chau primer null");

    if (nuevaKeyB.keyByte != nullptr)
      for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
       buffer[i + 10] = nuevaKeyB.keyByte[i];

       Serial.print(" chau segundo null");

    mfrc522.MIFARE_Write(bloqueTrailer, buffer, 16);

    Serial.print(" chau escritura");

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();

    Serial.print(" chau stops");

    Serial.println("Proceso Finalizado"); 

#

The read that was missing

#

And now the output goes from

#

This

#

To this

#
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal
Ingrese la contraseña
Contraseña correcta, acerque la tarjeta al lector
111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000111111 chau if's chau variables chau autenticacion chau read chau primer null chau segundo null chau escritura chau stopsProceso Finalizado
 chau funcion
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal 
#

Now all the function is executed

#

And to see if it did change the password

#

I had the code that reads the card information for my password

#
Firmware Version: 0x92 = v2.0
Escanea la tarjeta para ver su UID,SAK, tipo y los bloques de datos...
Card UID: E3 BD CD 1B
Card SAK: 08
PICC type: MIFARE 1KB
Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
  15     63   00 00 00 00  00 00 FF 07  80 69 E0 E1  E2 E3 E4 E5  [ 0 0 1 ] 
         62   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         61   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         60   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  14     59  PCD_Authenticate() failed: Timeout in communication.
  13     55  PCD_Authenticate() failed: Timeout in communication.
  12     51  PCD_Authenticate() failed: Timeout in communication.
  11     47  PCD_Authenticate() failed: Timeout in communication.
  10     43  PCD_Authenticate() failed: Timeout in communication.
   9     39  PCD_Authenticate() failed: Timeout in communication.
   8     35  PCD_Authenticate() failed: Timeout in communication.
   7     31  PCD_Authenticate() failed: Timeout in communication.
   6     27  PCD_Authenticate() failed: Timeout in communication.
   5     23  PCD_Authenticate() failed: Timeout in communication.
   4     19  PCD_Authenticate() failed: Timeout in communication.
   3     15  PCD_Authenticate() failed: Timeout in communication.
   2     11  PCD_Authenticate() failed: Timeout in communication.
   1      7  PCD_Authenticate() failed: Timeout in communication.
   0      3  PCD_Authenticate() failed: Timeout in communication. 
#

It verifies sector 15 has now the other password

#

So far so good.

#

Now.. The issues

#

First of all I sometimes encounter inconsistencies for example for executing the master mode code

#

You see all those 1111111000001111

#

It's because of the serial print is new card present

#

In a normal scenario when you enter the password

#

You will see 11111111

#

Then you read the card and see the prints

#

No 0

#

But sometimes

#

I put the card and it does

#

11111111110000000000000

#

Until I let the card go

#

Like it's detecting there's a card

#

But it doesn't Advance

#

First two times I had to read the card twice

#

Last time only once

#

That type of inconsistentencies

#

But that's the most irrelevant

#

The other thing is

#

Ofc the card now works for all the program

#

Problem

#

When I changed it password back

#

It also worked in all the program for some reason

#

The only place that didn't work was charging money

#

So I revised it because all was printed but no money was charged which was also weird

#
Serial.println("Ahora acerque la tarjeta para cargarle plata");
  while(!mfrc522.PICC_IsNewCardPresent());
  while(!mfrc522.PICC_ReadCardSerial());

  mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, &keyA, &(mfrc522.uid));
 
  String plataAEscribir = String(obtenerSaldo() + String(plata).toInt());
 
  plataAEscribir.toCharArray((char*)datosBloque, 16);
  mostrarByteArray(datosBloque,16);

  mfrc522.MIFARE_Write(numBloque,datosBloque,16);
 
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1(); 
#

Ok there's an authenticate there

#

It shouldn't go past it but

#

At least it doesn't charge money

#

So I said

#

Maybe the other functions doesn't have an authenticate

old mango
#

Ahh, is the write failing?

wary cloak
#

Turns out

#
Serial.println("Ahora acerque la tarjeta para reaizar su pago");
     
      while(!mfrc522.PICC_IsNewCardPresent());
     
      while(!mfrc522.PICC_ReadCardSerial());

      mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, &keyA, &(mfrc522.uid));

      int saldo = obtenerSaldo(); 
#
Serial.println("Apoye su tarjeta para ver su saldo");

  while(!mfrc522.PICC_IsNewCardPresent());
  while(!mfrc522.PICC_ReadCardSerial());

  mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, &keyA, &(mfrc522.uid));

  int saldo = obtenerSaldo();

  Serial.print("Su saldo actual es ");
  Serial.print(saldo); 
#

They do

#

So why

#

Can a card that doesn't have the desired password work

#

And this even happens in master mode

#

With cards that already have the password it also executes the function ofc it does nothing but how does it by pass the authenticate

#

.

#

So to list the problems

old mango
#

Wait, so, to make it clear

#

You can read the saldo but when you try to write it, it doesn't do anything?

#

Or am i misunderstanding

wary cloak
#
  1. inconsistencies in executing

  2. master mode executes doesn't matter your password even if it doesn't do anything because itc changes the password to the one you already have it kinda shouldn't

  3. excepting charging money the program doesn't care for the card's password and works regardless, all authentications seem worthless

wary cloak
#

As I said I changed the password of the card so it's safer and made this code to work only with cards with that password

#

And that's the point of master mode

#

Changing the password of the card so you can also use it

old mango
#

Got it so far

wary cloak
#

But here a non changed password card

#

Works too in almost all scenarios

#

It just doesn't charge it money that's the only thing it only works with my card with my password

#

And even then I don't know why it prints thing beyond the authenticate

#

What it should print in all this cases

wary cloak
#

And I sended the parts of all the functions I'm talking about to show all have authenticate so it doesn't make sense

#

And well we also have the inconsistencies but idk how to fix that and this is most important

old mango
#

So the problem is that you're authenticating with the wrong password but it's still working?

wary cloak
#

Yeah like authentication 98% of cases isn't doing anything

#

For example

#

This code the original password changer

#

As you see in this version it expects you to come with my key 0xC and so

#

As you can see in the declaration of key A

#

And see what happens when I enter with another password

#
Acerca la tarjeta al lector para escanear.
Las claves de esta tarjeta deben ser:
Key-A:  C0 C1 C2 C3 C4 C5
Key-B:  E0 E1 E2 E3 E4 E5
MUY IMPORTANTE: durante el proceso de actualización de las claves 
no separes la tarjeta del lector hasta que no termine.
UID de la tarjeta: E3 BD CD 1B
Modificando sector: 15
Fallo en la autenticación Key-A: Timeout in communication.
Claves del sector 15
Key-A:  C0 C1 C2 C3 C4 C5
Key-B:  E0 E1 E2 E3 E4 E5
Proceso finalizado. Ya puedes retirar la tarjeta del lector RFID
#

In master mode it says, it's finished thank you

#

It just executes it anyway

#

And in the other functions it works just fine no lcd authenticate error nothing

#

This is one example

wary cloak
#

Basically that's the issue

#

After solving that

#

Since master mode finally changes the password at least

#

Next would be error handling and finally EEPROM

#

But well ofc this is first

old mango
#

So, to summarize, the issue is that;
When using a card with the wrong password, it's still able to read data?

wary cloak
#

Yes, and not only that but it by passes all authenticate functions I mean it should go past it but it does

#

Modo maestro

#

Is executed all the way

#

Ofc it doesn't change the password because

#

It's trying to change to the one I already have

#

Even if it did

#

I wouldn't notice

old mango
#

I mean, of course it's going to execute all the way

wary cloak
#

But yeah charge money doesn't charge it that doesn't discard the fact that it goes through the authentication and prints the messages afterwards

old mango
#

You dont check if the authentication fails

wary cloak
#

But if it fails

old mango
#

The authentication will not exit the function if it fails

wary cloak
#

It panics

#

I guess

#

Or maybe

#

M

#

I see the codes and

#

Ok I show you

#

You see

#

The one from the original

#

Change password

#

Had error handling

#

Now the other example

#

Is from this code

#
#include <SPI.h>
#include <MFRC522.h>
 
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
 
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
 
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin(); // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522
    delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme
    mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
    Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
 
void loop() {
    // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }
 
    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
    }
 
    // Dump debug info about the card; PICC_HaltA() is automatically called
    mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
#

Well actually other one but

#

It's just I had to dig in in how dump to serial worked to make it work with my password but

#

Just as dump to serial

#

Calls haltA stop crypto

#

Maybe it has error handling built into it

#

So maybe

old mango
#

It does

wary cloak
#

Authentication just gives back something but doesn't panic on it's own

#

So maybe I just assumed it did

old mango
#

PCD_Authenticate will not do anything except communicate with the chip

wary cloak
#

Do you know what it returns?

old mango
#

It will return STATUS_OK if it succeeds

wary cloak
#

No status nooooo

#

So I was thinking

#

Couldn't we see in the repository how the error handling is made and copy the behavior? And while we are at it do the error handling I personally wanted to do?

old mango
#

Yep

wary cloak
#

Ok let's do that I guess -

#

The thing was when doing this alone

old mango
#

Honestly, you could derive it from the original code for the key changing

wary cloak
#

Since I don't know Cpp watching the repository I didn't understand a thing

wary cloak
#

It would be nice if we could strip the key parts of it

#

I just want it to say

#

Hey you don't have the correct card password bro bye and goes back to the beginning

#

And then well if in master mode you already have the password good it says, hey you are already fine, bye

#

That's it

old mango
#

I'm not talking about that, but rather the literal mechanism by which i detects/handles errors

wary cloak
#

What I said is that code it's too much code

#

To be pasting in all functions

#

So I had an idea

#

Well two ideas.

#

First one.

#

Create a function with status code, authenticate and all, and in all places there's authenticate call that function

#

Second idea.

#

Do all that at the beginning when you start normal mode ask for your card and then you can do as you please but with this method I will have to do like the same code twice because I would need to authenticate in master mode too

old mango
#

whichever you think is more suitable

wary cloak
#

I think the first one is the one that makes the more sense

#

Man I miss being in visual Studio and working in modules-

#

This code is so ugly -

wary cloak
old mango
#

well, first, describe in precise terms what the functions does

wary cloak
#

Well it does the error handling regarding authenticate, so if the key is the correct one and there will only be one correct answer the code will just go on everything is fine, if not tell you that the password of your card is incorrect and you can't access and then take you back to the beginning so maybe you can use master mode to solve it if you are the owner

#

If I would have to guess

#

It would need to receive the same parameters authentication needs so we can use it inside

#

I guess

old mango
#

yeah, sounds about right?

wary cloak
#

Now how to implement it

#

I have to see how the error handling exactly was

old mango
#

Also, before you try to do anything that writes to the saldo block, I'd advise you to read section 8.6.2.1 of that spec

#

There seems to be a specific way that data needs to be formatted

#

Looking at how MIFARE_Write on the sector trailer block seems to have failed when the access bits were set incorrectly

wary cloak
#

I know that the rfid functions need bytes to work with

old mango
#

That could be another failure point

#

Not just bytes but arranged specifically like this

wary cloak
#

For what I've seen

#

I need to declare a variable of status code type

#

Then

#

Binding what's given back of doing authenticate to that variable

#

Since it gives back an status code type also

#

And then as if it's on

#

Ok*

#

If not I can print the error message and return

old mango
#

yep

#

since it's an authenticate function, you probably want some way to tell the caller that the authentication failed

wary cloak
#

Ok so in my case my function wouldn't return anything

old mango
#

ah, so it'd loop until authentication succeeds?

wary cloak
#

No it would just print the message and return to the first menu

#

So if you are the owner you could use master mode on that card

old mango
#

i'm probably misunderstanding something but if the authentication function doesn't return a value, how does the code calling the authentication function know whether the authentication succeeded or not?

wary cloak
#

It will call it

#

And if it fails

#

It will exit the function and go back to the very beginning

#

Btw I did this I show you code above to show you where I am, I made status global just in case

#
#include <SPI.h>
#include <MFRC522.h>
 
#define RST_PIN 5
#define SS_PIN 53
 
MFRC522 mfrc522(SS_PIN, RST_PIN);
 
MFRC522::MIFARE_Key keyA = {keyByte: {0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5}};
MFRC522::MIFARE_Key keyB = {keyByte: {0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5}};

MFRC522::StatusCode estado;
byte sector = 15;
byte numBloque = 60;
byte bloqueTrailer = 63;

void Authentication(){
  estado = mfrc522.PCD_Authenticate(
      MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, antiguaKeyA,
      &(mfrc522.uid)
  );

  if (estado != MFRC522::STATUS_OK) {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
    Serial.println(mfrc522.GetStatusCodeName(estado));
    return;
  }
}
#

Oh

#

I need to change antigua key a

#

To key a

#

Aside from that

#

Anything else?

old mango
#

so, the caller would be something like

void a_random_function() {
  for(;;) {
    Authenticate();
    if(estado != STATUS_OK)
      continue;
    /* remainder of the code */
  }
}

?

wary cloak
#

I don't get the question

wary cloak
#

I use authenticate

#

Well now I would call my function

#

That has authenticate with the error handling inside

#

So, it's the way I made it ok?

#

Since key A, trailer sector and state are global

#

It doesn't recieve anything

#

And since it will just print and go back to the beginning of the program

#

It doesn't return anything

old mango
#

so, the issue is that, as you currently have it, the following is what'll happen if PCD_Authenticate fails

Authentication() is called
PCD_Authenticate() is called, and returns an error
It prints "Autenticacion fallida, ..."
It returns from Authentication()
And then it continues right on and calls obtenerSaldo()
Which will of course fail because it's not authenticated
wary cloak
#

And inside I only do authenticate bind it to the variable and ask if it's ok

wary cloak
#

Well now that I think about it I guess it would work as always

#

It would say hey you shouldn't do that, you just woups

#

Would

#

And that's it

old mango
#

Ah, yeah, that's a pretty big misunderstanding

#

In (almost) ANY language

wary cloak
#

What do I do then? A goto?

old mango
#

return will only return up ONE function

#

there are probably some languages that are exceptions to this but in any mainstream language i can think of that will be the case

wary cloak
#

Well

old mango
#

The proper way to do this

wary cloak
#

Since is way less verbose than I thought

#

We could just paste the code inside the function everywhere it's only like there more lines, that would be a solution

old mango
#

would be making Authenticate

bool Authenticate() {
  // ...
  if(status != STATUS_OK) {
    // ...
    return false;
  } else {
    return true;
  }
}
wary cloak
#

Unless you think there's a better way to be less verbose and avoid repetition

old mango
#

well

wary cloak
#

Well why nota

#

Not*

#

So

#

Let me see if I understood

wary cloak
old mango
#

what do you mean by that?

wary cloak
#

Like

#

If it fails shouldn't it return false and return true if it works

#

Like this

#
bool Authentication(){
  estado = mfrc522.PCD_Authenticate(
      MFRC522::PICC_CMD_MF_AUTH_KEY_A, bloqueTrailer, keyA,
      &(mfrc522.uid)
  );

  if (estado != MFRC522::STATUS_OK) {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
    Serial.println(mfrc522.GetStatusCodeName(estado));
    return false;
  } else {
    return true;
  }
}
#

Function is like this now

#

Do you agree?

old mango
#

Yeah

wary cloak
#

Ok so

#

Now that the function is ok

#

Theoretically ofc

old mango
wary cloak
#

Now I need to understand how would we call it

wary cloak
#

Ah wait a Moment

#

Forget it

wary cloak
old mango
#

if Authenticate() returns false (because of the !), an error occurred, so the current function should also fail

if(!Authentication()) { return false; }
wary cloak
#

But my functions currently don't return booleans

old mango
#

If Authentication() is being called in a loop, it may be more appropriate to do continue instead of return false if you want to retry the authentication

wary cloak
#

No, in all cases I want to return to the beginning because if it doesn't authenticate it will never will the card isn't allowed access

wary cloak
#

There are functions nested

old mango
wary cloak
#

First you choose between master and normal

#

Master is the one you saw is only one

#

Then inside normal you can call the other three

#

So you say

#

That aside from authentication

#

The leer saldo cargar saldo etc also return booleans

#

And also the normal mode function?

#

Because ig that would change it's functionality but yes allow me to do what I want to

old mango
#

i mean

wary cloak
#

Wouldn't*

old mango
#

can leerSaldo cargarSaldo etc fail?
if they can, it's probably better to immediately exit back out than try to continue normally

wary cloak
#

What I understood is that all functions that have authentication in them and in general can fail should have a boolean and it also would allow me to exit functions and go back to the beginning as I wanted to

old mango
#

that's the foundational principle of error handling
making sure that if something goes wrong, you don't keep doing things as if nothing went wrong
because that could make things even worse

#

yeap

wary cloak
#

Ok

#

I will do it with one to see if I understood how to call and what to do

#

If it's fine I'll do the same with the others

old mango
#

also, if you need to pass around more information than a simple 'true'/'false', you can do it like MFRC522 and have STATUS_OK and then STATUS_ERROR_<type> variants that you can return
but if all you need to know is "did this work or did this fail" booleans work pretty well

wary cloak
#

For now yes because the issue now

#

Is that it only works if the card has the password

#

After this works I want to treat two other scenarios

#

But in master mode

#
bool comprar() {
  unsigned char datosBloque[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
  Serial.println("Elija un producto");
  Serial.println("1-Fernet con Coca 400$");
  Serial.println("2-Sex on the beach 250$");
  Serial.println("3-Agua 100$");
 
  char input = waitForOption();

  int precio = 0;

  switch (input) {
    case '1':
      precio = 400;
      break;
    case '2':
      precio = 250;
      break;
    case '3':
      precio = 100;
      break;
    default:
      Serial.println("Elija una opcion valida");
      return;
  }

  Serial.println("Ahora acerque la tarjeta para reaizar su pago");
     
      while(!mfrc522.PICC_IsNewCardPresent());
     
      while(!mfrc522.PICC_ReadCardSerial());

      if(!Authentication()) { return false; }

      int saldo = obtenerSaldo();

      if(saldo < precio){
        Serial.print("Su saldo es insuficiente");
        mfrc522.PICC_HaltA();
        mfrc522.PCD_StopCrypto1();
        return;
      }
     
      String plataADescontar = String(obtenerSaldo() - precio);
      plataADescontar.toCharArray((char*)datosBloque, 16);
      mfrc522.MIFARE_Write(numBloque,datosBloque,16);

      Serial.println("Proceso terminado disfrute de su bebida");

      mfrc522.PICC_HaltA();
      mfrc522.PCD_StopCrypto1();

  return;
 }
#

Here in the buy function

#

I did it

#

I can show you the original again if you need

#

To check it's fine and I do the same with the rest

old mango
#

Well, a couple issues with that

#

Since it's a function with a return type that's not void, you have to return a value from it

#

So those return; statements won't work

wary cloak
#

Well that's really problematic

old mango
#

you'd need to change those to return false; or return true;

wary cloak
#

But

#

I mean

#

Originally

#

If I remember correctly

#

That would only return me to the normal mode function

#

So I could chose again

#

If buy or whatever

#

But

#

If this function as a whole returns false

#

Wouldn't I want normal mode also to return false?

#

Because

#

With authentication errors

#

I want go go all the way back

#

With this one's, it would be kinda annoying

old mango
#

well, you can just return true?

#

But yeah, it is difficult

wary cloak
#

That would exit the function and go back to the previous ? Then I guess that works

#

Anything else?

old mango
#

there is also the issue of returning values from functions

#

for things like obtenerSaldo()

#

the standard pattern for this is passing a pointer to a buffer to the function
so for example, obtenerSaldo() would become something like

bool obtenerSaldo(int *output) {
  // ...
  if(status != STATUS_OK) {
    return false;
  } else {
    int saldoActual = /* ... */;
    // write saldoActual to whatever is pointed to by `output'
    *output = saldoActual;
    return true;
}

And then that'd be called like

  int saldo;
  if(!obtenerSaldo(&saldo)) {
    return false; // or whatever the error handling method is here
  }
  // if obtenerSaldo() returned true, then `saldo' now has the value in it
wary cloak
#

This is why I wanted to avoid error handling damn ir-

#

It*

#

I have to change all my code and how it does stuff when it worked just fine for one feature

#

Let's go step by step

#

As for the bigger functions the read, write buy

#

Now they return booleans

#

I call my function that way to return false if it fails.

#

And all returns from before I turn them into return true?

old mango
#

if that works, then sure?

wary cloak
#

One question.

#

What will happen if authentication works and returns a true?

#

Isn't it the same and will it exit the function?

old mango
#

what do you mean by that

wary cloak
#

You said that all the returns I had

#

That were intended to end the function and go back to the previous I replace them with return true because it would cause the same effect right?

old mango
#

i don't understand how that relates to authentication returning true

wary cloak
#

Because if return true exits the function

#

Wouldn't that mean that authentication would end the function either way?

old mango
#

no?

wary cloak
#

Why?

#

Ah I think I get it

old mango
#

when you do

if(!Authenticate()) {
  return false;
}

that only does something if Authenticate() return false

wary cloak
#

It can only return false and it's inside an if

#

Ok

#

So that solves the big three problem

#

Now

#

About normal and master mode that are the bigger functions

old mango
#

if you just had return Authentication(); then yeah, it would work like you were worried about

wary cloak
#

Ok so now I will change those three functions and show you

#

If it's fine I will move to the bigger two

#

And finally the smalles

#

Smallest*

#

Ok here I go

#
bool leerSaldo() {
  Serial.println("Apoye su tarjeta para ver su saldo");

  while(!mfrc522.PICC_IsNewCardPresent());
  while(!mfrc522.PICC_ReadCardSerial());

  if(!Authentication()) { return false; }

  int saldo = obtenerSaldo();

  Serial.print("Su saldo actual es ");
  Serial.print(saldo);

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();

  return true;
}
#

This one is good?

old mango
#

yeap

wary cloak
#
bool comprar() {
  unsigned char datosBloque[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
  Serial.println("Elija un producto");
  Serial.println("1-Fernet con Coca 400$");
  Serial.println("2-Sex on the beach 250$");
  Serial.println("3-Agua 100$");
 
  char input = waitForOption();

  int precio = 0;

  switch (input) {
    case '1':
      precio = 400;
      break;
    case '2':
      precio = 250;
      break;
    case '3':
      precio = 100;
      break;
    default:
      Serial.println("Elija una opcion valida");
      return;
  }

  Serial.println("Ahora acerque la tarjeta para reaizar su pago");
     
      while(!mfrc522.PICC_IsNewCardPresent());
     
      while(!mfrc522.PICC_ReadCardSerial());

      if(!Authentication()) { return false; }

      int saldo = obtenerSaldo();

      if(saldo < precio){
        Serial.print("Su saldo es insuficiente");
        mfrc522.PICC_HaltA();
        mfrc522.PCD_StopCrypto1();
        return true;
      }
     
      String plataADescontar = String(obtenerSaldo() - precio);
      plataADescontar.toCharArray((char*)datosBloque, 16);
      mfrc522.MIFARE_Write(numBloque,datosBloque,16);

      Serial.println("Proceso terminado disfrute de su bebida");

      mfrc522.PICC_HaltA();
      mfrc522.PCD_StopCrypto1();

  return true;
 }
#

This one?

old mango
#

You missed the return in the default of the switch

wary cloak
#

Oh ok

#

That and it would be it?

old mango
#

yeah, looks ok otherwise

wary cloak
#

and finally

#
bool cargarSaldo() {

  char plata[16];
  unsigned char datosBloque[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

  Serial.println("ingrese la cantidad de dinero que desea cargar y finalice con cualquier letra");
  bool esNumero = obtenerPlata(plata);
  if(esNumero == false){
    Serial.println("No ingreso un numero");
    return true;
  }

  Serial.println("Ahora acerque la tarjeta para cargarle plata");
  while(!mfrc522.PICC_IsNewCardPresent());
  while(!mfrc522.PICC_ReadCardSerial());

  if(!Authentication()) { return false; }
 
  String plataAEscribir = String(obtenerSaldo() + String(plata).toInt());
 
  plataAEscribir.toCharArray((char*)datosBloque, 16);
  mostrarByteArray(datosBloque,16);

  mfrc522.MIFARE_Write(numBloque,datosBloque,16);
 
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
 
  return true;
}
#

This one?

old mango
#

I mean, I think the MIFARE_Write won't work but yeah, the returns are good

wary cloak
#

Why?

wary cloak
#

But it worked before

old mango
#

oh, really?

wary cloak
#

Why would this change that

#

Yes all the time

old mango
#

specifically, you've written to blocks that are NOT the trailer and it's worked?

wary cloak
#

Yes the financial system works since like a month ago

old mango
#

ah, cool

#

was worried for nothing then

wary cloak
#

Ok so now

#

The two biggest functions

#

First normal mode

#
bool modoNormal() {
  Serial.println("\n¿Quiere cargar plata, comprar o verificar su saldo?");
  Serial.println("1-Cargar Plata");
  Serial.println("2-Comprar");
  Serial.println("3-Ver estado de saldo");

  char input = waitForOption();

  switch (input) {
    case '1':{
      cargarSaldo();
      return;
    }
    case '2':{
      comprar();
      return;}
    case '3':{
      leerSaldo();
      return;}
    default:
      Serial.println("Elija una opcion valida");
      return ;

  }
}
#

How do we do it?

old mango
#

does modoNormal() actually need to return to its caller if an authentication failure occurs?

wary cloak
#

Yes because you would be stuck otherwise

#

The password would be wrong

#

You would return to normal mode

#

What do you do?

#

Now if you return to the beginning

old mango
#

i mean it already returns anyway

wary cloak
#

Oh

#

Ok so if nothing needs to be done cool

old mango
#

what is modoNormal() called by?

wary cloak
#

I change the return type to void then

#
void loop() {
  Serial.println("\n¿Quiere acceder al modo maestro u operar normal?");
  Serial.println("1-Modo Maestro");
  Serial.println("2-Modo Normal");

  char input = waitForOption();

  switch (input) {
    case '1':{
      modoMaestro();
      return;
    }
    case '2':{
      modoNormal();
      return;
    }
    default:
      Serial.println("Elija una opcion valida");
      return;
  }
}
old mango
#

Oh, yeah, I think you'd be fine leaving the return type for the modoNormal() as void

wary cloak
#

Ok now let's move to master then

#

Let me see what can I do

#

K

#

First issue

#

Here I check both keys

old mango
#

the key is that you only need to return false if the code calling the function would do something bad if it assumed the function succeeded when it really failed

wary cloak
#

So..

#

I guess

#

I make another function to check key b?

old mango
#

sure?

wary cloak
#

And I rename my function to authenticationA and this new authenticationB

old mango
#

could always just try doing both in Authentication()?

#

and if either fails, return false

wary cloak
#

Authentication only checks for A

#

And so did the other functions but this one

old mango
#

oh, so this is the only one that also checks keyB?

wary cloak
#

Yes

#

That's why

#

Oh

#

I could not so a function

#

Because I won't repeat

#

Tho

#

It would look a bit ugly to be honest

#

One with a function and the other one raw

#

Yeah I prefer the function one

old mango
#

Then yeah, do that

wary cloak
#
bool Authentication_B(){
  estado = mfrc522.PCD_Authenticate(
      MFRC522::PICC_CMD_MF_AUTH_KEY_B, bloqueTrailer, keyB,
      &(mfrc522.uid)
  );

  if (estado != MFRC522::STATUS_OK) {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
    Serial.println(mfrc522.GetStatusCodeName(estado));
    return false;
  } else {
    return true;
  }
}
old mango
#

yea

wary cloak
#
bool modoMaestro(){
  char password[] = {'c','a','d','3','6','5','\0'};

  Serial.println("Ingrese la contraseña");

  char user_password[7];
 
  waitForPassword(password, user_password);

  byte verificacion = strcmp(password,user_password);

  if(verificacion == 0){
    Serial.println("Contraseña correcta, acerque la tarjeta al lector");

    MFRC522::MIFARE_Key keyA = {keyByte : {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
    MFRC522::MIFARE_Key keyB = {keyByte : {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};

    MFRC522::MIFARE_Key nuevaKeyA = {
    keyByte : {0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5}};
    MFRC522::MIFARE_Key nuevaKeyB = {
    keyByte : {0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5}};


    const byte sector = 15;
    for(;;){
    if (!mfrc522.PICC_IsNewCardPresent()) {
      Serial.print(!mfrc522.PICC_IsNewCardPresent());
      continue;
    }
   
    if (!mfrc522.PICC_ReadCardSerial()) {
      Serial.print(!mfrc522.PICC_ReadCardSerial());
      continue;
    }

    Serial.print(" chau if's");


    byte bloqueTrailer = sector * 4 + 3;
    byte buffer[18];
    byte size = sizeof(buffer);

    Serial.print(" chau variables");

    if(!Authentication_A()) { return false; }
    if(!Authentication_B()) { return false; }

    Serial.print(" chau autenticacion");

    mfrc522.MIFARE_Read(bloqueTrailer, buffer, &size);

    Serial.print(" chau read");

    if (nuevaKeyA.keyByte != nullptr)
    for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
      buffer[i] = nuevaKeyA.keyByte[i];

      Serial.print(" chau primer null");

    if (nuevaKeyB.keyByte != nullptr)
      for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
       buffer[i + 10] = nuevaKeyB.keyByte[i];

       Serial.print(" chau segundo null");

    mfrc522.MIFARE_Write(bloqueTrailer, buffer, 16);
#
Serial.print(" chau escritura");

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();

    Serial.print(" chau stops");

    Serial.println("Proceso Finalizado");
     Serial.print(" chau funcion");
    break;
  }

  } else{
  Serial.println("Contraseña Incorrecta");
  return false;
  }
 }
#

Mm

#

I see an issue

#

The for inside

#

If I remember correctly

#

The only reason I placed that for

old mango
wary cloak
#

Is that when starting to debug

#

I didn't put the prints

#

So I though

#

New card present and such

#

Was failing

#

So instead of everywhere else I used the while here I used if's

wary cloak
#

Let me re write it then

#
void modoMaestro(){
  char password[] = {'c','a','d','3','6','5','\0'};

  Serial.println("Ingrese la contraseña");

  char user_password[7];
 
  waitForPassword(password, user_password);

  byte verificacion = strcmp(password,user_password);

  if(verificacion == 0){
    Serial.println("Contraseña correcta, acerque la tarjeta al lector");

    MFRC522::MIFARE_Key keyA = {keyByte : {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
    MFRC522::MIFARE_Key keyB = {keyByte : {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};

    MFRC522::MIFARE_Key nuevaKeyA = {
    keyByte : {0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5}};
    MFRC522::MIFARE_Key nuevaKeyB = {
    keyByte : {0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5}};


    const byte sector = 15;
    for(;;){
    if (!mfrc522.PICC_IsNewCardPresent()) {
      Serial.print(!mfrc522.PICC_IsNewCardPresent());
      continue;
    }
   
    if (!mfrc522.PICC_ReadCardSerial()) {
      Serial.print(!mfrc522.PICC_ReadCardSerial());
      continue;
    }

    Serial.print(" chau if's");


    byte bloqueTrailer = sector * 4 + 3;
    byte buffer[18];
    byte size = sizeof(buffer);

    Serial.print(" chau variables");

    if(!Authentication_A()) { return; }
    if(!Authentication_B()) { return; }

    Serial.print(" chau autenticacion");

    mfrc522.MIFARE_Read(bloqueTrailer, buffer, &size);

    Serial.print(" chau read");

    if (nuevaKeyA.keyByte != nullptr)
    for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
      buffer[i] = nuevaKeyA.keyByte[i];

      Serial.print(" chau primer null");

    if (nuevaKeyB.keyByte != nullptr)
      for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++)
       buffer[i + 10] = nuevaKeyB.keyByte[i];

       Serial.print(" chau segundo null");

    mfrc522.MIFARE_Write(bloqueTrailer, buffer, 16);
#
Serial.print(" chau escritura");

    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();

    Serial.print(" chau stops");

    Serial.println("Proceso Finalizado");
     Serial.print(" chau funcion");
    break;
  }

  } else{
  Serial.println("Contraseña Incorrecta");
  return;
  }
 }
#

Like that?

old mango
#

yeah

wary cloak
#

Ok now what's left are the smaller functions

#

I will just send them and tell me if any need change

old mango
# wary cloak You see all those 1111111000001111

regarding this, it might be easier to debug if you did

bool new_card = mfrc522.PICC_IsNewCardPresent();
if(!new_card) {
  Serial.print('n');
  continue;
}
bool read_card = mfrc522.PICC_ReadCardSerial();
if(!read_card) {
  Serial.print('r');
  continue;
}
#

just a side note

wary cloak
#

I can't mark down that message

#

If it keeps causing issues me might do that

#
char waitForOption(void)
{
  while (!Serial.available());
  char dato = Serial.read();
  while (Serial.read() != 10);
  return dato;
}

bool obtenerPlata(char *plata){
    int i;
    for (i = 0; i < 15; ++i)
    {
        char input = waitForOption();
        if (input >= '0' && input <= '9') {
          plata[i] = input;
        } else break;
    }

    plata[i] = '\0';
    return i;
   
  }

char* waitForPassword(char* password, char* user_password){
  int i;
  for(i=0;i<strlen(password);i++){
    char input = waitForOption();
    user_password[i] = input;
  }
  user_password[i] = '\0';
  return user_password;
}

int obtenerSaldo(){
  unsigned char datosBloque[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  byte arraySize = 18;
  mfrc522.MIFARE_Read(numBloque,datosBloque,&arraySize);
  String saldo_actual = String((char*)(datosBloque));
  int saldoActual = saldo_actual.toInt();
  return saldoActual;
}
old mango
#

because if you do

if(!PICC_IsNewCardPresent())
  Serial.print(!PICC_IsNewCardPresent());

The two PICC_IsNewCardPresent() calls might not be returning the same value

wary cloak
#

Here are the rest of the functions

wary cloak
wary cloak
old mango
#

no i mean what the serial is saying might not be the same as the result in the if

wary cloak
#

Ok let's try it then

#

If it does what it should

#

First I would try to use the card that shouldn't work

#

Then the one that should

#

Finally with master mode change the password for the one that shouldn't and see if it now does

#

errors

#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool Authentication_A()':
Prototipo_RFID_Escritura:21:3: error: no matching function for call to 'MFRC522::PCD_Authenticate(MFRC522::PICC_Command, byte&, MFRC522::MIFARE_Key&, MFRC522::Uid*)'
   );
   ^
In file included from C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:2:0:
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note: candidate: MFRC522::StatusCode MFRC522::PCD_Authenticate(byte, byte, MFRC522::MIFARE_Key*, MFRC522::Uid*)
  StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
             ^~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note:   no known conversion for argument 3 from 'MFRC522::MIFARE_Key' to 'MFRC522::MIFARE_Key*'
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool Authentication_B()':
Prototipo_RFID_Escritura:36:3: error: no matching function for call to 'MFRC522::PCD_Authenticate(MFRC522::PICC_Command, byte&, MFRC522::MIFARE_Key&, MFRC522::Uid*)'
   );
   ^
In file included from C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:2:0:
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note: candidate: MFRC522::StatusCode MFRC522::PCD_Authenticate(byte, byte, MFRC522::MIFARE_Key*, MFRC522::Uid*)
  StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
             ^~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note:   no known conversion for argument 3 from 'MFRC522::MIFARE_Key' to 'MFRC522::MIFARE_Key*'
exit status 1
no matching function for call to 'MFRC522::PCD_Authenticate(MFRC522::PICC_Command, byte&, MFRC522::MIFARE_Key&, MFRC522::Uid*)
old mango
#

ah, try &keyA and &keyB instead of keyA and keyB

#

Ah, yeah, signature is:

StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);

so it wants a pointer for the key parameter

wary cloak
#

It was only that it uploaded

#

So now let's try

#

It passed the first test

#

Now let's try the second test

#

It passed the second test

#

First failure

#

So it passed that

#

Nothing worked and it printed the message and came back where it should

#

It also passed that

#

The card with the password worked

#

It didn't pass in master mode that

#

Even tho this one already has the password changed

#

And in the function key A and key B are the default

#

It still passed

#
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal
Ingrese la contraseña
Contraseña correcta, acerque la tarjeta al lector
11111111111111111111111110000000000000000000011111111111111110000000001111110000011111111111111111111111111111111111111111 chau if's chau variables chau autenticacion chau read chau primer null chau segundo null chau escritura chau stopsProceso Finalizado
 chau funcion
#

Not only that

#

I had to read the cars like 4 times now

#

As you'll see by the zero's

#

But before speaking of fixing it I will do the last test

#

To see if that's the only problem

#

The last test is changing the password via master mode and see if it does well now

#

It failed

#

It rejected this one, it takes like key A is the global one and it doesn't overwrite it or shadow it the new declaration

#
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal
Ingrese la contraseña
Contraseña correcta, acerque la tarjeta al lector
111111111111111111111111111111 chau if's chau variablesAutenticacion fallida, tarjeta rechazada
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal
#

And speaking of Inconsistencies you see it read the card first try now

#

I think I will just change the name of the key and not use the global name let's see if that solves all

#

Issue is that this function checks for the global one ferrisHmm

#

I mean ofc I could

#

Literally copy the two functions and change the name but

#

I'm repeating way too much code

#

Isn't it a way that it will authenticate the key I pass as a parameter so I don't keep creating new functions that do the same?

old mango
#

yeah

wary cloak
#

Or a way to reassing in master mode key a and key b to the equivalents I need here

#

Both work

#

To shadow

old mango
#

Something like

enum KeyKind {
  KEY_A,
  KEY_B,
}
bool AuthenticateMIFAREKey(KeyKind kind, MFRC522::MIFARE_KEY *key) {
  MFRC522::PICC_Command command;
  if(kind == KEY_A) {
    command = MFRC522::PICC_CMD_MF_AUTH_KEY_A;
  } else if(kind == KEY_B) {
    command = MFRC522::PICC_CMD_MF_AUTH_KEY_B;
  } else {
    return false;
  }
  estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  // ...
}
wary cloak
#

Is there way that I can re assing key A in master mode as I originally intended?

old mango
wary cloak
#

In the global scope

#

Key a and b are declared

#

Which is fine for almost everywhere

#

But in master mode

#

Key A and Key B are created again or should because here they should be the default

#

But it seems key a and key b are still taken as the global one

#

At least

#

In the function

old mango
#

no?

wary cloak
#

Maybe that's the thing the function

old mango
#

you can pass in any key as key

wary cloak
#

That seemed complicated

#

So I went for option number two

#

Reassing key A and key B

#

And described what was happening rn

old mango
#

reassing?

wary cloak
#

Yeah

#

I want that key A and B have other values in that function

#

But that doesn't happen

old mango
wary cloak
#

When I call the function to authenticate them I takes as key a and b are the global ones

old mango
#

then you'd have

bool Authenticate_A() {
  return AuthenticateMIFAREKey(KEY_A, &keyA);
}
bool Authenticate_B() {
  return AuthenticateMIFAREKey(KEY_B, &keyB);
}

for the normal case

wary cloak
#

So much re writting I don't know if I should go to sleep or keep going because it's close to being over

wary cloak
old mango
#

you said it seemed complicated?

wary cloak
#

I think too much work for today let's finish it tomorrow

#

More than anything because I work at my bed with my legs crossed and I'm so uncomfortable and I also have this horrifying wire gore and two went out which is annoying

#

This isn't ok(?

#

In any case

#

Tomorrow we finish with this so it works as intended, then we do the other error handling I talked to you about and finally if possible at least start the EEPROM deal so this prototype can be finally finished

#

Well then there would be moving that functionality that was tested alone with the rest of the project

#

After that the only thing left are sensors, really easy stuff I already have a code on how the ultra sound ones work

#

I will be so happy to say bye bye to rfid and hi sensors

#

Tho I will see rfid back in rust but it will be way more tidy

old mango
#

just a heads up, probably won't be as active in chat tomorrow, since today i've mostly just been sketching out UI stuff

wary cloak
#

The thing is you went to sleep today if I recall by the time I wake up tomorrow (?

#

In any case we would work at night again

#

Maybe tomorrow this can be finished

#

It's so close

#

I'm so much looking forward to say it bye bye forever

old mango
#

lol true
even i, sitting on the other side of the screen, want to say goodbye to it already 😔

wary cloak
#

See? Working with the rfid to this level of deep just raises one's suicidal wishes ferrisHmm

#

2 months I think by now

#

Literally

#

The rest of the project

#

One week

#

Lcd, keyboard, relays

#

This mf

#

2 months

old mango
#

yeah, i get that
embedded can be such a hole when you get stuck

wary cloak
#

The primary problem this is my first project when I started it I had starting coding one month ago and I don't know cpp or have that much Arduino experience in any other thing close to null ferrisHmm

old mango
#

ah no wonder

#

yeah, it's not an easy first project, either

wary cloak
#

I hurts me to ask so much help but I think it's impossible not too in this case so I want to get rid of it to do normal stuff I should be able to do on my own and grow as a programmer

#

It*

old mango
#

what class are you doing this for, anyway?

wary cloak
#

I'm in a" technical "(?) School, in last year we have an extra one, for the subject named project, I'm going to be in some months a technician in electronics

#

Tho with this teacher the subject is basically..

#

"Good luck, it better don't be bad"

#

Well more or less

#

But the REAL problem

#

Is all the projects and programming knowledge I lost because of the pandemic the two most important years, the only two years were we had programming subjects, started using Arduino, could've built the robot and compited

#

I would've learned cpp

#

And I would likely had loved programming and did everything I did this 4 months

#

I would have two years of experience

#

So I would be far more prepared

#

More so because since I loved programming when I got into it for real I would've been digging deep that two years

#

In this four months I learned C to a decent extent, Arduino, like how to use the serial port and some other stuff, things of cpp not really deep, OOP, python to a really nice extent because of the course I'm doing and got recently into Django, also rust, and when I finish all of this I plan to learn cpp and finally HTML, CSS and JS

#

Imagine that motivation with two years of time

#

I would be so prepared or well

#

I wouldn't blame anyone

#

And at least if I need help it would be less and I could understand better what they say to me

wary cloak
old mango
#

damn. well, best of luck in your endeavours; hopefully you can get this project finalised speedily and then keep on learning and improving!

wary cloak
#

Thanks, I want to get rid of it so I can focus in Django

#

And surprisingly enough I kinda have my first job

old mango
#

Oh awesome

wary cloak
#

Two classmates want me to do their project (only code)

#

Lucky for me

#

89% of their components are the same of my project

#

The issue is

#

The one that's now.

#

Not*

#

A module for sending messages via sim to a phone

#

It can be

#

Or really complicated or really easy.

#

Anyways aside from django because my course ain't waiting after finishing this project I will focus on the classmate's one and the re writting of this project in rust

#

After those two finally learn cpp and then you know

#

I hope with everything I learned from rust, my experience using Arduino, my knowledge of C, and that I am somewhat familiarized to OOP thanks to python, C++ isn't going to be that much of a pair

#

Pain*

#

What do you think? Since you know cpp

old mango
#

Actually, sorry, lemme just get this out of the way real quick: are you, as a student working on this assignment, allowed to receive help with debugging this? And are you allowed to help those classmates with their project? Should've asked this at the beginning, to be honest

wary cloak
#

Ofc as I told you we are by our own meds the only thing that matters is that the project is done at least with this teacher, the subject is just "good luck"

#

Not like earlier programming subjects were for example we couldn't use libraries

#

Here we just gotta do it doesn't matter how

wary cloak
#

@old mango think you will be available tonight?

old mango
#

And yes, I'm more less available

#

Again, a bit busier than yesterday tho

wary cloak
#

Anyways I'll be back in 15 minutes

#

Meanwhile

#

What do you think of this function to verify any key to solve our problem

#
bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_KEY *key){
  MFRC522::StatusCode estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  if (estado != MFRC522::STATUS_OK) {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
    Serial.println(mfrc522.GetStatusCodeName(estado));
    return false;
  } else {
    return true;
  }
} 

#

In case you are wondering

#

Command is for

old mango
wary cloak
#

PICC_CMD_MF_AUTH_KEY_A or PICC_CMD_MF_AUTH_KEY_B

old mango
#

The only "meds" i know is "medications"

wary cloak
wary cloak
#

The key and which of the two

#

Anyways I'll be back in 15 minutes tell me what you think of that solution

old mango
#

Yeah, from a quick glance that looks fine?

wary cloak
#

And ofc I back now to try that code and see if it solves the issue and we can move on

old mango
#

In modoMaestro?

#

You actually dont

#

If i remember, those were originally from the cambiarKeys, yes?

wary cloak
#

Yes and then I had to

#

When I removed them

#

It had some error

old mango
#

cambiaraKeys took nuevaKeyA and nuevaKeyB as parameters, so the checking for nullptrs is actually a very normal sanity check
However, in your code, there shouldnt be any issue because nuevaKeyA and nuevaKeyB are declared in the same function and you know that they are non null

old mango
#

That's odd

wary cloak
#

No not here there

#

But new key A and B where also declared

#

Tho

#

O thing it checked the old ones I don't remember

#

When everything works we could try and remove the null ptrs checks

wary cloak
#

so in the three functions that already worked I implemented it like this

#
if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, *keyA)) { return false; }```
#

Now I will in master mode and then try it all

old mango
#

Uh, syntax note:

#

*, when used on a variable that's already been declared, is the dereference operator

wary cloak
#

So I should pass this "&"?

#

As a parameter

old mango
#

Yeah

#

& does the opposite from *

wary cloak
#

But not in the signature of the function or also there?

old mango
#

It means 'the address of the variable'

#

Okay, so a variable declared <type> *my_var, is a pointer

#

Pointing to a value of <type> at some location in memory

#

To retrieve or set that value, you use the * operator

wary cloak
#

And if I use my var after declaring it without the *?

old mango
#

And to set that pointer to point at a certain value, you use &

old mango
wary cloak
#

So we declare a pointer which is a variable that's a memory adress that points to a value of a certain type and having declare a pointer we read with & and write write *?

wary cloak
old mango
#

Ah, misread

old mango
#

Then it'll literally just be a memory address

wary cloak
#

And I couldn't do anything because it would just be an hexa number?

old mango
#

However, if it's pointing to a struct or class, you can still call methods and access fields on it with the -> operator

wary cloak
#

I really don't feel secure with pointers yet and I want to badly to understand them

old mango
wary cloak
#

In rust it seems more easy

#

Btw I had errors

wary cloak
#

...

old mango
#

Gimme a sec to open discord on my desktop; it's too hard to type out code examples on my phone

wary cloak
#

I copied the errors and it says

#

-2152

#

That's why I don't like using Arduino -

#

I can't even send it god damn it-

old mango
#

so, the stray '\???' in program seems to indicate you have some weird invisible characters after that closing brace—it should probably be possible to try to select them and delete them

wary cloak
#

But why would I if I didn't change almost anything

#

There's literally nothing there

#

I hate this errors messages so much -

old mango
#

i think just delete the line and retype it

wary cloak
#

Why is that a thing-?

old mango
#

I think you must've accidentally entered some weird characters ~somehow~

wary cloak
#
Prototipo_RFID_Escritura:45:61: error: 'MFRC522::MIFARE_KEY' has not been declared
 bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_KEY *key){
                                                             ^~~~~~~~~~
Prototipo_RFID_Escritura:45:61: error: 'MFRC522::MIFARE_KEY' has not been declared
 bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_KEY *key){
                                                             ^~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool Authentication(MFRC522::PICC_Command, int*)':
Prototipo_RFID_Escritura:46💯 error: no matching function for call to 'MFRC522::PCD_Authenticate(MFRC522::PICC_Command&, byte&, int*&, MFRC522::Uid*)'
   MFRC522::StatusCode estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));
                                                                                                    ^
In file included from C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:2:0:
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note: candidate: MFRC522::StatusCode MFRC522::PCD_Authenticate(byte, byte, MFRC522::MIFARE_Key*, MFRC522::Uid*)
  StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
#
^~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note:   no known conversion for argument 3 from 'int*' to 'MFRC522::MIFARE_Key*'
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool cargarSaldo()':
Prototipo_RFID_Escritura:180:22: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
   if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
                      ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool comprar()':
Prototipo_RFID_Escritura:228:26: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
       if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
                          ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool leerSaldo()':
Prototipo_RFID_Escritura:259:22: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
   if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
#
^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'void modoMaestro()':
Prototipo_RFID_Escritura:316:24: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA)) { return false; }
                        ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:316:72: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA)) { return false; }
                                                                        ^~~~~
Prototipo_RFID_Escritura:317:24: error: 'PICC_CMD_MF_AUTH_KEY_B' was not declared in this scope
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB)) { return false; }
                        ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:317:72: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB)) { return false; }
                                                                        ^~~~~
exit status 1
'MFRC522::MIFARE_KEY' has not been declared
old mango
#

Okay, so most of these errors aren't actually so bad

wary cloak
#

But Arduino makes it seem that way-

old mango
#

Looking at the first one, it's saying it can't find a type named 'MFRC522::MIFARE_KEY'

wary cloak
#

But it's supposed to be in the library

#

Oh

#

I see

#

It's Key

old mango
#

yep

wary cloak
#

What else?

old mango
#

Try a recompile now, and tell me if the next error goes away

wary cloak
#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool cargarSaldo()':
Prototipo_RFID_Escritura:180:22: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
   if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
                      ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool comprar()':
Prototipo_RFID_Escritura:228:26: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
       if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
                          ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool leerSaldo()':
Prototipo_RFID_Escritura:259:22: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
   if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &keyA)) { return false; }
                      ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'void modoMaestro()':
Prototipo_RFID_Escritura:316:24: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA)) { return false; }
                        ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:316:72: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA)) { return false; }
                                                                        ^~~~~
#
Prototipo_RFID_Escritura:317:24: error: 'PICC_CMD_MF_AUTH_KEY_B' was not declared in this scope
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB)) { return false; }
                        ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:317:72: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB)) { return false; }
                                                                        ^~~~~
exit status 1
'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope
old mango
#

Yep!
In case you didn't see, the error

C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool Authentication(MFRC522::PICC_Command, int*)':
Prototipo_RFID_Escritura:46💯 error: no matching function for call to 'MFRC522::PCD_Authenticate(MFRC522::PICC_Command&, byte&, int*&, MFRC522::Uid*)'
   MFRC522::StatusCode estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));
                                                                                                    ^
In file included from C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:2:0:
C:\Users\fuchi\OneDrive\Documentos\Arduino\libraries\MFRC522\src/MFRC522.h:320:13: note: candidate: MFRC522::StatusCode MFRC522::PCD_Authenticate(byte, byte, MFRC522::MIFARE_Key*, MFRC522::Uid*)
  StatusCode PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);

went away

#

this is actually caused by that MIFARE_Key misspelling

#

If you look at the first line, it says In function 'bool Authentication(MFRC522::PICC_Command, int*)

#

You declared it as bool Authentication(MFRC522::PICC_Command, MRFC522::MIFARE_KEY *)
But since it couldn't find MIFARE_KEY, it seems to have just assumed it was an int* instead, which led to it getting confused

wary cloak
#

But I already solved the typo

old mango
#

yep, that's why the error went away

#

i pasted that chunk from the first error message

wary cloak
#

Oh ok

old mango
#

Now, look at the next message: error: 'PICC_CMD_MF_AUTH_KEY_A' was not declared in this scope

wary cloak
#

This time the name is highlighted not as before so I don't think it's wrong

old mango
#

I'll give you a hint: in C++, :: is called the scope resolution operator

wary cloak
#

Why I need it here and not when I used it in the other function

wary cloak
#

OH!

#

I see

old mango
#

What did you see?

wary cloak
#

That it did have the :: to bring it into scope

old mango
#

Precisely speaking, you need to add MFRC522:: at the beginning

#

If you just say PICC_CMD_MF_AUTH_KEY_A, it will try to find that name in the global scope

#

but it only actually exists within the scope of MFRC522

wary cloak
#

Ok solved

#

Let's see now

old mango
#

Also, there's still that last warning, that you're doing a return false in a function that returns void

#

Also, I've got to leave for a bit to cook; be back later

wary cloak
#

Sure gl

#

It compiled but I have a warning maybe is that one you talked about

#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'void modoMaestro()':
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:316:81: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA)) { return false; }
                                                                                 ^~~~~
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino:317:81: warning: return-statement with a value, in function returning 'void' [-fpermissive]
     if(!Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB)) { return false; }
                                                                                 ^~~~~
#

Yeah it is

#

Maybe a normal return will do then

#

Done no more warnings time to test

#

Well it doesn't work

#

It doesn't advance with any of the keys

#

Isn't that just fascinating-

#

Oh it was just another inconsistency

#

I tried it again re opening the serial monitor starting the program again

#

And it passed the same test as the other

#

Not working with the card without changed password and working with the other

#

Now, the moment of truth see if it pass the other two test

#

Yeah it passed all the tests

#

Problem solved we can move on to the next thing now

#

When you come back ofc

#

Unless..

#

@oblique bridge think you could give a hand?

oblique bridge
#

Hey whats up?

wary cloak
#

All more or less fine over here

#

What about you?

oblique bridge
#

yep good good i havent really being following this thread where we up to?

wary cloak
#

Well for starters you know nothing about what are we working on?

oblique bridge
#

nah not really sorry ahahahah

wary cloak
#

Np

#

Because

#

Like some days ago someone new said it wanted to help so

#

I could look to when I give them all the context

#

And I won't have to do it again (?

#

Tho I would need to update it a bit but it's fine

oblique bridge
#

yeah algs you can just fill in whenever hahaha im happy to help out where i can if need be, still need to learn so much embedded rust as well

wary cloak
#

just for now we switched back to normal arduino tho, cpp, so maybe you can help, but do not worry soon enough the rust re writting will continue

oblique bridge
#

oh i know cpp better than rust i think AHAHHA

#

so whats the project?

wary cloak
#

that will come in handy

#

I am looking for the context

#

Well idk if it's too much to read but.. I think here I were I started explaining them the project and what we were doing, literally all context I could provide, after that not much else happened so I won't have to say much else

oblique bridge
#

oh yep sure ill take a look

wary cloak
#

Thanks

oblique bridge
#

what's your native language?

#

and do we have a github for dis?

wary cloak
wary cloak
#

So neither this nor the rust one have it, which is a huge shame for me it would help so much

oblique bridge
oblique bridge
wary cloak
#

From now onwards I'm using git in any new project because my school mail is myself sending the same code to me like "code that does this but with this error" "This code again but it know works" "Ok now fr it works "

wary cloak
#

I learned the UK version but since all content I watch is likely from the US my English is more American leaning it's inevitable

#

Or maybe a weird mixture which would also be inevitable

oblique bridge
#

ye, im from new zealand so again different english slang; we tend to learn words for things in any version of spanish but means we end up having some really whack writing that sorta mashes stuff together ahahah

#

then you can push your local repository to github which means your code will be stored online as well as on your computer, so you dont have to worry about loosing it!

wary cloak
#

I have a github account

oblique bridge
#

oh awesome

wary cloak
#

With the silliest name I could think of

#

I really need to think a user name for those places

#

Tho I get I could upload my rust code but idk my arduino one

#

In any case I wasn't thinking in making repositories for this

#

Since they have already started there are a lot of things lost

#

And using version control on a close to last version

#

Or maybe it's also because of how much I like pure perfect things

#

Like I would love this to have been done like this from the beginning and having all the versions

#

Doing it know would be like doing it halfway through

#

And I really hate that

oblique bridge
#

all your versions will be uploaded to github when you push, because its stored in your local git history :D

#

but yep ic what u mean das algs

wary cloak
#

But the thing is there aren't versions if I keep re writting above only one version

#

As I said I just recently installed git

#

Because of the course I'm in I learned it and may use it soon for the first time

oblique bridge
#

yep yep

#

i digress, i do think venturing into git will def help you in your journey moving forward tho

#

its a really powerful tool

wary cloak
#

Ofc that's why I said every project from now own will be in git but this came too late for that

#

I'm any case, I want to check a thing from another code tho from the same project but I will be alert when you finish reading and so, to add the rest of context

oblique bridge
#

alright i've had a look through, although i havent actually seen the code i think i have a good idea what's going on now :D

wary cloak
#

Ok now that was from that day

#

What changed is

#

The base functionality in rust it's finished

#

The merging of lcd and keyboard, and the relay logic

#

That's also that changed from the rust project

#

But now we are working on the original version

#

To finish the rfid prototype

#

Tho there is not well in what stage it was and what were the issues

#

But if you know the whole project maybe it's enough with saying this

#

What's next now

#

Is the error handling for the scenario where you try to change the password with a card that's already fine and then finally the whole EEPROM deal

#

With that we would finish the rfid prototype and could pass all of that, that worked on it's own to the rest of the project

#

And finally after that I would have to work with the sensors but that should be the easy part so

#

Now I passed all the code earlier but ofc it has changed now

#

@old mango when you come back I have a suggestion for the error handling

#

Remember how what I wanted to do now

old mango
#

Aight what is it

wary cloak
#

Is that when you enter master mode with a card that doesn't need to change it's password anymore it returned you to the beginning with the message like "this card it's already fine"?

#

Well I thought

old mango
#

Yeah, that sounds like it fits your requirements

wary cloak
#

The card is already rejected and with an error message, if only we could personalize the message for master mode

#

But the message it's inside the function

#

It's there a way to do that? Because something as simple as that I think would do

old mango
#

Well, you can add a parameter to the Authenticate function that suppresses printing a message

wary cloak
#

Suppresses?

old mango
#

In the way i used it: prevent, stop from happening

#

Like reprimir, i think, if you were adking for the meaning

wary cloak
#

I know what you mean but not why

#

Because I want to print a different message just for that scenario, not printing nothing

#

We could theoretically

old mango
#

And then if Authenticate fails in modoMaestro, you stop it from erroring, check against the new password, and if that works, you can print the custom message

wary cloak
#

You lost me a little bit there

old mango
#

So, your goal is to print a custom message in modoMaestro if the password is currently set to the new one, yes?

wary cloak
#

Yes

old mango
#

And the error message is coming from Authenticate

wary cloak
#

Indeed so it's the default error message

old mango
#

Yeah, so first, you suppress that error message from checking against the default keys
And if it matches the factory defaults, great, continue as normal, but if it doesn't, you detect that, and go into an if, like you already do
Except now, instead of just returning, you need to check whether it failed because the password is the correct one, or if it's some other, unrelated, password
So you do a second Authentication, using the new password; if this works, then you call HaltA and StopCrypto or whatever to un-authenticate, and then print out the custom message before returning to the main menu
If the second check fails, it's an unrelated password altogether, so you just need to not suppress the error (so it gives a correct message), and return back to the main menu

wary cloak
#

It's certainly hard to explain in the air

old mango
#

There's three behaviours you want to see:

  1. Card password is factory default -> continue
  2. Card password is correct current one -> custom message
  3. Card password is anything else -> normal message
wary cloak
#

Ok I get that

#

Ok I think I get it more now

#

So let's see..

#

I call authentication in master mode

#

With the factory keys

#

If it doesn't fail

#

Okay nothing needs to be changed it's okay

#

But if it fails

#

It won't print the default error message

#

It would enter in another if

#

To authenticate to the new password

#

If it authenticates it prints the custom error messages and if it doesn't the generic one

#

Did I get it?

old mango
#

Yep!

wary cloak
#

Ok let's see if I implement it correctly

#

I don't I do it really long but I will finish it and show you for the feedback-

#
bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_Key *key){
  estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  if (estado != MFRC522::STATUS_OK) {
    MFRC522::StatusCode estado_A = mfrc522.PCD_Authenticate(command, bloqueTrailer, &nuevaKeyA, &(mfrc522.uid))
    MFRC522::StatusCode estado_B = mfrc522.PCD_Authenticate(command, bloqueTrailer, &nuevaKeyB, &(mfrc522.uid))
    if ((estado_A && estado_B) != MFRC522::STATUS_OK){
      Serial.print("La tarjeta ya forma parte del sistema");
      return false;
    } else {
      Serial.print("Autenticacion fallida, tarjeta rechazada");
      return false;
    }
  } else {
    return true;
  }
}
#

Like that?

#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'bool Authentication(MFRC522::PICC_Command, MFRC522::MIFARE_Key*)':
Prototipo_RFID_Escritura:21:86: error: 'nuevaKeyA' was not declared in this scope
     MFRC522::StatusCode estado_A = mfrc522.PCD_Authenticate(command, bloqueTrailer, &nuevaKeyA, &(mfrc522.uid))
                                                                                      ^~~~~~~~~
exit status 1
'nuevaKeyA' was not declared in this scope
#

Though I would have this problem

#

I will try with key A and B which are global and if I remember correctly are the same

#

Forgot semi colons

#

It compiled let's try it

#

it works wrong-

old mango
#

Well, let me ask quickly: do you want it to do the double authentication every time you call Authenticate?

#

Or just in modoMaestro?

wary cloak
#

Just in master mode in any other case I want the behavior it had before

#

And it's working bad

#

Even though my card has the wrong password when it fails for example trying to charge money the error message is the one that should show in Modo maestro for the specific scenario

#
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal

¿Quiere cargar plata, comprar o verificar su saldo?
1-Cargar Plata
2-Comprar
3-Ver estado de saldo
Apoye su tarjeta para ver su saldo
La tarjeta ya forma parte del sistema
¿Quiere acceder al modo maestro u operar normal?
1-Modo Maestro
2-Modo Normal
#

It should say the other error message

old mango
wary cloak
#

What I was telling you from the beginning is that if maybe there was a way to change the message only for master mode without bothering the rest of the program

#

But this solution didn't seem bad I don't know why it isn't working

#
bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_Key *key){
  estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  if (estado != MFRC522::STATUS_OK) {
    MFRC522::StatusCode estado_A = mfrc522.PCD_Authenticate(command, bloqueTrailer, &keyA, &(mfrc522.uid));
    MFRC522::StatusCode estado_B = mfrc522.PCD_Authenticate(command, bloqueTrailer, &keyB, &(mfrc522.uid));
    if ((estado_A && estado_B) != MFRC522::STATUS_OK){
      Serial.print("La tarjeta ya forma parte del sistema");
      return false;
    } else {
      Serial.print("Autenticacion fallida, tarjeta rechazada");
      return false;
    }
  } else {
    return true;
  }
}
#

I don't see the issue

#

If the first authentication failed why did the second succeed when I'm comparing it to the same key

#

The only difference

old mango
#

Look at the second if

wary cloak
#

Is that here I'm checking also key B because master mode does but no other do but it shouldn't check anyways-

old mango
#

What does (a && b) actually do?

wary cloak
#

I'm saying that both key A and B should be different from status ok

old mango
#

Well, what (a && b) actually does is 'convert a and b to boolean values, and if they're both true, evaluate to true, otherwise evaluate to false

wary cloak
wary cloak
old mango
#

If you're using that approach, yes, but

old mango
wary cloak
#

Yeah I don't understand that I understanded your other explanation (?

#

I don't see how not printing the original error messages changes the thing

#

Because still I need to print a error message and it has to be inside authentication

#

Because if it fails it returns and it wouldn't achieve the print

old mango
#

Ah! It does not have to be inside Authentication

#

Because you can check if Authentication returns true or false, and print based on that

wary cloak
#

Ok but how do we do the message suppression?

old mango
#

Well, normally, what do you do if you only want a piece of code to run under certain conditions?

wary cloak
#

An if statement

old mango
#

Yup!

wary cloak
#

But what do we check, that we are in master mode?

old mango
#

That would work; if you want to check that, how would you pass that information into Authentication()?

wary cloak
#

No clue I don't know how to check if I'm inside an specific function I mean I have an idea but it's horrible

old mango
#

Well, what's that idea

wary cloak
#

Like having a global boolean variable for if we are or not in master mode, and it will be always false and we set it to true when in the function so the if would ask for that boolean but it seems idk, horrible or "desprolijo" To me

#

Like not elegant(?

old mango
#

Well, that is very inelegant

wary cloak
#

See

old mango
#

But i think there's a much simpler way; what's the normal way to pass information to a function?

wary cloak
#

Well parameters? There's no other way

old mango
#

Why not create a parameter that specifies whether Authentication should print the error or not?

#

Instead of using a global variable

wary cloak
#

I get that but

#

Idk what we would do later

old mango
#

What do you mean later?

#

In modoMaestro?

wary cloak
#

Yeah like ok we pass that we want or not the message to be printed, then?

wary cloak
old mango
#

Okay, so looking at how you originally did it inside Authentication, how would you do that in modoMaestro(), using the return value of Authentication() instead of the return value of PCD_Authenticate()?

wary cloak
#

I didn't get the question

#

I mean I didn't get it but maybe the answer is

#

Oh

#

I see it know.

#

So

#

When I do authentication I do an if with the two key as I do now if I get true in both I continue, if I get false from any of the two then check with the other passwords and if I get true from both print the custom error message if not then print the normal one

#

More or less

old mango
#

Yeah, that sounds about right

wary cloak
#

Ok I will try it

#
bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_Key *key, bool print_error_message){
  estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  if (estado != MFRC522::STATUS_OK) && (print_error_message == true) {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
    return false;
  } else {
    return true;
  }
}
#

Oh wait

#

Forget it

#

Don't see that

#
bool Authentication(MFRC522::PICC_Command command, MFRC522::MIFARE_Key *key, bool print_error_message){
  estado = mfrc522.PCD_Authenticate(command, bloqueTrailer, key, &(mfrc522.uid));

  if (estado != MFRC522::STATUS_OK {
    if(print_error_message == true){Serial.print("Autenticacion fallida, tarjeta rechazada")}
    return false;
  } else {
    return true;
  }
}
#

Like this?

#

and about master mode..

#
if (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB, false)) == false {
  if (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &keyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &keyB, false)) == true {
    Serial.print("La tarjeta ya forma parte del sistema");
  } else {
    Serial.print("Autenticacion fallida, tarjeta rechazada");
  }
}
#

like this?

#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'void modoMaestro()':
Prototipo_RFID_Escritura:283:82: error: expected identifier before '(' token
     if (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB, false)) == false {
                                                                                  ^
exit status 1
expected identifier before '(' token
#

What's an identifier-

old mango
#

A name of a variable, a type, whatever

wary cloak
#

Btw I obviously changed every other call to authentication to include a true to print the normal error message

old mango
#

Check your parentheses first

wary cloak
#

Ok

#

In the meantime tell me if the change to the function I did and the implementation in master mode are ok

old mango
#

When it says "expected XX", that usually just means there's a syntax error somewhere

#

Also remember that unlike rust, C++ requires you to have parentheses around the entire condition in the if statement

wary cloak
#

If I do that it says it expects a primary expression before ==

#

Idk what the hell it wants know-

old mango
wary cloak
#
C:\Users\fuchi\OneDrive\Documentos\Arduino\Prototipo_RFID_Escritura\Prototipo_RFID_Escritura.ino: In function 'void modoMaestro()':
Prototipo_RFID_Escritura:283:155: error: expected primary-expression before '==' token
     if ((Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB, false))) == false {
                                                                                                                                                           ^~
exit status 1
expected primary-expression before '==' token
#

It's like a baby or a dog I don't know what the hell it wants from me-

old mango
wary cloak
#

But i did

#

That why

#

The error message is different

old mango
#

No, you have parentheses around the Authentication() calls

wary cloak
#

It went from identifier

#

To primary expression

old mango
#

And a single ) at the end

#

But there's still the false that needs a parentheses around it too

wary cloak
#

The false after the == ?

#
if ((Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB, false))) == (false)
#

Please god it literally has all the parenthesis I saw them with the highlighting-

#

When you click a parenthesis it shows you the one that closes it

#

And it's all closed all good -

#

What the hell wants from me-

old mango
#

when i say parentheses around the whole condition that means everything between the if and the { is enclosed by a single set of parentheses, like so:

if(
(a && b) == false
)

(note: separated on different lines for emphasis)

wary cloak
#

But I did that ;;;

#

Before I didn't and it showed this

wary cloak
#

When I did it started showing this -

wary cloak
#

It has parenthesis for calling the function, for the expression of calling the function

old mango
#

you're missing a ( in that second one

wary cloak
#

And for the comparison between the two

old mango
#

and the false was still outside

wary cloak
#

Finally..

#

I hate you cpp..

#

So

#

Are the way I implemented the authentication function in master mode and the new Authentication function good?

old mango
#

i think?

wary cloak
#

Ok let's try it then..

old mango
#

lemme see the current version rl quick

wary cloak
#

And

#
if (((Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &antiguaKeyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &antiguaKeyB, false))) == false) {
      if (((Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_A, &keyA, false)) && (Authentication(MFRC522::PICC_CMD_MF_AUTH_KEY_B, &keyB, false))) == true) {
        Serial.print("La tarjeta ya forma parte del sistema");
      } else {
        Serial.print("Autenticacion fallida, tarjeta rechazada");
      }
    }
old mango
#

add one more left parentheses at the beginnings

wary cloak
#

But it compiles already

old mango
#

oh, nvm, is misread that

#

yeah, that is fine

wary cloak
#

But it doesn't work correctly