Go Down

Topic: Compare if two variables are equal (Read 141 times) previous topic - next topic

goerth

Jun 05, 2019, 06:28 am Last Edit: Jun 05, 2019, 06:31 am by Goerth

I have this program that reads data from a plc and then send them to a database, the received value changes until a button is pressed, so if the button is not pressed the same value is maintained and therefore value is sent several times. how could compare the previous data with the new to know if they are or are not equal, if they are the same do not send the data and if the value changes send the data. How could I make the comparison?

CODE:

Code: [Select]
int result =  node.readInputRegisters(address, bytesQty);

if (result != 0) {
  USB.println("Communication error");
  delay(1000);
}
else
{
  USB.print(F("Read value: "));
  USB.println(node.getResponseBuffer(0));
  frame.createFrame(ASCII); 

frame.addSensor(SENSOR_MODBUS_HOLDING_REGS, (int) node.getResponseBuffer(0));
  delay(1000);
}
delay(1000);
node.clearResponseBuffer();


error = xbee802.send( RX_ADDRESS, frame.buffer, frame.length );   

if( error == 0 )
{
  USB.println(F("Envio Correcto"));
  Utils.blinkGreenLED(); 
}
else
{
  USB.println(F("Envio Erroneo"));
  Utils.blinkRedLED();
}

delay(60000);


Regards

sterretje

You provide just a part of your code. Except for the fact that I can see that you use an Xbee, I have e.g. no reference to e.g. what node is and what getResponseBuffer returns.

You can look at the state change detection example that comes with the IDE. It's meant for buttons but the same principles apply to any type of input. Simply store the last reading if it differs from the previous one and send data if it differs from the previous one.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

UKHeliBob

Quote
how could compare the previous data with the new to know if they are or are not equal
Your question surely answers itself.  Save the value received, then next time a value is received compare the current value with the previous value.  Act accordingly if they are the same or different.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up