The code uploaded successfuly but it doesn't work

Hi everyone,

This is my first time working with Arduino, and I'm encountering a problem with a school project. I successfully uploaded the code I wrote to my Arduino Uno R3, but it doesn't function. Interestingly, when I remove the MFRC522 library, the Arduino works perfectly. This leads me to believe the Arduino can't handle the code, so I tried to optimize it but it doesn't work.

Could you please suggest some solutions to tackle this problem?

The code:

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

LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rc522(8, 12);
Servo myservo;

const byte PROGMEM IDs[3][4] = {
  { 0xB3, 0x60, 0x12, 0xAA },
  { 0xD3, 0x92, 0x83, 0xB7 },
  { 0xC9, 0xBB, 0xF3, 0xC0 }
};
int SOLDE[3] = { 100, 50, 25 };

int IDsolde() {
  for (int i = 0; i < 3; i++) {
    if (memcmp_P(rc522.uid.uidByte, IDs[i], 3) == 0) {
      return i;
    }
  }
  return -1;
}

void setup() {
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  rc522.PCD_Init();
  myservo.attach(3);
  myservo.write(0);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print(F("WELCOME"));
}

void loop() {
  if (rc522.PICC_IsNewCardPresent() && rc522.PICC_ReadCardSerial()) {
    int m = IDsolde();
    if (m != -1) {
      int S = SOLDE[m];
      if (S > 0) {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        lcd.setCursor(2, 0);
        lcd.print(F("Votre Solde:"));
        lcd.setCursor(6, 1);
        lcd.print(S);
        lcd.print(F("DH"));
        unsigned long startTime = millis();
        while (millis() - startTime < 4000) {
          if (digitalRead(2) == HIGH) {
            SOLDE[m] -= 5;
            myservo.write(90);
            lcd.clear();
            lcd.setCursor(5, 0);
            lcd.print(F("ENJOY"));
            delay(5000);
            myservo.write(0);
            delay(25);
            break;
          }
        }
      } else {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        delay(100);
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        lcd.setCursor(2, 0);
        lcd.print(F("solde kinem"));
        lcd.setCursor(6, 1);
        lcd.print(S);
        lcd.print(F("DH"));
        delay(4000);
      }
    } else {
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print(F("Chfaar"));
      for (int i = 1; i < 50; i++) {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        delay(100);
      }
    }
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(F("WELCOME"));
  }
}

Sketch uses 8506 bytes (26%) of program storage space. Maximum is 32256 bytes.
Global variables use 505 bytes (24%) of dynamic memory, leaving 1543 bytes for local variables. Maximum is 2048 bytes.

Hoping to help.

Apply the IDE Auto Format tool to your sketch.

Use the IDE Copy for Forum tool, and paste the sketch here.

So it looks like code

void setup() {
   Serial.begin(115200);
}

a7

Welcome to the forum 18bilal-up!

Aside from formatting the code as already suggested in order to make it easier to read (please use the CODE tags), can you elaborate please on what "it doesn't function" mean? What exactly is the problem? What is or is not happening? What do you expect to happen? Is it not happening when the code is compiled, or after it has been uploaded? If its not compiling, then what errors are you getting?

Try to be as specific as you can about the problem so that those reading your comment can understand what is going on and be in a better position to help.

Put it back to original.

2 Likes
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rc522(8, 12);
Servo myservo;

const byte PROGMEM IDs[3][4] = {
  { 0xB3, 0x60, 0x12, 0xAA },
  { 0xD3, 0x92, 0x83, 0xB7 },
  { 0xC9, 0xBB, 0xF3, 0xC0 }
};
int SOLDE[3] = { 100, 50, 25 };

int IDsolde() {
  for (int i = 0; i < 3; i++) {
    if (memcmp_P(rc522.uid.uidByte, IDs[i], 3) == 0) {
      return i;
    }
  }
  return -1;
}

void setup() {
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  rc522.PCD_Init();
  myservo.attach(3);
  myservo.write(0);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print(F("WELCOME"));
}

void loop() {
  if (rc522.PICC_IsNewCardPresent() && rc522.PICC_ReadCardSerial()) {
    int m = IDsolde();
    if (m != -1) {
      int S = SOLDE[m];
      if (S > 0) {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        lcd.setCursor(2, 0);
        lcd.print(F("Votre Solde:"));
        lcd.setCursor(6, 1);
        lcd.print(S);
        lcd.print(F("DH"));
        unsigned long startTime = millis();
        while (millis() - startTime < 4000) {
          if (digitalRead(2) == HIGH) {
            SOLDE[m] -= 5;
            myservo.write(90);
            lcd.clear();
            lcd.setCursor(5, 0);
            lcd.print(F("ENJOY"));
            delay(5000);
            myservo.write(0);
            delay(25);
            break;
          }
        }
      } else {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        delay(100);
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        lcd.setCursor(2, 0);
        lcd.print(F("solde kinem"));
        lcd.setCursor(6, 1);
        lcd.print(S);
        lcd.print(F("DH"));
        delay(4000);
      }
    } else {
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print(F("Chfaar"));
      for (int i = 1; i < 50; i++) {
        digitalWrite(4, HIGH);
        delay(100);
        digitalWrite(4, LOW);
        delay(100);
      }
    }
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(F("WELCOME"));
  }
}

After uploading the code, nothing works, including the functions in the void setup() section. However, when I remove the MFRC522 library and its associated functions, the Arduino works correctly.

Not really.

This is not letting your program progress past the first line:

  if (rc522.PICC_IsNewCardPresent() && rc522.PICC_ReadCardSerial()) {

Your card reader is not working.

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