Spi ı2c problem RC522 USB HOSTSHIELD BARCODE SCANNER

Hi, an arduino is an arduino usb hostshield barcode reader attached to it.
i have an lcd i2c driver and a rc522 rfid reader.

It communicates over hostshield and rc522 spi over lcd i2c. All working alone but not working together

#include <SPI.h>
#include <MFRC522.h>
#include <HID.h>
#include <hid.h>
#include <hiduniversal.h>
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <SPI.h>                   //SPI kütüphanemizi tanımlıyoruz.

#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>              //MFRC522 kütüphanemizi tanımlıyoruz.
#include <Usb.h>
#include <usbhub.h>
#include <hidboot.h>

int led = A0;
#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          7          // Configurable, see typical pin layout above
#define SS_USB          10

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

float  toplam = 0;
int i = 0;

float pkalem    = 4.70;
float ple       = 0.65;

char harfler[13] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
char kalem[13]   = {'9', '7', '9', '9', '7', '5', '3', '2', '9', '3', '6', '8', '5'};
char le[13]      = {'8', '6', '9', '1', '0', '1', '4', '0', '0', '0', '0', '1', '2'};
LiquidCrystal_I2C lcd(0x27,16,2);
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);

class KbdRptParser : public KeyboardReportParser
{     
    void PrintKey(uint8_t mod, uint8_t key);
  protected:
    virtual void OnKeyDown (uint8_t mod, uint8_t key);
    virtual void OnKeyPressed(uint8_t key);
  
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  uint8_t c = OemToAscii(mod, key);

  if (c)
    OnKeyPressed(c);
 
}

void KbdRptParser::OnKeyPressed(uint8_t key)
{   
  
  static uint32_t next_time = 0;
  if ( millis() > next_time ) {
    i = 0;
    delay( 5 );
  }

  next_time = millis() + 200;


  //Serial.print( (char)key );

  harfler[i] = (char)key;

  //Serial.print("i = "); Serial.println(i);
  //Serial.print("bs harfler = "); Serial.print(harfler[0]); Serial.print("."); Serial.print(harfler[1]); Serial.print("."); Serial.print(harfler[2]); Serial.print("."); Serial.println(harfler[3]);



  i++;


  if (harfler[7] == kalem[7])
  { if (harfler[8] == kalem[8])
    { if (harfler[9] == kalem[9])
      { if (harfler[10] == kalem[10])
        { if (harfler[11] == kalem[11])
          { if (harfler[12] == kalem[12])
            {
              if (i == 13)
              {
                toplam = pkalem + toplam;
                String kalems = String(pkalem);
              }

              digitalWrite (led, 1);
              lcd.setCursor( 0, 0);
              lcd.print("KALEM.   4.70 TL");
              lcd.setCursor( 0, 1);
              lcd.print("TOPLAM           ");
              lcd.setCursor( 7, 1);
              lcd.print(toplam);
              lcd.setCursor( 14, 1);
              lcd.print("TL");
            }
          }
        }
      }
    }
  }
 if (harfler[7] == le[7])
  { if (harfler[8] == le[8])
    { if (harfler[9] == le[9])
      { if (harfler[10] == le[10])
        { if (harfler[11] == le[11])
          { if (harfler[12] == le[12])
            {
              if (i == 13)
              {
                toplam = ple + toplam;
                String ples = String(ple);
                char charBuf[5];
                ples.toCharArray(charBuf, 5);
                Serial.write(charBuf,5);
                Serial.println(charBuf);
              }


              lcd.setCursor( 0, 0);
              lcd.print("LE'PORT. 0.65 TL");

              lcd.setCursor( 0, 1);
              lcd.print("TOPLAM           ");
              lcd.setCursor( 7, 1);
              lcd.print(toplam);
              lcd.setCursor( 14, 1);
              lcd.print("TL");

            }
          }
        }
      }
    }
  }
else
  {
    if (i == 13)
    {
      
      Serial.print("yabanci");

     

lcd.setCursor( 0, 0);
    lcd.print(" TANIMSIZ  URUN ");
    lcd.setCursor( 0, 1);
    lcd.print(harfler[0]);
    lcd.print(harfler[1]);
    lcd.print(harfler[2]);
    lcd.print(harfler[3]);
    lcd.print(harfler[4]);
    lcd.print(harfler[5]);
    lcd.print(harfler[6]);
    lcd.print(harfler[7]);
    lcd.print(harfler[8]);
    lcd.print(harfler[9]);
    lcd.print(harfler[10]);
    lcd.print(harfler[11]);
    lcd.print(harfler[12]);
    lcd.print("                  ");


    }
  }

   Serial.print(harfler[0]);
      Serial.print(harfler[1]);
      Serial.print(harfler[2]);
      Serial.print(harfler[3]);
      Serial.print(harfler[4]);
      Serial.print(harfler[5]);
      Serial.print(harfler[6]);
      Serial.print(harfler[7]);
      Serial.print(harfler[8]);
      Serial.print(harfler[9]);
      Serial.print(harfler[10]);
      Serial.print(harfler[11]);
      Serial.println(harfler[12]); 
      
}


KbdRptParser Prs;
int sil_pini = A1;
int sil;

/**
 * Initialize.
 */
void setup() {  
    delay(200);
    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 card
    if (Usb.Init() == -1) {
    //Serial.println("OSC Başlatılamadı.");
  }

  //  delay( 200 );

  Hid.SetReportParser(0, (HIDReportParser*)&Prs);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.noAutoscroll();




  lcd.setCursor( 0, 0);
  lcd.print("ALINAN URUNLERIN");

  lcd.setCursor( 0, 1);
  lcd.print("TOPLAMI 00.00 TL");
  delay(200);
    // Prepare the key (used both as key A and as key B)
    // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
    for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
    }
    
    Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
    Serial.print(F("Using key (for A and B):"));
    dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
    Serial.println();

    Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
}

/**
 * Main loop.
 */
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;

    // Show some details of the PICC (that is: the tag/card)
    Serial.print(F("Card UID:"));
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println();
    Serial.print(F("PICC type: "));
    MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    Serial.println(mfrc522.PICC_GetTypeName(piccType));

    // Check for compatibility
    if (    piccType != MFRC522::PICC_TYPE_MIFARE_MINI
        &&  piccType != MFRC522::PICC_TYPE_MIFARE_1K
        &&  piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
        Serial.println(F("This sample only works with MIFARE Classic cards."));
        return;
    }

    // In this sample we use the second sector,
    // that is: sector #1, covering block #4 up to and including block #7
    byte sector         = 1;
    byte blockAddr      = 4;
    byte dataBlock[]    = {
        0x01, 0x02, 0x03, 0x04, //  1,  2,   3,  4,
        0x05, 0x06, 0x07, 0x08, //  5,  6,   7,  8,
        0x09, 0x0a, 0xff, 0x0b, //  9, 10, 255, 11,
        0x0c, 0x0d, 0x0e, 0x0f  // 12, 13, 14, 15
    };
    byte trailerBlock   = 7;
    MFRC522::StatusCode status;
    byte buffer[18];
    byte size = sizeof(buffer);

    // Authenticate using key A
    Serial.println(F("Authenticating using key A..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }

    // Show the whole sector as it currently is
    Serial.println(F("Current data in sector:"));
    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
    Serial.println();

    // Read data from the block
    Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();
    Serial.println();
 

    // Authenticate using key B
    Serial.println(F("Authenticating again using key B..."));
    status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("PCD_Authenticate() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
    }
  
    // Write data to the block
    Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    dump_byte_array(dataBlock, 16); Serial.println();
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Write() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.println();

    // Read data from the block (again, should now be what we have written)
    Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
    Serial.println(F(" ..."));
    status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);
    if (status != MFRC522::STATUS_OK) {
        Serial.print(F("MIFARE_Read() failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
    }
    Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
    dump_byte_array(buffer, 16); Serial.println();

    // Check that data in block is what we have written
    // by counting the number of bytes that are equal
    Serial.println(F("Checking result..."));
    byte count = 0;
    for (byte i = 0; i < 16; i++) {
        // Compare buffer (= what we've read) with dataBlock (= what we've written)
        if (buffer[i] == dataBlock[i])
            count++;
    }
    Serial.print(F("Number of bytes that match = ")); Serial.println(count);
    if (count == 16) {
        Serial.println(F("Success :-)"));
    } else {
        Serial.println(F("Failure, no match :-("));
        Serial.println(F("  perhaps the write didn't work properly..."));
    }
    Serial.println();

    // Dump the sector data
    Serial.println(F("Current data in sector:"));
    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
    Serial.println();

    // Halt PICC
    mfrc522.PICC_HaltA();
    // Stop encryption on PCD
    mfrc522.PCD_StopCrypto1();
    
    
    
}

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
    
  sil = analogRead (sil_pini);



  if (sil > 500)
  {
    toplam = 0;

    digitalWrite (led, 1);

    lcd.setCursor( 0, 0);
    lcd.print("ALINAN URUNLERIN");
        lcd.setCursor( 0, 1);
        lcd.print("TOPLAM           ");
        lcd.setCursor( 7, 1);
        lcd.print(toplam);
        lcd.setCursor( 14, 1);
        lcd.print("TL");
        digitalWrite (led, 0);
        
  }

 
  Usb.Task();
}

you need to identify which device(s) are the problem

  1. disable all your IO devices
  2. enable the devices one at time and test

when a test fails you then have some idea of the location of the problem(s)
it may also be worth trying different combinations of enabling devices to determine if there are multiple conflicts

they all work alone. It doesn't work when I connect them all together and run.

Please clarify - are you mean that each device works with this full code when connected alone? Or you test your devices with it own separate codes for each of it?

rc522 works with the readandwrite example in the example part of its library.

The barcode is connected to the shield via usb. It works when I remove the rc522 in this code.

Isn't this a memory problem? What does arduino IDE say after compilation?

i think it's a communication problem but i don't know how to solve it rc 522 and usb hostshield communicate via spi i separated pins but i don't know

How did you "separated pins"?
SPI is multi-client bus, you not need using separate pins expect SS pin. All spi devices share the same MOSI, MISO and CLK pins.

You didn't answer my question in post #6

only ss pins are separate.
ide doesn't give me an error

RF ss pin 7

USB hotsheıld ss pin 10

lcd ı2c driver ı2c communication A5 A4

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