Saving the variable for some time instants

Hello everyone,

I am using a multiplexer, 4 electrodes and an atmega328p to observe capacitance change. I have one main capacitance variable across A0 and A2. By changing selective inputs of mux, im trying to get capacitance values of different electrodes from main capacitance variable like this: ( val is analog read across A0 and A2)

 for (int i=0; i<4; i++)
  {
    if(i==0)
    {
       digitalWrite(5, LOW);
   digitalWrite(6, LOW);


 while( digitalRead(5)!= LOW && digitalRead(6) != LOW){}
       
   val1 = val;
  
  
    tem1 = dtostrf(val1, 5, 0, buffer1);
    
      temper1 = (tem1);
    
String stringOne =  String("1):" + temper1);

  message.addUriRecord(stringOne);
digitalWrite(8,HIGH);
  digitalWrite(5, HIGH);
   digitalWrite(6, LOW);

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

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



 while( digitalRead(5)!= HIGH && digitalRead(6) != LOW){}
     
 val2=val;

      tem11 = dtostrf(val2, 5, 0, buffer2);
   
      temper11 = (tem11);
      
String stringTwo =  String("2)"+ temper11);

  message.addUriRecord(stringTwo);
digitalWrite(8,LOW);
     digitalWrite(5, LOW);
   digitalWrite(6, HIGH);
   
    }
   
   
      if(i==2)
    {
        digitalWrite(5, LOW);
   digitalWrite(6, HIGH);

  

  while( digitalRead(5)!= LOW && digitalRead(6) != HIGH){}

 
  val3=val;



      tem21 = dtostrf(val3, 5, 0, buffer3);
    
      temper21 = (tem21);
    
String stringThree =  String("3)" +temper21);

  message.addUriRecord(stringThree);
   digitalWrite(8,HIGH);
 digitalWrite(5, HIGH);
   digitalWrite(6, HIGH);
   
    }
  
      if(i==3)
    {
      digitalWrite(5, HIGH);
   digitalWrite(6, HIGH);
   
    

 while( digitalRead(5)!= HIGH && digitalRead(6) != HIGH){}

val4=val;

   tem31 = dtostrf(val4, 5, 0, buffer4);
   
      temper31 = (tem31);
    
String stringFour =  String("4)" + temper31);

  message.addUriRecord(stringFour);
 digitalWrite(8,LOW);
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
    }


    
  }

However, im getting same ADC value for 4 of them. I mean i cannot see each individual capacitances of electrodes. I am observing for example 85 for 4 of them. Does it mean that i cannot assign the val value to val(1234) at that time instant ?

Thank you,

Will you please post your entire code as well as use auto-format in the Arduino IDE. This will make it much easier to read and understand.

Thanks, – DH

Hey, thank you for your answer.
(Since im just testing the copying val data to another value for specific time instant, it is the test code of mine without using eeprom related things. Since i copied them from my main code to test code, they are still in the test code.)

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;
boolean off1 = false;
boolean off2 = false;
boolean off3 = false;
boolean off4 = false;

#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;




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);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);


  delay(100);


}
void loop()
{

  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 temper1 ;


  String tem11 ;

  String temper11 ;


  String tem21 ;

  String temper21 ;


  String tem31 ;

  String temper31 ;

  if (millis64() - lastTime >= interval) {
    // do something every second

    lastTime = millis64(); // reset last time fired
  }




  NdefMessage message = NdefMessage();

  char buffer1[20];
  char buffer2[20];
  char buffer3[20];
  char buffer4[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);


      while ( digitalRead(5) != LOW && digitalRead(6) != LOW) {}

      val1 = val;

      
      tem1 = dtostrf(val1, 5, 0, buffer1);

      temper1 = (tem1);

      String stringOne =  String("1):" + temper1);

      message.addUriRecord(stringOne);
      digitalWrite(8, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);

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

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



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



      while ( digitalRead(5) != HIGH && digitalRead(6) != LOW) {}

      val2=val;

      
      tem11 = dtostrf(val2, 5, 0, buffer2);

      temper11 = (tem11);

      String stringTwo =  String("2)" + temper11);

      message.addUriRecord(stringTwo);
      digitalWrite(8, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);

    }


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



      while ( digitalRead(5) != LOW && digitalRead(6) != HIGH) {}


      val3=val;
      


      tem21 = dtostrf(val3, 5, 0, buffer3);

      temper21 = (tem21);

      String stringThree =  String("3)" + temper21);

      message.addUriRecord(stringThree);
      digitalWrite(8, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);

    }

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



      while ( digitalRead(5) != HIGH && digitalRead(6) != HIGH) {}

      val4=val;

      tem31 = dtostrf(val4, 5, 0, buffer4);

      temper31 = (tem31);

      String stringFour =  String("4)" + temper31);

      message.addUriRecord(stringFour);
      digitalWrite(8, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
    }




  }

  m24sr.writeNdefMessage(&message);



}

I’m sorry but i don’t quite understand what is happening and what needs to happen.

With analogRead you will always get a value, regardless of whether there is any connection to the port or not. You should only try to verify working code with set values such as 0 and 5V, otherwise you never really know if you're getting valid data or the result of a floating input.

I'm more interested in what

digitalWrite(5, LOW);
digitalWrite(6, HIGH);
while ( digitalRead(5) != LOW && digitalRead(6) != LOW) {}

is designed to accomplish. There are several variations of this statement and all seem a bit odd. Are you certain that this is what you want? (Hint: think about parentheses, they're free, and semi-colons, also free, not to mention testing for a condition that could never exist on an output pin that you've just set LOW and want to wait while !LOW??)

DKWatson:
With analogRead you will always get a value, regardless of whether there is any connection to the port or not. You should only try to verify working code with set values such as 0 and 5V, otherwise you never really know if you're getting valid data or the result of a floating input.

I'm more interested in what

digitalWrite(5, LOW);
digitalWrite(6, HIGH);
while ( digitalRead(5) != LOW && digitalRead(6) != LOW) {}

is designed to accomplish. There are several variations of this statement and all seem a bit odd. Are you certain that this is what you want? (Hint: think about parentheses, they're free, and semi-colons, also free, not to mention testing for a condition that could never exist on an output pin that you've just set LOW and want to wait while !LOW??)

Actually, im getting the correct ADC value. I checked it by setting digital5&6 HIGH which means i selected 4th electrode via mux, and sampled a liquid on electrode. It was measuring 50-60 without nothing but it reached 700 when i sampled liquid.

digitalWrite(5, LOW);
digitalWrite(6, HIGH);
while ( digitalRead(5) != LOW && digitalRead(6) != LOW) {}

About this part, actually it was "while ( digitalRead(5) != LOW && digitalRead(6) != HIGH) {}" . I used such while loop to make the circuit wait until the pins are set as i wanted. Because, there was synchronization problem when i just used "digitalWrite(5, LOW); digitalWrite(6, HIGH);". If there is a synchronization problem, i might be hard to control mux selective inputs that is why i used such while loop.

My main problem is that i cannot get different values for val1, val2, val3 and val4. They are becoming same value although i made them equal to val in different time instants.

Thank you

They are becoming same value although i made them equal to val in different time instants.

As far as I can see you read val once in the code then use it in a for loop. What is it that introduces the element of time to the reading of val or the copying to val1, val2 etc ??