Posted 5 Jun 2023 Hello all, we are building a solar tracker as part of a university project and we have problems comparing some values. We have two photoresistors that will be outputing two values between 0 and 10 volts. What we want to do is that if one is higher than the other to turn a motor in one direction but if it is the other way around, so that in turns a different direction. However, if the difference between them is lower than a set value, we want it to stay stationary (like a deadzone). We have the first part figured out but we have trouble with the deadzone. When substracting the two values we get negative values and we don't know how to handle negative hexadecimal values. Could we substract them but have the answer as an absolute differences? How could we do this? Thanks for the answers! Share this post Link to post Share on other sites
Posted 5 Jun 2023 3 hours ago, igotz57 said: handle negative hexadecimal values PLC's use two's complement arithmetic like all modern computers. When viewed in hex, I find it best to think of an odometer that has hex digits instead of decimal, so rolling backward from zero yields FFFF (or 32-bit FFFFFFFF) and then continues counting down. Most PLC editors will simply show such values as negative decimal integers so you don't have to worry about the rollover. Share this post Link to post Share on other sites
Posted 5 Jun 2023 Instead of comparing after the subtraction you need to compare before and then perform the subtraction based on which value is higher. Example: If A < B then compare B-A If B < A then compare A-B You'll be seeing variable roll-over otherwise like pturmel said Share this post Link to post Share on other sites
Posted 5 Jun 2023 I would simply subtract B from A to get the delta as D. Then I would determine if outside the deadband by checking (D>DBpos) || (D<DBneg). DBpos and DBneg being constants, preferably. The latter being negative. Then motor control is: if outside deadband and Dsign is off, run forward. If outside deadband and Dsign is on, run backward. Where Dsign is the high bit of the delta value. (Or vice versa, per your setup.) The above is best when the motor control is "run" and "direction". If the motor control is "run forward" and "run reverse" as separate coils, you can use the halves of the deadband check directly. Share this post Link to post Share on other sites
Posted 7 Jun 2023 So, if I have a value of -500 this would be 01F4 and limits of -250 and +250, FF06 and 00FA respectively. I would check if 01F4<FF06 and 01F4>00FA. And as the first one is true, the motor would turn one way, but if the other one were to be true, so motor would spin the other way. We are using Cx programmer and it seams that we have to enter hexadecimal values directly, but if we can use the two complement's method we'll have no problem. On the other hand, getting rid of the negative values as photovoltaic has suggested would be interesting too. We'll try both methods to see which is the most suitable for us. Thanks you both! Share this post Link to post Share on other sites