Issue in two integer array comparison

Thread Starter

ecaits

Joined Jan 6, 2014
56
Dear Sir,

I have stored setpoint value in EEPROM memory in by keypad and I want to compare setpoint value with process value. Process value is stored in one array, say data[5]. I read the setpoint value from EEPROM and again stored in another array, say presetvalue[5]. I am using below logic to compare the both value but it is not working properly. I have used RC0 pin, which is connected to LED. When PV is higher than SV then RC0 pin will be high so LED should be glow.


for(i=0;i<5;i++) // To read the setvalue from EEPROM
{
value=eeprom_read(i);
presetvalue=value; // Stored setvalue in presetvalue[5] array
}

for(i=0;i<5;i++) // Comparison logic for setvalue and process value

{
if(data > presetvalue) // When PV>SV, RC0 pin high
{
RC0=1;
i=5;
}


else if(data==presetvalue) // If equal then compare next bit (From MSB
to LSB)
{ RC0=1; }


else if(data < presetvalue) // When PV<SV, RC0 pin low
{
RC0=0;
i=5;
}
}

Plz suggest me where may be problem.

If there is another good logic, then plz suggest me.

Thanks in advance,
 

tshuck

Joined Oct 18, 2012
3,534
How are these values loaded? You may be having a big/little endian issue.

In what way is it not working?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
The way you set up the Comparison loop has it only check the zero element.

If it is > or < then i=5 and you drop out. Only when they are equal do you go onto the next element.

Is that OK or do you want to check all 5 elements?

In the future please use the code tag button ( the # symbol) so your code looks more like this:

Rich (BB code):
for(i=0;i<5;i++) // To read the setvalue from EEPROM
{
 value=eeprom_read(i);
 presetvalue=value; // Stored setvalue in presetvalue[5] array
} 
for(i=0;i<5;i++) // Comparison logic for setvalue and process value
{ 
 if(data > presetvalue) // When PV>SV, RC0 pin high
 { 
  RC0=1;
  i=5;
 }
 else if(data==presetvalue) // If equal then compare next bit (From MSB to LSB)
 { RC0=1; }
 
 else if(data < presetvalue) // When PV<SV, RC0 pin low
 {
  RC0=0;
  i=5;
 }
} 
 
Top