*Solved* Serial Read Issues

Hello,
I have be working on a program for controlling two motors from a single joystick with differential output. The serial portion is just for checking functionality of my program before I work on the circuit boards. I cannot for the life of me understand why the program will not send the serial data to the serial monitor while it is in an if statement. I would appreciate someone grabbing my head and shoving my nose into the problem. I believe it has to do with my Serial.print being in a nested if statement, but I am not too sure on how to move forward.

So far I have tried:

-change while statements to if statements,
-change the time delay on if statements,
-placing Serial.print directly after the initial if statement,
-placing a Serial.print before the if statement-- (this works for printing information, but does not show me that the if statements are working),
-searching the internet

Here is the code:

int pinMotorA1 = 8; //motor pins
int pinMotorA2 = 11;
int MotorAspeed = 9;
int pinMotorB1 = 12;
int pinMotorB2 = 13;
int MotorBspeed = 10;

int x = 0; //variables
int y = 0;
int i = 0;
int g = 0;

int IUP = A0; //joystick input
int ILR = A1;

int fastA = 0; //speed for pwm
int fastB = 0;

void setup() 
{
  pinMode(pinMotorA1, OUTPUT); // Motor driver output stuff
  pinMode(pinMotorA2, OUTPUT);
  pinMode(MotorAspeed, OUTPUT);
  pinMode(pinMotorB1, OUTPUT);
  pinMode(pinMotorB2, OUTPUT);
  pinMode(MotorBspeed, OUTPUT);

  i = analogRead(ILR); // Setup for joystick
  g = analogRead(IUP);
  
  x = map(i, 0, 1023, -254, 254); //map joystick input to pwm values
  y = map(g, 0, 1023, -254, 254);
  
  Serial.begin(9600);
}

void forward()
{
     analogWrite(MotorAspeed,fastA);
     analogWrite(MotorBspeed,fastB);
     digitalWrite(pinMotorA1,HIGH);//Motor A clockwise
     digitalWrite(pinMotorA2,LOW);
     digitalWrite(pinMotorB1,LOW);//Motor B counter-clockwise
     digitalWrite(pinMotorB2,HIGH);
}  

void reverse()
{
     analogWrite(MotorAspeed, fastA);
     analogWrite(MotorBspeed, fastB);
     digitalWrite(pinMotorA1, LOW);//Motor A counter-clockwise
     digitalWrite(pinMotorA2, HIGH);
     digitalWrite(pinMotorB1, HIGH);//Motor B clockwise
     digitalWrite(pinMotorB2, LOW);
}  

void right()
{
     analogWrite(MotorAspeed, fastA);
     analogWrite(MotorBspeed, fastB);
     digitalWrite(pinMotorA1, LOW);//Motor A counter-clockwise
     digitalWrite(pinMotorA2, HIGH);
     digitalWrite(pinMotorB1, LOW);//Motor B counter-clockwise
     digitalWrite(pinMotorB2, HIGH);
}  

void left()
{
     analogWrite(MotorAspeed, fastA);
     analogWrite(MotorBspeed, fastB);
     digitalWrite(pinMotorA1, HIGH);//Motor A clockwise
     digitalWrite(pinMotorA2, LOW);
     digitalWrite(pinMotorB1, HIGH);//Motor B clockwise
     digitalWrite(pinMotorB2, LOW);
}

void stop()
{
     digitalWrite(MotorAspeed,LOW); // Unenble the pin, to stop the motor. this should be done to avid damaging the motor. 
     digitalWrite(MotorBspeed,LOW);
     delay(1000);
}     

void loop()
{
  i = analogRead(ILR); // Setup for joystick
  g = analogRead(IUP);
  
  x = map(i, 0, 1023, -254, 254); //map joystick input to pwm values
  y = map(g, 0, 1023, -254, 254);
  
  //Serial.print(x);
  //Serial.print(",");
  //Serial.print(y);
  //Serial.println("");
  
  //center up direction  
  if ((x = 0) && (y > 0)){ 
    fastA = y;
    fastB = y;
    forward();
    delay(500);
    stop();
    Serial.print("Straight Forward");
    Serial.println("");
  }
  
  no direction
  while ((x = 0) && (y = 0))
  { 
    stop();
    delay(100);
    Serial.print("Stop");
    Serial.println("");
  }

  // up right
  if ((x > 0) && (y > 0)){
    if (y > x){
      fastA = y - x; 
      fastB = y;
      forward();
      delay(500);
      stop();
      Serial.print("Forward Right");
      Serial.println("");
    }
    // above right
    else{
      fastA = x - y;
      fastB = x;
      right();
      delay(500);
      stop();
      Serial.print("Hard Right Forward");
      Serial.println(""); 
    }
  }
  //down
  if ((x = 0) && (y < 0)){ 
    fastA = y;
    fastB = y;
    forward();
    delay(500);
    stop();
    Serial.print("Straight Reverse");
    Serial.println("");
  } 
  //down left
  if ((x < 0) && (y < 0)){
    if (x < y){
      fastA = abs(y) + x;
      fastB = abs(y);
      reverse();
      delay(500);
      stop();
      Serial.print("Reverse Left");
      Serial.println("");
    }
    //below left
    else{
      fastA = abs(x) + y;
      fastB = abs(x); 
      right();
      delay(500);
      stop();
      Serial.print("Hard Left Reverse");
      Serial.println("");
    }
  }
  //down right
  if ((x > 0) && (y < 0)){
    if (abs(y) > x){
      fastA = abs(y) - x;
      fastB = abs(y); 
      reverse();
      delay(500);
      stop(); 
      Serial.print("Reverse Right");
      Serial.println("");
    }
    //below right
    else{
      fastA = x + y;
      fastB = x;
      left();
      delay(500);
      stop();
      Serial.print("Hard Right Reverse");
      Serial.println("");
    }
    
  }
  if ((y > 0) && (x < 0)){
    //up left
    if (y > abs(x)){
      fastA = y + x;
      fastB = y;
      forward();
      delay(500);
      stop();
      Serial.print("Forward Left");
      Serial.println("");
    }
    //above left
    else{
      fastA = abs(x) - y;
      fastB = abs(x);
      left();
      delay(500);
      stop();
      Serial.print("Hard Left Forward");
      Serial.println("");
    }

  }

}

Thanks in advance.

Called blocking code A if statement blocks things from happening if it’s false. So as long as it’s false nothing happens.

If (x=0)

x will never be 0 and Y less then 0 not with a joystick
You’d have better luck with

if ((x >= 0) && (y > 0)

This mean < less than can’t be less then 0 adc is 0 to 1024 for 10 bit ADC.
And if you read your code I don’t think any of the test you have you’d want them to be < less then.
And x== 0 is ok if you no it’s going to be 0 but a joystick reads haft way when your not touching it so
x would be some thing like 512 and y would be the same.
so your test for say lower to move and turn one way and higher and low to move and turn the other way but you’ll have to test that.
Like the joystick I have left would be lower right would be higher then 512.
And forward would be higher and backward would be lower then 512.

  if ((x = 0) && (y > 0)){

I think you might have meant to write

  if ((x == 0) && (y > 0)){

Problem with that is a joystick never hardly ever reads 0 so a test for.
x==0 not going to happen much.

But I set the map to make it go from -255 to 255. So center should be 0, right? And straight forward is just one of the if statements, I would imagine some other statements should work......

Adding the "==" makes it work!

Thanks so much for your help guys!

I would think something like this would be more in line with what you’d test for.

if ((x != 512))&&(y!=512)){  // we have joystick movement.
     Serial.write(x);             // you can use map to get a range for the joystick 
     Serial.write(y);           // I just use Serial.write(adcval0) and adcval1 to see what the joystick looks 
}                                 // like at rest. Then I know.

But I set the map to make it go from -255 to 255. So center should be 0, right? And straight forward is just one of the if statements, I would imagine some other statements should work......

I'm sorry but yes X== is right and the x=0 was the show stopper.

I didn't look at your map part of the code till just now you said why's the if then not sending serial.
Your good to go if you change X==0.

:slight_smile: Thanks!