Stuck on comparators

Hello to all…
I’m stuck on a portion of code in the loop of a sketch to control a motor using a H-Bridge (L293NE)
Having an error around a fixed value, I want to move the motor in FWD/REV, if the error is above 2 units or below -2 units. In the mean time, if the error is equal os less than +2 or -2, I want the motor stop.
Here´s the code:

  if(error >= 2){
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  }
  if(error < 2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }
  if(error <= -2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  }
  if(error > -2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }

With the previous code, the motor moves when error reaches -2 units and stops correctly if error not exceeds that -2 value,… but it doesn´t rotates opposite when reaching +2 units.
Am I doing something wrong?..
It would be my poor knowledge implementing code… or a better code to do what I want…
Thanks in advance…

You should be using "else if" statements instead of using a bunch of "if" statements. This should fix things.

Yes, as mentioned use 'else if' .
When a compare is found in the chain, the other 'else ifs' are not run.

.

Thanks Power_Broker & LarryD…
This thing is driving me nuts… (sorry)…
I have change code in different ways using if else a bunch of fashions…
Please… see this code:

  if(error <= -2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  }
  else if(error >= -2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }
  if(error >= 2){
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  }
  else if(error <= 2){
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  }

This way it does oppsite from my first post:
It rotates when going >2 units and stops when below 2 (positive)… but it doesn´t starts when going negative …
Keep on trying gents… I´ll post any lucky result.

Try this:

if(error < -2)
{
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}
else if(error > 2)
{
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}
else
{
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Thanks a million Power_Broker...
It works!!!!...
I´m too old fo this games.. (64 and counting).
Things were a lot easier when vacuum tubes and analog resolvers...
I owe you a big ONE.

I´ll try to understand what I was doing... and keep learning.

No problem! Glad it works.

I´m too old for this games… (64 and counting)…
Those days when vacuum tubes and mechanical resolvers… :frowning:
Thanks again and… to complete the whole thing the goal is to turn the motor at intervals… In other words:

if(error < -2)
{
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(1000);
}
else if(error > 2)
{
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  delay(1000)
{
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

The idea is to use the mechanical motor output to re-align the error. So ¿ Is OK to insert a delay of milliseconds on each case waiting for realignment?.

Is OK to insert a delay of milliseconds on each case waiting for realignment?.

Using delay() is OK as long as you don't want the program to do anything else during the delay period.

Thanks UkHeliBob.. I have tried and is delaying the whole program.
I'll rename SOLVED this thread as far as the main goal is achieved.
Best regards

I have tried and is delaying the whole program.

As I warned.

Investigate the use of millis() for non blocking timing as in the BlinkWithoutDelay example and Several things at the same time

64, your the same age as many old *arts here, welcome to the kindergarten. :wink:

.

if(error > 2) {
  // error > 2
}
else if(error >= -2) {
  // error between -2 and 2 inclusive
}
else {
  // error < -2
}

pacheco:
I´m too old for this game …
I´ll try to understand what I was doing… and keep learning.

Hi pacheco. If you find too many chains of “if else if” confusing, then there is another way to structure it. If you make all the conditions mutually exclusive (only one is true) then you can keep it very simple. Sure it’s a little less run time efficient, but not enough to make any meaningful difference in most situations.

For example

if (error < -2) ...
if (error > 2) ...
if (error >=-2 && error <=2) ...