Hi! So I’m still fairly new to this whole scene and I would like some help. Is there a way to say if these two analog inputs are within a certain range of each other then blah. So far I’ve tried averaging two different readings for each of the inputs and rounding. Here is some of my code that I commented to make my problem a bit more clear. Any help is great.

Robot__Forward_.ino (1.53 KB)

Two things strike me:

• Shouldn’t you check if “mid” is between 2 values, not equal to 1 specific? In your case, between say 900 and 1100. Chances of an exact match are slim.
• Which brings me to second point: 1000 is orders of magnitude out. Analog values go from 1 to 1000 (roughly): you would accept 2 as being the same if they were within (say) 10 or 50 or so, surely?

Could you explain you’re second point a bit more? I don’t entirely understand how the signals are handled. Thanks again.

analogRead gives valies from 0 (0 volts) to 1023 (5 volts). Let’s say one side reads 700 or so, the other reads 100. Clearly those are different and you would home to one side. But if the other side reads 650? Or 750? Those are probably close enough to mean “do nothing”. But is 600 or 800 close enough? Maybe… you decide.

But what you’re trying to do (assuming you fix the first == problem) is (I think) saying they’re the same if the difference is 1000, which is the whole scale of an analogRead.

What I would do is have a variable let’s call it “threshold” and initialise it to 50 or 100. Then have a line to see if your absolute difference is < threshold. (Reason I suggest using a variable is it’s easier to change: if you decide it should be 50 not 100 it’s easy to find at the top of the sketch, don’t have to hunt.)

``````int threshold = 100;
``````

And then change this:

``````if(round(mid) == 1000)

//.. to this:

if(mid < threshold)
``````

Above all off the top of my head as I get ready to leave for work… ymmv

Nice. Makes perfect sense now and it works. Thanks again man!

JavierC: Nice. Makes perfect sense now and it works. Thanks again man!