Problem using multiple 'if' statements

I am in the process of a project and rather than sit here and explain it, I hooked up L.E.D.s in place of everything else.

Essentially, I have a joystick and when I roll the joystick forward I want 1 L.E.D. to illuminate and when I roll the joystick backwards, I want another L.E.D. to illuminate. When the joystick is in the middle position, I don't want either of them on.

I know that when the Joystick is stationary, the value is 510.

  if (analogRead (JoyY) > 530) {
      digitalWrite (led2, HIGH); }
     
   
   if (analogRead (JoyY) < 490) {
      digitalWrite (led4,HIGH); }  
     
     else {
      digitalWrite (led2,LOW); 
      digitalWrite (led4,LOW);
   }

Is this just something simple I'm missing?

When I roll the joystick forward, nothing happens. When I roll it backwards, things work as intended.

I eliminated any possible faulty components / wiring because the intention is to have this working with a motor, and the motor is behaving in the exact same manner.

I'm still fairly new to Arduino so please bear with me on this stuff.

As written, if JoyY is >=490, both LEDs get written low - even if LED2 had just been written high.

   if (analogRead (JoyY) < 490) {
      digitalWrite (led4,HIGH); }  
     
     else {
      digitalWrite (led2,LOW); 
      digitalWrite (led4,LOW);
   }

Break up the else , use one for each if.

CrossRoads:
Break up the else , use one for each if.

Breaking up the 'else' statements seems to have fixed the problem. Thanks!!

But for some reason now, I can't figure out how to include this into my project.

I guess I should just try and explain.

I'm using an H-bridge and I wired up a motor so it can go forward and backwards depending on the value of the joystick.

Problem is in order for the motor to stop spinning, both it's pins must be set to either 'HIGH' or 'LOW'.

Any clue on how I could adapt that?

I had this:

if (analogRead(JoyY) > 530) {
      digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(Rmotor2Pin, HIGH);  // set leg 2 of the H-bridge high
    } 
   
      
    if (analogRead(JoyY) < 490) {
      digitalWrite (Rmotor1Pin,HIGH);
      digitalWrite (Rmotor2Pin,LOW);
    }
      
  else {
      digitalWrite (Rmotor1Pin,LOW);
      digitalWrite (Rmotor2Pin,LOW); }

but after reading your reply, I thought I would go and see if I could think of something else. I thought of maybe trying this.

if (analogRead (JoyY) > 530) {
      digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(Rmotor2Pin, HIGH);    
      digitalWrite (led4, HIGH); }
     
    if ( 490 < analogRead(JoyY) < 530 ) {
     digitalWrite (Rmotor1Pin,LOW);
     digitalWrite (Rmotor2Pin,LOW);
     digitalWrite (led4,LOW);
     digitalWrite (led3,LOW);
    }
    
   if (analogRead (JoyY) < 490) {
      digitalWrite (Rmotor1Pin,HIGH);
      digitalWrite (Rmotor2Pin,LOW);
      digitalWrite (led3,HIGH); }

It's not working and the L.E.D.s are giving me a good visual of what is happening. I just don't know what to do to fix it.

position = analogRead (JoyY);  // read the joystick position once, make a decision
if (position > 600) {
      digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(Rmotor2Pin, HIGH);  // set leg 2 of the H-bridge high
    } 
   
    if (position < 500) {
      digitalWrite (Rmotor1Pin,HIGH);
      digitalWrite (Rmotor2Pin,LOW);
    }
      
  if ( (position >=500) && (position <= 600) ) // in the no-movement center zone? 
{
      digitalWrite (Rmotor1Pin,LOW);
      digitalWrite (Rmotor2Pin,LOW); 
}

FullyJosh:
I know that when the Joystick is stationary, the value is 510.

what is this value??! and how did u know it ..... ??

sorry for that stupid question

Hani:

FullyJosh:
I know that when the Joystick is stationary, the value is 510.

what is this value??! and how did u know it ..... ??
sorry for that stupid question

that is just the analogRead value, i think that's clear...

couldn't yo ujust do this (instead of CrossRoads' code)

position = analogRead (JoyY);  // read the joystick position once, make a decision
if (position > 600) 
{
  digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
  digitalWrite(Rmotor2Pin, HIGH);  // set leg 2 of the H-bridge high
} 
else if (position < 500) 
{
  digitalWrite (Rmotor1Pin,HIGH);
  digitalWrite (Rmotor2Pin,LOW);
}
else
{
  digitalWrite (Rmotor1Pin,LOW);
  digitalWrite (Rmotor2Pin,LOW); 
}

because this covers every case completely, which is better imo, but it doesn't make any difference on the normal functioning

CrossRoads:

position = analogRead (JoyY);  // read the joystick position once, make a decision

if (position > 600) {
      digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(Rmotor2Pin, HIGH);  // set leg 2 of the H-bridge high
    }
   
    if (position < 500) {
      digitalWrite (Rmotor1Pin,HIGH);
      digitalWrite (Rmotor2Pin,LOW);
    }
     
  if ( (position >=500) && (position <= 600) ) // in the no-movement center zone?
{
      digitalWrite (Rmotor1Pin,LOW);
      digitalWrite (Rmotor2Pin,LOW);
}

Thank you very much for the super fast reply. I didn't get the chance to actually test the code until now but it I just did and it works flawlessly. I really appreciate your help!

Hani:

FullyJosh:
I know that when the Joystick is stationary, the value is 510.

what is this value??! and how did u know it ..... ??

sorry for that stupid question

Someone posted this code on Sparkfun so I just incorporated it into my code.

 /* Released to public domain */  
const int selectPin = 2;  
const int joystick_xPin = A0;  
const int joystick_yPin = A5;  
int oldX = 0;  
int oldY = 0;  
int oldSelect = 0;  
void setup()  
{  
  pinMode(selectPin, INPUT);  
  digitalWrite(selectPin, HIGH);  
  Serial.begin(9600);  
}
void loop()
{  
  int joystick_x;  
  int joystick_y;  
  int select;  
  joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);  
  joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);  
 select = !digitalRead(selectPin);  
  if((oldX != joystick_x) ||  
      (oldY != joystick_y) ||  
     (oldSelect != select)){  
       Serial.print("joystick X: ");  
       Serial.print(joystick_x);  
       Serial.print(" joystick Y: ");  
       Serial.print(joystick_y);  
       if(select){  
          Serial.print(" select");  
       }  
       Serial.println("");  
       oldX = joystick_x;  
       oldY = joystick_y;  
       oldSelect = select;  
  }  
 delay(10);  
}

Steen:
couldn't yo ujust do this (instead of CrossRoads' code)

position = analogRead (JoyY);  // read the joystick position once, make a decision

if (position > 600)
{
  digitalWrite(Rmotor1Pin, LOW);   // set leg 1 of the H-bridge low
  digitalWrite(Rmotor2Pin, HIGH);  // set leg 2 of the H-bridge high
}
else if (position < 500)
{
  digitalWrite (Rmotor1Pin,HIGH);
  digitalWrite (Rmotor2Pin,LOW);
}
else
{
  digitalWrite (Rmotor1Pin,LOW);
  digitalWrite (Rmotor2Pin,LOW);
}




because this covers every case completely, which is better imo, but it doesn't make any difference on the normal functioning

I tested your code too and it works. To me, it doesn't matter which code I use so long as it works. I'm actually really glad you posted an alternative. The reason being is that I need to drive a second motor and having two scripts to work with can help me figure out which way would be easiest for this all to work. I also need to add the 'x' value on the joystick to switch between the left and right motor.

I was thinking of just having a bunch of 'if' - or in your case, 'else if' statements that cover every scenario. I'm counting 6 right now.

Both forward/back
Left forward/back
Right forward/back

Anyway, I think I'm rambling at this point. Nonetheless thanks for helping me with my code! This whole thing is coming together nice and smoothly!

I was thinking of just having a bunch of 'if' - or in your case, 'else if' statements that cover every scenario. I'm counting 6 right now.

Both forward/back
Left forward/back
Right forward/back

The joystick only has two degrees of freedom. I think you need to recount/reconsider. You have X left/right and Y forward/back.

PaulS:

I was thinking of just having a bunch of 'if' - or in your case, 'else if' statements that cover every scenario. I'm counting 6 right now.

Both forward/back
Left forward/back
Right forward/back

The joystick only has two degrees of freedom. I think you need to recount/reconsider. You have X left/right and Y forward/back.

Yes, but there is also a point where the x value is stationary and y value is changing. In this case, I want both motors to work simultaneously.

Would you suggest I approach this differently?

Yes, but there is also a point where the x value is stationary and y value is changing. In this case, I want both motors to work simultaneously.

Would you suggest I approach this differently?

Not without knowing what the motors are doing. I hear two motors and a joystick and I think of two possibilities. One involves each motor controlling an axis independently, like an XY plotter. The other is operating something like a tank, where each motor controls one tread.

How to use the X, Y values depends on which situation applies to your case.

PaulS:

Yes, but there is also a point where the x value is stationary and y value is changing. In this case, I want both motors to work simultaneously.

Would you suggest I approach this differently?

Not without knowing what the motors are doing. I hear two motors and a joystick and I think of two possibilities. One involves each motor controlling an axis independently, like an XY plotter. The other is operating something like a tank, where each motor controls one tread.

How to use the X, Y values depends on which situation applies to your case.

The 'tank' approach is more or less what I was going for.

The 'tank' approach is more or less what I was going for.

So, you have 8 limit conditions to handle - (Xmax, Ymin), (XMax, 0), (Xmax, YMax), (0, Ymax), (XMin, YMax), (XMin, 0), (XMin, YMin), and (0, YMin).

Once you know what each of the limit conditions should do, anywhere between the limits should be easy to deal with.

PaulS:

The 'tank' approach is more or less what I was going for.

So, you have 8 limit conditions to handle - (Xmax, Ymin), (XMax, 0), (Xmax, YMax), (0, Ymax), (XMin, YMax), (XMin, 0), (XMin, YMin), and (0, YMin).

Once you know what each of the limit conditions should do, anywhere between the limits should be easy to deal with.

This is exactly what I was getting at in last few posts. The only difference is "(Xmax,0)" and "(Xmin,0)" don't really serve any purpose to me. The motors should only spin when the Y value is a 'max' or a 'min'.

Steen:

Hani:

FullyJosh:
I know that when the Joystick is stationary, the value is 510.

what is this value??! and how did u know it ..... ??
sorry for that stupid question

that is just the analogRead value, i think that's clear...

ok so what he did first is connecting the joystick to the arduino pins and the he read the value comming from the joystick in stationary using the analogRead??

The motors should only spin when the Y value is a 'max' or a 'min'.

Ah. An unusual requirement, to say the least. So, the Y axis controls direction only, and the X axis controls speed? Do both motors turn the same speed, or does the position of the joystick in X affect each motor differently?

PaulS:

The motors should only spin when the Y value is a 'max' or a 'min'.

Ah. An unusual requirement, to say the least. So, the Y axis controls direction only, and the X axis controls speed? Do both motors turn the same speed, or does the position of the joystick in X affect each motor differently?

Y controls the direction the motors spin and X controls which motor spins.

If X is in the middle and Y is a max, then both motors should spin forward.
If X is a max and Y is a max , then the left motor should spin forward.
If x is a min and Y is a max, then the right motor should spin forward.

Same would go is Y was a min except it would be backwards

If Y is anywhere in the middle, then it should remain stationary and neither motor should spin.

Theoretically, they should both spin at the same speed when X is in the middle and Y is a max or a min.

Let's deal just with the YMax case. The YMin is the same, except that the direction is the other way.

X can vary from XMin to XMax.

If X is a max and Y is a max , then the left motor should spin forward.
If x is a min and Y is a max, then the right motor should spin forward.

What about in between? Or are you only wanting the motors to move when the joystick is in the corners? Or, rather, are you only wanting them to move when the joystick is in (near) one of 6 positions?

PaulS:
Let's deal just with the YMax case. The YMin is the same, except that the direction is the other way.

X can vary from XMin to XMax.

If X is a max and Y is a max , then the left motor should spin forward.
If x is a min and Y is a max, then the right motor should spin forward.

What about in between? Or are you only wanting the motors to move when the joystick is in the corners?

If x is stationary (obviously I would give it a range of what I feel is stationary) and Y is a max, then both motors should spin.

The x value also has a stationary position in which case I need both motors to operate.

I guess for sake of argument you could think of like a compass

N- Both motors are moving Forward
NE - The right motor is moving Forward
NW- The left motor is moving Forward

There are 3 different behaviours in the forward direction.

The only purpose x serves is to isolate the two motors.

So, you have a nested if structure, like so:
if(y ~= YMax)
{
if(x ~= XMax)
spin one motor forward
else if(x ~= 0)
spin both motors forward
else if(x ~= XMin)
spin other motor forward
}
else if (y ~= YMin)
{
if(x ~= XMax)
spin one motor backward
else if(x ~= 0)
spin both motors backward
else if(x ~= XMin)
spin other motor backward
}

PaulS:
So, you have a nested if structure, like so:
if(y ~= YMax)
{
if(x ~= XMax)
spin one motor forward
else if(x ~= 0)
spin both motors forward
else if(x ~= XMin)
spin other motor forward
}
else if (y ~= YMin)
{
if(x ~= XMax)
spin one motor backward
else if(x ~= 0)
spin both motors backward
else if(x ~= XMin)
spin other motor backward
}

B-E AUTIFUL ! I can't wait to get home tonight and test this out! Thanks a ton for this, really really appreciate it!