Problem with 'i' was not declared in this scope

Hello,
This gets asked a lot, but I would like to post my personal version of this problem, since I am just a beginner with programming, and can’t seem to find the solution -or reason for that matter- to this error.

I am trying to write a sketch for an rf522 card readr. The scenario would be a sort of RDIF card Easter egg hunt. Once a reader has read 2 cards, a message should be displayed. The sketch is a WOP rigth now, and is mostly a mash-up of different skeches from other programmers, since I haven’t gotten to programming level yet.

I am getting a “‘i’ was not declared in this scope” error when trying to compile. From what I can gather, it refers to the “mfrc522.uid.uidByte[i]”. How do I declare that value? Isn’t it supposed to be achieved by hte program itself once the reader reads a card?
Thanks, any help is appreciated.

MKy sketch here:

#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED_G 2 //define green LED pin
#define LED_R 3 //define red LED
#define BUZZER 4 //buzzer pin

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

int count = 0;
const int rs = 8, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

byte card_ID[4]; //card UID size 4byte
byte Name1[4] = {0x99, 0x5F, 0xD9, 0xA2}; //first UID card
byte Name2[4] = {0xC1, 0x7A, 0xDD, 0x2B}; //second UID card
byte Name3[4] = {0xE4, 0xAB, 0x52, 0x2A}; //third UID card
byte Name4[4] = {0x75, 0x9D, 0xF0, 0xC2}; //fourth UID card 
byte Name5[4] = {0xB9, 0xCF, 0xD4, 0xA2}; //fifth UID card
byte Name6[4] = {0xF2, 0x8A, 0xF5, 0x33}; //sixth UID card   

//if you want the arduino to detect the cards only once
int NumbCard[6];//this array content the number of cards. in my case i have just two cards.
int j = 0;

String Name;//user name
long Number;//user number
int n ;//The number of card you want to detect (optional)

 
void setup() 
{
  lcd.begin(16,2);
   
  Serial.begin(9600);   // Initiate a serial communication
  Serial.println("RFID attendance");
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
 

  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
//  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())   {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())   {
    return;//if read card serial(0) returns 1, the uid struct contians the ID of the read card.
  }

  for (byte i = 0; i < mfrc522.uid.size; i++) 
     card_ID[i] = mfrc522.uid.uidByte[i];
     
     //Show UID on serial monitor
     Serial.print("UID tag :");
     String content= "";
     byte letter;
  
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
 
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "75 9D F0 C2") 
  {
   //  delay(500);
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
//    tone(BUZZER, 500);
    delay(300);
//    noTone(BUZZER);
    digitalWrite(LED_G, LOW);
    //  lcd.clear(); 
    count++;                    // Increment Count by 1
    Serial.println(count);
    //delay(400);
  }
   if (content.substring(1) == "B9 CF D4 A2") 
  {
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
//    tone(BUZZER, 500);
    delay(300);
 //   noTone(BUZZER);
    digitalWrite(LED_G, LOW);
     count++;                    // Increment Count by 1
      Serial.println(count);
  // delay(400);
  }
   
if (content.substring(1) == "F2 8A F5 33") 
  {
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
 //   tone(BUZZER, 500);
    delay(300);
 //   noTone(BUZZER);
    digitalWrite(LED_G, LOW);
      lcd.clear(); 
    count++;                    // Increment Count by 1
      lcd.setCursor(1, 0);
    Serial.println(count);
   // delay(400);
  }
  if (content.substring(1) == "F2 8A F5 33") 
  {
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
 //   tone(BUZZER, 500);
    delay(300);
 //   noTone(BUZZER);
    digitalWrite(LED_G, LOW);
      lcd.clear(); 
    count++;                    // Increment Count by 1
      lcd.setCursor(1, 0);
    Serial.println(count);
   // delay(400);
  }
  if (content.substring(1) == "F2 8A F5 33") 
  {
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
 //   tone(BUZZER, 500);
    delay(300);
 //   noTone(BUZZER);
    digitalWrite(LED_G, LOW);
      lcd.clear(); 
    count++;                    // Increment Count by 1
      lcd.setCursor(1, 0);
    Serial.println(count);
   // delay(400);
  }
  if (content.substring(1) == "F2 8A F5 33") 
  {
    Serial.println("Authorized card");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
 //   tone(BUZZER, 500);
    delay(300);
 //   noTone(BUZZER);
    digitalWrite(LED_G, LOW);
      lcd.clear(); 
    count++;                    // Increment Count by 1
      lcd.setCursor(1, 0);
    Serial.println(count);
   // delay(400);
  }
 if (NumbCard[j] == 1) { //to check if the card already detect
    //if you want to use LCD
    Serial.println("Tarj. Ya Existe");
    Serial.println("numj:");
    Serial.println(NumbCard[j]);
    lcd.setCursor(0,0);
    lcd.print("Tarj. Ya Existe");
    tone(Buzzer, 300);
    digitalWrite(RedLed, HIGH);
    delay(1000);
    noTone(Buzzer);
    digitalWrite(RedLed, LOW);
    lcd.setCursor(0,0);
    lcd.print("ACERQUE TARJETA ");
  }
  
  else {
    NumbCard[j] = 1;//put 1 in the NumbCard array : NumbCard[j]={1,1} to let the arduino know if the card was detecting
    n++;//(optional)
    Serial.println(Number); //send the Number to excel
    Serial.println(j);
    digitalWrite(GreenLed, HIGH);
    digitalWrite(RedLed, LOW);
    digitalWrite(Buzzer, HIGH);
    delay(30);
    digitalWrite(Buzzer, LOW);
   }
  delay(1000);
cont:
  delay(2000);
  digitalWrite(GreenLed, LOW);
  digitalWrite(RedLed, LOW);

} 

Hello,

for (byte i = 0; i < mfrc522.uid.size; i++) 
     card_ID[i] = mfrc522.uid.uidByte[i];

Your loop end here… You need to add brackets { } for more than one line

The problem shows up well if you Auto format the code in the IDE

  for (byte i = 0; i < mfrc522.uid.size; i++)
    card_ID[i] = mfrc522.uid.uidByte[i];
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content = "";
  byte letter;
  Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");

Without curly brackets around the dependant code i is only in scope for a single line of code

Geez! Thankyou both. This is what happens from unproofed copy-pasting sketches.

I can succesfully compile and upload the sketch now. But I get nothing on the serial monitor now.

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);   // Initiate a serial communication
  Serial.println("RFID attendance");
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522


  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  //  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

}

This should at least display the greeting and prompt message on the serial monitor, right?

Probably. If it doesn’t, first check that serial monitor is open and set to 9600. If that’s o.k. try moving the lcd.begin BELOW everything else in setup, in case it is hanging there.

Steve

Done! Thanks. It was the lcd.begin.

I decided to remove the lcd for now while I test the project just to keep things simpler. Once the code works, output to the lcd is pretty simple

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