Need Upwork code explained to me, programmer MIA

Hello there,

I really need some help.

I have no idea how to use an Arduino so i went on 'upwork' to hire someone to help me write the code for a project I'm trying to complete. Unfortunately they have gone MIA and I don't know anything about coding. Our agreement was that he would talk me through how to use the code, and modify it as I needed. But all i have is a code I don't understand. I was hoping someone could translate it for me as I'm not sure what he has given me, or if it is at all what i had requested.

The project was a box which opens when a button is pressed by 'servo motors'. Once open 6 RFID readers are exposed which communicate through the 'arduino' to an RPi which would play a short video linked to the different cards.

Thank is advance

/*************************     Libraries      *********************************/
#include <MFRC522.h>  //https://github.com/miguelbalboa/rfid
#include <Servo.h>

/************************* Constants *********************************/
#define SS_PIN 5
#define RST_PIN 0
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
Servo myServo5;
int index;
// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 7;  // the number of the pushbutton pin
// Variables will change:
int lastState = LOW;  // the previous state from the input pin
int currentState;     // the current reading from the input pin


/************************* Parameters *********************************/
String id_allowed[] = { "4a88f980", "a7607cb4", "d4fadae9", "40b28d4d", "196a7be", "061854d" };


/********************* Global connection instances **************************/
MFRC522 rfid(SS_PIN, RST_PIN);
String ID_Card, op_name;
byte nuidPICC[4];
void setup() {
  myServo1.attach(5);   //servo1
  myServo2.attach(6);   //servo2
  myServo3.attach(9);   //servo3
  myServo4.attach(10);  //servo4
  myServo5.attach(11);  //servo5

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  //init rfid
  SPI.begin();      // init SPI bus
  rfid.PCD_Init();  // init MFRC522
  Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
  myServo1.write(0);
  myServo2.write(0);
  myServo3.write(0);
  myServo4.write(0);
  myServo5.write(0);
}

void loop() {
  String idcard;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
                                            // Initialisé la boucle si aucun badge n'est présent
  if (!rfid.PICC_IsNewCardPresent())
    return;

  // Vérifier la présence d'un nouveau badge
  if (!rfid.PICC_ReadCardSerial())
    return;

  // Enregistrer l'ID du badge (4 octets)
  for (byte i = 0; i < 4; i++) {
    nuidPICC[i] = rfid.uid.uidByte[i];
  }
  // Affichage de l'ID
  Serial.println("Un badge est détecté");
  Serial.println(" L'UID du tag est:");
  idcard = "";
  for (byte i = 0; i < 4; i++) {
    idcard += String(rfid.uid.uidByte[i], HEX);
  }
  Serial.println(idcard);
  if (idcard == "a7607cb4") {
    index = "0";
    do {
      Serial.write(index);
    } while (Serial.read() != index);
  }


  // Re-Init RFID
  rfid.PICC_HaltA();       // Halt PICC
  rfid.PCD_StopCrypto1();  // Stop encryption on PCD

  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN);

  if (lastState == LOW && currentState == HIGH) {
    for (int pos = 0; pos <= 90; pos += 1) {  // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myServo1.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 0; pos <= 90; pos += 1) {  // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myServo2.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 0; pos <= 90; pos += 1) {  // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myServo3.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 0; pos <= 90; pos += 1) {  // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myServo4.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 0; pos <= 90; pos += 1) {  // goes from 0 degrees to 90 degrees
      // in steps of 1 degree
      myServo5.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    // save the last state
    lastState = currentState;
  }
  if (lastState == HIGH && currentState == LOW) {
    for (int pos = 90; pos >= 0; pos -= 1) {  // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myServo1.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 90; pos >= 0; pos -= 1) {  // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myServo2.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 90; pos >= 0; pos -= 1) {  // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myServo3.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 90; pos >= 0; pos -= 1) {  // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myServo4.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    for (int pos = 90; pos >= 0; pos -= 1) {  // goes from 90 degrees to 0 degrees
      // in steps of 1 degree
      myServo5.write(pos);  // tell servo to go to position in variable 'pos'
      delay(15);            // waits 15ms for the servo to reach the position
    }
    // save the last state
    lastState = currentState;
  }
}

You will likely be posting code multiple times in this thread, so you may as well do it right.
Instead of pasting it straight into your message box, click on </> then paste into the correct place. Notice how nice the code looks! Next, go back to your IDE and press ctrl-T (Autoformat), then ctrl-shft-c (copy for forum), and paste in that version in instead. There, you've posted your first code correctly!
Read this, as well:
How to get the best from this forum

the above suggests there's a single servo the open/closes the box, but i see 6 servos that when the button is pressed sequentially drives each servo to 90 deg

and when released sequentially drives each servo back to 0 deg

i guess it makes sense to exit from loop() early if there is no PICC (rfid?) card present

as well as if it hasn't read anything

but i don't understand the loops that concatenate bytes from a card - are there really 6 RFID readers -- why aren't 6 different readers being checked?

these results are transmitted out thru the serial interface

don't understand why a card would be halted

there's also 6 IDs

but i only see one IDE being checked

don't understand what this read is attempting to do.

ask questions

There seem to be a couple of small errors in the code.

Pin 5 is used for the select pin of the RFID reader and also for one of the servos:

#define SS_PIN 5
...
...
MFRC522 rfid
(SS_PIN, RST_PIN);
...
...

void setup() {
  myServo1.attach(5); //servo1

Not sure exactly what is going on here, but assigning "0" to an int is a bit unusual, and will show a warning in the compiler. Looks like it would be printing out a string of zeros until the ascii character for zero is received, which would require something to be sending to the serial port.

  if (idcard == "a7607cb4") {
    index = "0";
    do {
      Serial.write(index);
    } while (Serial.read() != index);
  }

Whoever wrote this apparently has never heard of arrays.

1 Like

Thank you for the information camsysca, looks much better.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.