2

Consider:

#include <EEPROM.h>

byte guifactor1 = 1;
byte guifactor2 = 2;
byte guifactor3 = 3;
byte guifactor4 = 4;
byte guifactorgas = 5;
byte guifactorwater = 6;

volatile unsigned long count1factor;
volatile unsigned long count2factor;
volatile unsigned long count3factor;
volatile unsigned long count4factor;
volatile unsigned long countgasfactor;
volatile unsigned long countwaterfactor;

void loop(){
if (guifactor1 > -1){
    EEPROM.write(1, guifactor1);
    EEPROM.commit();
    count1factor = EEPROM.read(1);
    Serial.println(count1factor);
  }

  if (guifactor2 > -1){
    EEPROM.write(2, guifactor2);
    EEPROM.commit();
    count2factor = EEPROM.read(2);
    Serial.println(count2factor);
  }

  if (guifactor3 > -1){
    EEPROM.write(3, guifactor3);
    EEPROM.commit();
    count3factor = EEPROM.read(3);
    Serial.println(count3factor);
  }

  if (guifactor4 > -1){
    EEPROM.write(4, guifactor4);
    EEPROM.commit();
    count4factor = EEPROM.read(4);
    Serial.println(count4factor);
  }

  if (guifactorgas > -1){
    EEPROM.write(5, guifactorgas);
    EEPROM.commit();
    countgasfactor = EEPROM.read(5);
    Serial.println(countgasfactor);
  }

  if (guifactor1 > -1){
    EEPROM.write(6, guifactorwater);
    EEPROM.commit();
    countwaterfactor = EEPROM.read(6);
    Serial.println(countwaterfactor);
  }
}

void setup(){

}

The problem I have with my code is that it either doesn’t write the value to the EEPROM or it doesn’t read the value as it doesn’t print anything to the monitor.

I expected to read 1 2 3 4 5 6 from the monitor as I write the bytes to the EEPROM and then transfer the value to the unsigned long and print the variable.

9
  • What problem do you actually have? You didn't include any problem description. From your code I would say: guifactor1 and its siblings will be implicitly initialized with zero (since they are global and you don't explicitly initialize them). Checking guifactor1 > -1 won't ever be true, since you initialized them to zero and don't change them anymore, and also they are unsigned longs, so they cannot ever carry a negative value. You should rethink the logic of your program.
    – chrisl
    Commented May 6, 2022 at 10:13
  • 1
    Your problem description is still too vague. Please describe exactly what you expected the code to do and what is actually did. Provide the expected and actual output. And write a minimal compilable example code, that shows your problem. We cannot debug code that we cannot see
    – chrisl
    Commented May 6, 2022 at 11:34
  • 1
    If you are asking just about the code lines reading and writing the EEPROM, then the answer is "Nothing is wrong with it." The problem must be somewhere in the code that you refuse to show. And we just cannot help you with that. Our mind reading powers are limited.
    – chrisl
    Commented May 6, 2022 at 14:23
  • 1
    the problem is solved
    – jay
    Commented May 6, 2022 at 14:57
  • 1
    You know that EEPROMs are limited in the number of write operation, do you? As you're writing to EEPROM in loop without any delay, this code is one way to kill the EEPROM quite fast.
    – Sim Son
    Commented May 6, 2022 at 17:44

1 Answer 1

5

You forgot to call Serial.begin() in setup().

As a side note, your if tests are useless. The compiler warns me that:

comparison is always true due to limited range of data type

For reference: the previous answer, valid for the initial question where guifactor1 and co. were all unsigned long.


The program does nothing because all the actions are conditioned on tests such as

if (guifactor1 > -1)

and these tests are always false. The reason they are false is because arithmetic comparison operators such as < perform usual arithmetic conversions on their operands prior to doing the actual comparison. Since guifactor1 is unsigned long, and this type has a higher conversion rank than int (the type of -1), the second operand is implicitly converted to unsigned long, yielding ULONG_MAX.

5
  • this is not my entire code. i didnt upload all of it because it could be a bit overwelming. i edited the code above so that the problem is a bit more clear i also changed the unsigned long to a byte.
    – jay
    Commented May 6, 2022 at 11:29
  • 1
    @JayvandeWetering: Please, test the program you posted (not your original code, but the exact code that is in your question, as you just amended it) and make sure it does show the incorrect behavior. Commented May 6, 2022 at 11:33
  • i have tested the program i posted and it still either doesnt write or doesnt read from the eeprom
    – jay
    Commented May 6, 2022 at 11:35
  • 1
    @JayvandeWetering: How do you know it doesn't write or doesn't read? Please update your question with the relevant details. Commented May 6, 2022 at 11:38
  • i think i updated the question enough so that it is clear what is expected and what is the actual output
    – jay
    Commented May 6, 2022 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.