# Not sure what I'm doing wrong with my If statement?

I'm measuring the x error (xE) and determine the sign (xS). If eX<0 then xS = -1 else 1. Then I compare it to the previous sign (xSp) which I store at the end. If they are the same sign I then want to add the x error to the total error (ixE) unless they are not the same which I want the total error to be set to zero.

What happens, though, I can't troubleshoot. The signs appear to be one cycle late (xS) or early (xSp) but consistent each time... Also, if the signs are dissimilar it does not set the total error to zero?? Using the serial monitor I can see when the signs should be negative, positive, or dissimilar but it just isn't catching them. It must be something I'm doing wrong...

Best Regards,

Code:

``````  xE =analog.read... //reads x axis of touch screen
delay(1-100) //doesn't seem to make a difference
if (xE<0) {xS =-1;}
else {xS = 1;}
if (xS = xSp) {ixE = ixE + xE;}
else {ixE = 0;}
if (xE<0) {xSp =-1;}
else {xSp = 1;}

Serial Monitor:
(xE)  (xS)   (xSp)     (ixE)
-26534	-1	-1	-131658
-19942	-1	-1	-151600
-12764	-1	-1	-164364
-5440	-1	-1	-169804
1738    -1	 1	-168066
8769	 1	 1	-159297
15654	 1	 1	-143643
19462	 1	 1	-124181
21220	 1	 1	-102961
20927	 1	 1	-82034
20488	 1	 1	-61546
16972	 1	 1	-44574
10966	 1	 1	-33608
4375     1	 1	-29233
-3096	 1	-1	-32329
-10567	-1	-1	-42896
-18038	-1	-1	-60934
-24336	-1	-1	-85270
-30196	-1	-1	-115466
-34590	-1	-1	-150056
-36934	-1	-1	-186990
``````

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.

It will be formatted in a scrolling window that makes it easier to read.

What is the application, there may be a simpler way of doing it?

Thanks.. Tom..

if (xS = xSp) should be if (xS ==xSp)

Hope that helps

noweare

Thank you! something so simple. Also, the misaligned signs were because I had the serial.print at the end so it was taking a snapshot after everything completed or updated.