Go Down

Topic: How to write 10 digits in arduino eeprom (Read 259 times) previous topic - next topic

Ahmed_Ali1955

hello
i tried to save 10 digits numbers in arduino eeprom but cannot anyone help me in this my sketch
Code: [Select]

Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

Measure the liquid/water flow rate using this code.
Connect Vcc and Gnd of sensor to arduino, and the
signal line to arduino digital pin 2.
 
 */
#include <EEPROM.h>
byte statusLed    = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount; 

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
{
 
  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);
   
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
 
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);


// ############################################
// ############################################
// Initiaize the value of totalMilliLiters
  if (EEPROM.read(1) == 0xFF){
  EEPROM.put(1,totalMilliLitres);
} else {
      EEPROM.get(1,totalMilliLitres);
}
// ############################################
}
/**
 * Main program loop
 */
void loop()
{
   
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
       
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
   
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
   
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;
   
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
     
    unsigned int frac;
   
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print("L/min");
    Serial.print("\t");       // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");       
    Serial.print(totalMilliLitres);
    Serial.println("mL");
    Serial.print("\t");       // Print tab space
  Serial.print(totalMilliLitres/1000);
  Serial.print("L");

  // ######################################################
  // ######################################################
  // Save the Value in EEPROM
    EEPROM.put(1,totalMilliLitres);           //  Was read value  EEPROM.get(1,totalMilliLitres);
  // ######################################################
  // ######################################################
    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
   
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

/*
Insterrupt Service Routine
 */
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

pert

It's very hard to read your code. Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

PaulS

Quote
i tried to save 10 digits numbers in arduino eeprom but cannot anyone help me in this my sketch
The code did something that only you can see. You expect the code to do something, but you didn't explain what you expect it to do.

I think that it is safe to assume that if the code did what you expected, you would not have said that you could not save the data in EEPROM.

So, I think that it is safe to assume that you don't think you have stored the data in EEPROM. But, without knowing why you think that, what the code actually does, and how that differs from what you expect it to do, we can't help you.
The art of getting good answers lies in asking good questions.

Ahmed_Ali1955

i think the code now more clear

Code: [Select]

#include <EEPROM.h>

unsigned long totalMilliLitres = 7825348931; // we need to save this Value

void setup()
{

// Initiaize the value of totalMilliLiters
  if (EEPROM.read(1) == 0xFF){
  EEPROM.put(1,totalMilliLitres);
} else {
      EEPROM.get(1,totalMilliLitres);
}
// ############################################
}

void loop()
{
   
 
    EEPROM.put(1,totalMilliLitres);           //  Was read value  EEPROM.get(1,totalMilliLitres);
     
   
    Serial.print("Output Liquid Quantity: ");       
    Serial.print(totalMilliLitres);
    Serial.println("mL");

}


PaulS

https://www.arduino.cc/reference/en/language/variables/data-types/unsignedlong/
Quote
Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
Code: [Select]
unsigned long totalMilliLitres = 7825348931;
Is that a valid value?

I still do not know what the code actually does. I still do not know what you expect it to do. I still do not know whether your expectations are reasonable (though I have my doubts).
The art of getting good answers lies in asking good questions.

Ahmed_Ali1955

i need to write this value in eeprom unsigned long totalMilliLitres = 7825348931;

PaulS

i need to write this value in eeprom unsigned long totalMilliLitres = 7825348931;
So you've said. That is NOT a valid value for an unsigned long on most Arduinos.

You still have NOT said what problem you are having.
The art of getting good answers lies in asking good questions.

GolamMostafa

#7
Jul 11, 2018, 06:02 pm Last Edit: Jul 11, 2018, 06:05 pm by GolamMostafa
i need to write this value in eeprom unsigned long totalMilliLitres = 7825348931;
1.  Please, read Post#4 and try to understand what the post has said.

From the Post#4, we can understand that --
(a) You have this decimal number: 7825348931. Write its equivalent binary number in hex form (hex is a compact form of binary, which we use as a matter of convenience): 0x01D26D5943. Here, we have 5-byte data -- 01, D2, 6D, 59, 43 -----> 5x8 = 40-bits.

(b)  You have declared data type unsigned long. It is a memory space which is composed of 4 consecutive memory locations. Each memory location can hold 8-bit data. So, the capacity of the space is maximum 4x8 = 32 bit. The value it can store is: 0x0000000 - 0xFFFFFFFF ----> 0 - 4,294,967,295. But, your number is 7 825 348 931 which is larger than 4,294,967,295. Now, I repeat the question of Post#4, is your number is a valid number for your totalMilliLitres variable? Can your variable hold this number under the given unsigned long data type?

(c)  What is the solution? You need a variable of larger data type -- say, 64-bit. You may make the following declaration :
Code: [Select]
unsigned long long totalMilliLitres = 7825348931; //0x01D26D5943
 

(d)  Now, execute the following codes to save/read back/display your data in the EEPROM (tested in UNO).
Code: [Select]
#include<EEPROM.h>

void setup()
{
  Serial.begin(9600);
  unsigned long long totalMilliLitres = 7825348931; //0x1D26D5943;
  
  union
  {
    unsigned long long y;
    byte dataArray[5];
  } myData;
  
  EEPROM.put(0x0100, totalMilliLitres);  //storing 7825348931
  EEPROM.get(0x0100, myData.y);  //reading back the number from EEPROM

  for (int i = 4; i >= 0; i--)
  {
    Serial.print(myData.dataArray[i], HEX); //Serial Monitor shows: 1D26D5943
  }
}

void loop()
{
  
}


2.  You may try to write a small program and post it here to show the value (that we have read back from EEPROM) in the Serial Monitor as: 7825348931 (decimal format).

Go Up