Reading values from EEPROM

lets say you save the numbers 3,5,9 and 20 on a txt file using coolterm, is it possible to create a if statment on void loop so that if the number entered on a keypad is any of the numbers in this txt file... then do this else do that bla bla?

Eg.

if(keyentry == txtfilecodes)
{
opendoor();

}
else
{
serial.print("Fail");

}

For a more better understanding, my project reqires car parking customers to enter their phone number before entering the car parking area. A random code will be sent to their phone so that they could use that code to exit the parking area. So everytime I generate a random code for people that want to park their car, I want to save these random generated codes to a txt file and then do this if statment.

is it possible to create a if statment on void loop so that if the number entered on a keypad is any of the numbers in this txt file... then do this else do that bla bla?

Only if teh Arduino can access the file which it can't if it is on a PC unless the PC actually sends the contents of the file to the Arduino which I suspect that Coolterm cannot do

Have you considered other methods of storing the data such as an SD card attached to the Arduino, EEPROM or SPIFFS if using a suitable board ?

UKHeliBob:
Only if teh Arduino can access the file which it can't if it is on a PC unless the PC actually sends the contents of the file to the Arduino which I suspect that Coolterm cannot do

Have you considered other methods of storing the data such as an SD card attached to the Arduino, EEPROM or SPIFFS if using a suitable board ?

Hi thanks for your reply, I have not tried these other methods, what do you recommen me?

The easiest is probably EEPROM as it requires no more hardware but each memory location has a guaranteed lifetime of 100,000 writes which for some applications could be a limitation

Which Arduino board do you have ?

UKHeliBob:
The easiest is probably EEPROM as it requires no more hardware but each memory location has a guaranteed lifetime of 100,000 writes which for some applications could be a limitation

Which Arduino board do you have ?

Thanks Bob, I watched a video tutorial on EEPROM looks like a good option. I have a arduino mega with 4KB EEPROM.

Give it a try. You will learn something even if you do not use EEPROM as the final solution

You do not save a file to EEPROM, rather you save values to a location. You mention values such as 3,5,9 and 20. If the value is never above 255 then you can hold them in a single byte and use EEPROM.write() and read(). You don't actually need to save the number itself, you could save say 1 to location 20 to indicate that the number was allocated and 0 when it is not allocated. It is up to you

2donboi2:
So everytime I generate a random code for people that want to park their car, I want to save these random generated codes to a txt file and then do this if statment.

It doesn't sound like you need to store much data, and I guess you should be able to put that in an array. The PC file would be no more than a long term backup, wouldn't it?

You could have the Arduino call the phone itself and skip the entire file thing.

Feather FONA ← Arduino with cellphone built in. Knock yourself out!

-jim lee

UKHeliBob:
Give it a try. You will learn something even if you do not use EEPROM as the final solution

You do not save a file to EEPROM, rather you save values to a location. You mention values such as 3,5,9 and 20. If the value is never above 255 then you can hold them in a single byte and use EEPROM.write() and read(). You don't actually need to save the number itself, you could save say 1 to location 20 to indicate that the number was allocated and 0 when it is not allocated. It is up to you

Actually for my project I need to save 5 digit values, is that possible?

for my project I need to save 5 digit values, is that possible?

Yes, of course. Are they numbers or are they a string ? Saving and testing for either is possible.

How many different numbers/strings do you need to save and what is the maximum value that you anticipate saving ? When does the number/string get removed from wherever you saved it ? Removing a number or string from a file will be more complicated that removing it from EEPROM

UKHeliBob:
Yes, of course. Are they numbers or are they a string ? Saving and testing for either is possible.

How many different numbers/strings do you need to save and what is the maximum value that you anticipate saving ? When does the number/string get removed from wherever you saved it ? Removing a number or string from a file will be more complicated that removing it from EEPROM

I tried storing a big number but it doesnt work, I tried to store 500 but it stored 244.

for example the code below when val = 5 it will store 5 at location 0. but if i try storenumber bigger than 255 it will not store that number it will store another.

#include <EEPROM.h>

int addr = 0;
byte val;

void setup() {

Serial.begin(9600);
while (!Serial)
{
}

}

void loop()
{
int val = 5;
EEPROM.write(addr, val);
val = EEPROM.read(addr);
Serial.print(addr);
Serial.print("\t");
Serial.print(val, DEC);
Serial.println();
while(1);
}

if i try storenumber bigger than 255

That is because val is a byte and can only hold up to 255.

Use an unsigned int. Use EEPROM.put() and get() to save/load the value but remember that saving an int takes 2 bytes so saving 2 or more numbers needs the EEPROM location moving on by 2 bytes

2 topics merged so that the question in post #12 can be seen in context

Have you managed to save and load a series of numbers in the required range ? If so, then please post your code so far

UKHeliBob:
That is because val is a byte and can only hold up to 255.

Use an unsigned int. Use EEPROM.put() and get() to save/load the value but remember that saving an int takes 2 bytes so saving 2 or more numbers needs the EEPROM location moving on by 2 bytes

IT works thanks!!!

What's holding you back?

Duplicate topics merged AGAIN

@2donboi2 - stop opening new topics on the same subject !

I have a couple values saved on my EEPROM, I want to do an if statment so if the number entered on keypad is = to ANY of the values stored in the EEPROM blbla else blabla.

E.g
if (EnteredNum == EEPROMValues)
{
openservo();
}
else
{
Serial.monitor("Wrong Number Code");
}

Without seeing your code, my advise would be to loop through the values stored in EEPROM and compare with EnteredNum.
Something like

bool found = false;
for (int index = 0 ; index < EEPROM.length()  ; ++index)
{
  if(EnteredNum == EEPROM.read(index))
  {
    found = true;
    break;
  }

  if(found == true)
  {
    openservo();
  }
  else
  {
    Serial.println("Wrong number");
  }
}

Now it depends what your numbers are (bytes, ints, longs) if the above wil work or not.

How many more times ?

Stop creating duplicate topics and enjoy a short rest from the forum

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.