Problem while programming NFC (m24sr64)

Hello everyone,

Its another topic of my projects. As i have programming issues, i wanted to start a new topic.

I am trying to program my projects which i use atmega328p, NFC tag (m24sr64) and multiplexer (SN74LV4052APWR). I will get data from 4 different interdigited electrodes which have 8 legs in total. Therefore, i used dual multiplexer. When i make selective inputs, for example 00, it will select 1Y0 and 2Y0 which are the 2 legs of one electrodes. It perfectly suits to me. However, i don’t know how to program the arduino-multiplexer system. I need to check 4 of them and take action accordingly. I wrote a sample for loop to test but im not sure if it is true or not.

Second issue is related with NFC tag. I have to write the time info which will come from millis() function when there is a capacitance change on the electrodes. After getting the info, i have to write them to URI. However, i have some problems with “message.addUriRecord()” function. when i use only one electrode and use only one “message.addUriRecord()” function to write time info, it works perfectly. I can read written time info on URI via my mobile phone. However, i have 4 electrodes and i have 4 different time info. But using message.addUriRecord() more than once causes problem. It writes nothing to URI if i use 4 different message.addUriRecord() for 4 different time info strings. Moreover, while working with only one message.addUriRecord(); function, if i make its string too long, it doesn’t work.

In summary, i have to get 4 different time info as 4 different URI record. But i couldn’t get when i use 4.

Thank you so much.

Here is my codes (Due to character limitation, i have added the other half of the code as comment):

const int OUT_PIN = A2;
const int IN_PIN = A0;
const float IN_STRAY_CAP_TO_GND = 24.48;
const float IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND;
const float R_PULLUP = 34.8;  
const int MAX_ADC_VALUE = 1023;

#include <EEPROM.h>

int addr15 = 15;
int addr16 = 16;

int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}


uint64_t millis64() {
    static uint32_t low32, high32;
    uint32_t new_low32 = millis();
    if (new_low32 < low32) high32++;
    low32 = new_low32;
    return (uint64_t) high32 << 32 | low32;
}




#include <PN532.h>
#include <NfcAdapter.h>
#include <Wire.h>
#include <crc16.h>
#include <M24SR.h>
#define gpo_pin 7
M24SR m24sr(gpo_pin);

unsigned long lastTime = millis64();
unsigned long interval = 1000;

unsigned long oynabunla = millis64();


void setup()
{
  m24sr._setup();
  pinMode(OUT_PIN, OUTPUT);
  pinMode(IN_PIN, OUTPUT);
  Serial.begin(9600);
analogRead(IN_PIN);
pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);

  
  delay(100);
  
   
}
void loop()
{
 boolean off1 =false;
boolean off2 =false;
boolean off3 =false;
boolean off4 =false;

int addr1 = 5;
int addr2 = 6;
int addr3 =7;
int addr4 =8;

int addr11 = 15;
int addr12 = 16;
int addr13 =17;
int addr14 =18;

int addr21 = 25;
int addr22 = 26;
int addr23 =27;
int addr24 =28;

int addr31 = 35;
int addr32 = 36;
int addr33 =37;
int addr34 =38;

String tem1 ;
     String tem2 ;
      String tem3 ;
     String tem4 ;
      String temper1 ;
       String temper2 ;
        String temper3 ;
         String temper4 ;

         String tem11 ;
     String tem12 ;
      String tem13 ;
     String tem14 ;
      String temper11 ;
       String temper12 ;
        String temper13 ;
         String temper14 ;

         String tem21 ;
     String tem22 ;
      String tem23 ;
     String tem24 ;
      String temper21 ;
       String temper22 ;
        String temper23 ;
         String temper24 ;

         String tem31 ;
     String tem32 ;
      String tem33 ;
     String tem34 ;
      String temper31 ;
       String temper32 ;
        String temper33 ;
         String temper34 ;

if (millis64() - lastTime >= interval){
        // do something every second
 
        lastTime = millis64(); // reset last time fired
    }




   NdefMessage message = NdefMessage();

    char buffer[20];
  
   unsigned long Now = millis64()/1000;
  int Seconds1 = Now%60;
  int Minutes1 = Now/60;
  int Minutes = (Minutes1)%60;
  int Hours1 = Minutes/60;
  int Hours = (Hours1)%24;
  int Days1 = (Hours) /24;
    pinMode(IN_PIN, INPUT);
    digitalWrite(OUT_PIN, HIGH);
    int val = analogRead(IN_PIN);
    int val1;
    int val2;
    int val3;
    int val4;
    digitalWrite(OUT_PIN, LOW);

    if (val < 1000)
    {
      pinMode(IN_PIN, OUTPUT);

      float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);

      Serial.print(F("Capacitance Value = "));
      Serial.print(capacitance, 3);
      Serial.print(F(" pF ("));
      Serial.print(val);
      Serial.println(F(") "));
    }
    else
    {
      pinMode(IN_PIN, OUTPUT);
      delay(1);
      pinMode(OUT_PIN, INPUT_PULLUP);
      unsigned long u1 = micros();
      unsigned long t;
      int digVal;

      do
      {
        digVal = digitalRead(OUT_PIN);
        unsigned long u2 = micros();
        t = u2 > u1 ? u2 - u1 : u1 - u2;
      } while ((digVal < 1) && (t < 400000L));

      pinMode(OUT_PIN, INPUT);  
      val = analogRead(OUT_PIN);
      digitalWrite(IN_PIN, HIGH);
      int dischargeTime = (int)(t / 1000L) * 5;
      delay(dischargeTime);   
      pinMode(OUT_PIN, OUTPUT);  
      digitalWrite(OUT_PIN, LOW);
      digitalWrite(IN_PIN, LOW);

      float capacitance = -(float)t / R_PULLUP
                              / log(1.0 - (float)val / (float)MAX_ADC_VALUE);

      Serial.print(F("Capacitance Value = "));
      if (capacitance > 1000.0)
      {
        Serial.print(capacitance / 1000.0, 2);
        Serial.print(F(" uF"));
      }
      else
      {
        Serial.print(capacitance, 2);
        Serial.print(F(" nF"));
      }
 
        Serial.print(F(" ("));
      Serial.print(digVal == 1 ? F("Normal") : F("HighVal"));
      Serial.print(F(", t= "));
      Serial.print(t);
      Serial.print(F(" us, ADC= "));
      Serial.print(val);
      Serial.println(F(")"));
    }
    while (millis64() % 1000 != 0)
      ;
 for (int i=0; i<4; i++)
  {
    if(i==0)
    {
       digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   val1 = val;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (off1 == false && val1>150 && i==0)
{
  off1=true;
  EEPROM.write(addr1, Seconds1);
  EEPROM.write(addr2, Minutes1);
  EEPROM.write(addr3, Hours1);
  EEPROM.write(addr4, Days1);

   int vvalue1 = EEPROM.read(addr1);
     int vvalue2 = EEPROM.read(addr2);
     int vvalue3 = EEPROM.read(addr3);
     int vvalue4 = EEPROM.read(addr4);

      tem1 = dtostrf(vvalue1, 5, 0, buffer);
     tem2 = dtostrf(vvalue2, 5, 0, buffer);
       tem3 = dtostrf(vvalue3, 5, 0, buffer);
      tem4 = dtostrf(vvalue4, 5, 0, buffer);
      temper1 = (tem1);
      temper2 = (tem2);
        temper3 = (tem3);
         temper4 = (tem4);
String stringOne =  String("Time past for 1-)Days:" + temper4 + " Hours:" + temper3 + " Minutes:" + temper2 + " Seconds:" + temper1);

  message.addUriRecord(stringOne);

}

   
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }

    
    if(i==1)
    {
       digitalWrite(5, HIGH);
   digitalWrite(6, LOW);
   val2=val;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  if (off2 == false && val2>150 && i==1)
{
  off2=true;
  EEPROM.write(addr11, Seconds1);
  EEPROM.write(addr12, Minutes1);
  EEPROM.write(addr13, Hours1);
  EEPROM.write(addr14, Days1);

   int vvalue11 = EEPROM.read(addr11);
     int vvalue12 = EEPROM.read(addr12);
     int vvalue13 = EEPROM.read(addr13);
     int vvalue14 = EEPROM.read(addr14);

      tem11 = dtostrf(vvalue11, 5, 0, buffer);
     tem12 = dtostrf(vvalue12, 5, 0, buffer);
       tem13 = dtostrf(vvalue13, 5, 0, buffer);
      tem14 = dtostrf(vvalue14, 5, 0, buffer);
      temper11 = (tem11);
      temper12 = (tem12);
        temper13 = (tem13);
         temper14 = (tem14);
String stringTwo =  String("Time past for 2-)Days:" + temper14 + " Hours:" + temper13 + " Minutes:" + temper12 + " Seconds:" + temper11);

message.addUriRecord(stringTwo);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

   
    }
      if(i==2)
    {
       digitalWrite(5, LOW);
   digitalWrite(6, HIGH);
   val3=val;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


 if (off3 == false && val3>150 && i==2)
{
  off3=true;
  EEPROM.write(addr21, Seconds1);
  EEPROM.write(addr22, Minutes1);
  EEPROM.write(addr23, Hours1);
  EEPROM.write(addr24, Days1);

   int vvalue21 = EEPROM.read(addr21);
     int vvalue22 = EEPROM.read(addr22);
     int vvalue23 = EEPROM.read(addr23);
     int vvalue24 = EEPROM.read(addr24);

      tem21 = dtostrf(vvalue21, 5, 0, buffer);
     tem22 = dtostrf(vvalue22, 5, 0, buffer);
       tem23 = dtostrf(vvalue23, 5, 0, buffer);
      tem24 = dtostrf(vvalue24, 5, 0, buffer);
      temper21 = (tem21);
      temper22 = (tem22);
        temper23 = (tem23);
         temper24 = (tem24);
String stringThree =  String("Time past for 2-)Days:" + temper24 + " Hours:" + temper23 + " Minutes:" + temper22 + " Seconds:" + temper21);

 message.addUriRecord(stringThree);

  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

   
    }


    
      if(i==3)
    {
       digitalWrite(5, HIGH);
   digitalWrite(6, HIGH);
   val4=val;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



 if (off4 == false && val4>150 && i==3)
{
  off4=true;
  EEPROM.write(addr31, Seconds1);
  EEPROM.write(addr32, Minutes1);
  EEPROM.write(addr33, Hours1);
  EEPROM.write(addr34, Days1);

   int vvalue31 = EEPROM.read(addr31);
     int vvalue32 = EEPROM.read(addr32);
     int vvalue33 = EEPROM.read(addr33);
     int vvalue34 = EEPROM.read(addr34);

      tem31 = dtostrf(vvalue31, 5, 0, buffer);
     tem32 = dtostrf(vvalue32, 5, 0, buffer);
       tem33 = dtostrf(vvalue33, 5, 0, buffer);
      tem34 = dtostrf(vvalue34, 5, 0, buffer);
      temper31 = (tem31);
      temper32 = (tem32);
        temper33 = (tem33);
         temper34 = (tem34);
String stringFour =  String("Time past for 2-)Days:" + temper34 + " Hours:" + temper33 + " Minutes:" + temper32 + " Seconds:" + temper31);

  message.addUriRecord(stringFour);
 
}

   
    }
  }



if( val1>150 || val2>150 || val3>150 || val4>150)
{

  m24sr.writeNdefMessage(&message);
}

}

[/code]

Update:

I rearranged the code to decrease the number of strings as:

  EEPROM.write(addr11, Seconds1);
  EEPROM.write(addr12, Minutes1);
  EEPROM.write(addr13, Hours1);
  EEPROM.write(addr14, Days1);

   int vvalue11 = EEPROM.read(addr11);
     int vvalue12 = EEPROM.read(addr12);
     int vvalue13 = EEPROM.read(addr13);
     int vvalue14 = EEPROM.read(addr14);


     char cstr11[16];
char cstr12[16];
char cstr13[16];
char cstr14[16];
String tt11=itoa(vvalue11, cstr1, 10);
String tt12=itoa(vvalue12, cstr1, 10);
String tt13=itoa(vvalue13, cstr1, 10);
String tt14=itoa(vvalue14, cstr1, 10);

But now, I can write this for 2 times:

 String stringTwo =  String("Days:" + tt14 + " Hours:" + tt13 + " Minutes:" + tt12 + " Seconds:" + tt11);

But when i make it 3 times, it causes error and doesn't write 3rd one. I have to use 4 times.

When i use 2 times, the free ram stays same after writing, but when i use 3 times, the ram decreases dramatically after writing.