Need help combining Sketches

I seem to have trouble combining these two sketches and can't get the speed detection to work when i finally combine them. Also I seem to be getting an error trying to source the name of the rfid tag. Here's the speed detection code:

#include <Wire.h> //include necessary libraries
#include <LiquidCrystal_I2C.h> //include I2C communication protocol

LiquidCrystal_I2C lcd(0x27, 16, 2); //define address and size of lcd

int sen1=5; //input 1
int sen2=6; //input 2


unsigned long t1=0; //time variable
unsigned long t2=0; //time variable
float velocity; //variable value 'velocity'
void setup()
{
  lcd.init();
	lcd.backlight(); //initiate lcd
  
	
  pinMode(sen1,INPUT); 
  pinMode(sen2,INPUT); //define pins 11 and 12 as inputs
  Serial.begin(9600); //initiate serial communication
  lcd.clear(); 
  lcd.setCursor(0,0); 
  lcd.print(" Speed Detector "); //write the msg at start


}

void loop() 
{
   


  while(digitalRead(sen1));
  while(digitalRead(sen1)==0);
  t1=millis();
  while(digitalRead(sen2));
  t2=millis();
  velocity=t2-t1;
  velocity=velocity/1000;//convert millisecond to second
  velocity=(0.05/velocity);//v=d/t
  velocity=velocity*3600;//multiply by seconds per hr
  velocity=velocity/1000;//division by meters per Km
  int sped = velocity;
   if (sped < 150)
  {
    
   lcd.setCursor(3,1);
   lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
  }  
  
   else if (sped > 150)
  {
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print("   OVERSPEED");

    lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print(" Speed Detector");
   delay(500);
    
  }
 
}

And the RFID part in the reply (just reading part, that's all i want to work right now)

I need it to be so that both work simultaneously. It doesn't have to be much complicated, the Serial monitor can show the rfid details while the screen shows the speed

/*
 * Initial Author: ryand1011 (https://github.com/ryand1011)
 *
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 *
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 *
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/

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

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // 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
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
  Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read
}

//*****************************************************************************************//
void loop() {

  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;

  //-------------------------------------------

  // 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;
  }

  Serial.println(F("**Card Detected:**"));

  //-------------------------------------------

  mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card

  //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex

  //-------------------------------------------

  Serial.print(F("Name: "));

  byte buffer1[18];

  block = 4;
  len = 18;

  //------------------------------------------- GET FIRST NAME
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  //PRINT FIRST NAME
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
    }
  }
  Serial.print(" ");

  //---------------------------------------- GET LAST NAME

  byte buffer2[18];
  block = 1;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  //PRINT LAST NAME
  for (uint8_t i = 0; i < 16; i++) {
    Serial.write(buffer2[i] );
  }


  //----------------------------------------

  Serial.println(F("\n**End Reading**\n"));

  delay(1000); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//

Please post the combined code.

#include <Wire.h> //include necessary libraries
#include <LiquidCrystal_I2C.h> //include I2C communication protocol
#include <SPI.h>
#include <MFRC522.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); //define address and size of lcd

int sen1=5; //input 1
int sen2=6; //input 2

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance


unsigned long t1=0; //time variable
unsigned long t2=0; //time variable
float velocity; //variable value 'velocity'
void setup()
{
  lcd.init();
	lcd.backlight(); //initiate lcd
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();   \
  delay(250);                                           // Init MFRC522 card
  Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read

	
  pinMode(sen1,INPUT); 
  pinMode(sen2,INPUT); //define pins 11 and 12 as inputs
  Serial.begin(9600); //initiate serial communication
  lcd.clear(); 
  lcd.setCursor(0,0); 
  lcd.print(" Speed Detector "); //write the msg at start


}

void loop() 
{
   
MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;
if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  

  byte buffer1[18];

  block = 4;
  len = 18;

  //------------------------------------------- GET FIRST NAME
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
     Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }




  Serial.println(F("**Card Detected:**"));

  //---------------------------------------- GET LAST NAME

  byte buffer2[18];
  block = 1;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  //PRINT LAST NAME
  for (uint8_t i = 0; i < 16; i++) {
    Serial.write(buffer2[i] );
  }


  //----------------------------------------

  Serial.println(F("\n**End Reading**\n"));

  delay(1000); //change value if you want to read cards faster

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


  while(digitalRead(sen1));
  while(digitalRead(sen1)==0);
  t1=millis();
  while(digitalRead(sen2));
  t2=millis();
  velocity=t2-t1;
  velocity=velocity/1000;//convert millisecond to second
  velocity=(1.0/velocity);//v=d/t
  velocity=velocity*3600;//multiply by seconds per hr
  velocity=velocity/1000;//division by meters per Km
  int sped = velocity;
   if (sped < 120)
  {
    
   lcd.setCursor(3,1);
   lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(3000);
  lcd.setCursor(3,1);
   lcd.print("            ");
  }  
  
   else if (sped > 120)
  {
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print("   OVERSPEED");
    Serial.println ("Violation Recorded");
        

    lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);

   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
   lcd.setCursor(3, 1);
    lcd.print(velocity);
   lcd.print(" Km/hr   ");
   delay(500);
   lcd.setCursor(3,1);
   lcd.print("            ");
   delay(500);
  lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("vehicle");
     
   lcd.setCursor(0, 1);
   lcd.print("penalised");
   delay(1000);
     lcd.clear();
     for (uint8_t i = 0; i < 16; i++) {
       lcd.clear();
       lcd.setCursor(0, 0);
    
    Serial.write(buffer2[i] );
    lcd.print(buffer2[i] );
  }
  delay(2000);
     
   lcd.setCursor(0, 0);
   lcd.print(" Speed Detector");
   delay(500);
    
  }
 
}

I am not able to get the lcd to display the RFID tag name

Try Serial.print of buffer2, not Serial.write.

Maybe your card reader is working, but when you display the data on the LCD, it is erased after every character:

    for (uint8_t i = 0; i < 16; i++) {
      lcd.clear();                    // <--- THIS IS ERASING THE LCD AFTER EVERY CHARACTER
      lcd.setCursor(0, 0);
      Serial.write(buffer2[i]);
      lcd.print(buffer2[i]);
    }

LOL.

It is also placing each character at exactly the same location on the screen!

Time for some code to move.

a7

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