Switch Case Function Arrangement Question

Hello Everyone

I hope that all of you are doing well. I've written the following algorithm function to read and convert multiple values from EEPROM to it's original values for when the value is larger then 255. You'll need the write function to be able to implement the read function, but if anyone is interested I will share the code as soon as I'm done.

Now "Example 1" was my first attempt and when I looked over it, it didn't really make sense to me. It seemed to me that when the while loop is entered then the EEPROM is not going to read anything meaningful. I say this since no addresses have been assigned to Even_Uneven_Detector_Address, Multiplier_Address and Variable_Address to read the values and to assign them to specific variables at the stage of reading from the EEPROM.

Since then I've changed the function so that it looks as in "Example 2". In "Example 2" the addresses is assigned with a first case function and then the values are read from the respective addresses and the values are then assigned using a second case function to the respective variables

The code in my second example makes sense to me, I just want to confirm what I'm saying is correct or whether I'm going a bit way overboard and that I could have just used the code as in my first example? Just double checking here. Thanks for taking the time in advance :slight_smile:

Example 1:

    //*********************************************************************************************************************************************************    
    //**********************************************            Reading From EEPROM Function             ******************************************************    
    //*********************************************************************************************************************************************************       

    
    void Read_From_EEPROM()
    {
      Stage_Storage_Selector = 0; 
     
      while ( Stage_Storage_Selector <= 2 )
      { 
          Even_Uneven_Detector =   EEPROM.read(Even_Uneven_Detector_Address);      
          Multiplier           =   EEPROM.read(Multiplier_Address); 
          Variable             =   EEPROM.read(Variable_Address);      
      
          if ( Even_Uneven_Detector  ==  2 && Multiplier ==  2 )
          {
              Variable = ((Variable * 2) - 1);
          }
            
          if ( Even_Uneven_Detector  ==  1 && Multiplier ==  2 )
          {
              Variable = Variable * 2;
          }

          switch (Stage_Storage_Selector) 
          {
            case 0:           
              PREH_Temp                                   =  Variable;
              PREH_Temp_Address                           =  Variable_Address;
        
              PREH_Multiplier                             =  Multiplier;
              PREH_Multiplier_Address                     =  Multiplier_Address;
        
              PREH_Even_Uneven_Detector                    =  Even_Uneven_Detector;
              PREH_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;
            
            case 1:    
              HEAT_Temp                                   =  Variable;
              HEAT_Temp_Address                           =  Variable_Address;
        
              HEAT_Multiplier                             =  Multiplier;
              HEAT_Multiplier_Address                     =  Multiplier_Address;
        
              HEAT_Even_Uneven_Detector                    =  Even_Uneven_Detector;
              HEAT_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;
        
            case 2:           
              SLDR_Temp                                   =  Variable;
              SLDR_Temp_Address                           =  Variable_Address;
        
              SLDR_Multiplier                             =  Multiplier;
              SLDR_Multiplier_Address                     =  Multiplier_Address;
        
              SLDR_Even_Uneven_Detector                    =  Even_Uneven_Detector;
              SLDR_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;                            
          }
       Stage_Storage_Selector++;      
      }    
    }

Example 2:

    void Read_From_EEPROM()
    {
      Stage_Storage_Selector = 0; 
     
      while ( Stage_Storage_Selector <= 2 )
      {           
          switch (Stage_Storage_Selector)                  // First case is to read and assign the correct addresses to the various variables
          {
            case 0:           
              PREH_Temp_Address                            =  Variable_Address;
              PREH_Multiplier_Address                      =  Multiplier_Address;
              PREH_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;
            
            case 1:    
              HEAT_Temp_Address                            =  Variable_Address;
              HEAT_Multiplier_Address                      =  Multiplier_Address;
              HEAT_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;
        
            case 2:           
              SLDR_Temp_Address                            =  Variable_Address;
              SLDR_Multiplier_Address                      =  Multiplier_Address;
              SLDR_Even_Uneven_Detector_Address            =  Even_Uneven_Detector_Address;
              break;                     
          }
            
          Even_Uneven_Detector =   EEPROM.read(Even_Uneven_Detector_Address);      
          Multiplier           =   EEPROM.read(Multiplier_Address); 
          Variable             =   EEPROM.read(Variable_Address);      
      
          if ( Even_Uneven_Detector  ==  2 && Multiplier ==  2 )
          {
              Variable = ((Variable * 2) - 1);
          }
            
          if ( Even_Uneven_Detector  ==  1 && Multiplier ==  2 )
          {
              Variable = Variable * 2;
          }

          switch (Stage_Storage_Selector) 
          {
            case 0:           
              PREH_Temp                                   =  Variable;       
              PREH_Multiplier                             =  Multiplier;
              PREH_Even_Uneven_Detector                   =  Even_Uneven_Detector;
              break;
            
            case 1:    
              HEAT_Temp                                   =  Variable;
              HEAT_Multiplier                             =  Multiplier;
              HEAT_Even_Uneven_Detector                   =  Even_Uneven_Detector;
              break;
        
            case 2:           
              SLDR_Temp                                   =  Variable;
              SLDR_Multiplier                             =  Multiplier;
              SLDR_Even_Uneven_Detector                   =  Even_Uneven_Detector;
              break;                      
          }
      
          Stage_Storage_Selector++;      
      }    
    }

I see multiple issues with your code...

  1. Your function is relying heavily on global variables. As a result, you have quite a bit of code duplication in the process. Your function signature for Read_from_EEPROM() should take parameters so you can pass in what variable you want to read and return the value.

  2. I think you're making this more complicated than it has to be ;). If I understand you correctly, you want to store numbers > 255 in EEPROM. And currently you're using 3 bytes to do so (multilier, value & "odd" detector). Instead, just use something like this (untested):

void writeToEeprom(int base_address, unsigned int value)
{
    EEPROM.write(base_address, (byte)(value & 0xFF));
    EEPROM.write(base_address+1, (byte)((value >> 8) & 0xFF)))
}

unsigned int readFromEeprom(int base_address)
{
    unsigned int value = EEPROM.read(base_address);
    value += EEPROM.read(base_address+1) << 8;
    return value;
}

See: Arduino Playground - EEPROMWriteAnything

Hello Nick and Int2str

Thank you both for your feedback. Thanks Nick for the reference, I'll look into that.

@ Int2str

Yes I know I'm heavily relying on global variables at this stage, still getting back into coding so need to get some practice in with functions with returns and structures etc, but I'll get into it as I go along. But thank you for pointing that out for me.