Issue with code, don't know why my code activates motors but not simultaneously

This code is suppoued to receive two values and activate de motors with them when I send the values one activates but not the other, for example: I travel through the x axis of my joystinck in an app and it activates motor x and does the same with motor y when I do it on y axis but when walk through anything in the middle of both jus one turns on.

Could you check if anything is bad in my code while I check my app?

const int pin_x =10;
  const int pin_y = 11;
  int in[2]{0};
  int index;
  int speedx=0; int speedy=0;
  const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1;
    
class Car{
  private:
 int m_speedx = 0;
 int m_speedy = 0;
 public:
void getintoSpeed(int pinx ,int piny ,int x,int y){m_speedx =x; m_speedy =y;turnOnMotors(pinx,piny);}
void turnOnMotors(int pin_x, int pin_y){analogWrite(pin_x,m_speedx);analogWrite(pin_y,m_speedy);}
};
Car car;
void setup() {
  pinMode(pin_x, OUTPUT);
  pinMode(pin_y,OUTPUT);
  Serial.begin (38400); //for SC with bluetooth
 
}

void turn(int &speedOne, int &speedTwo){
    speedOne =255;
    speedTwo = 0;
  }

void loop() {
  // put your main code here, to run repeatedly:
  //speedx = ((analogRead(X_pin)-507)/507)*255;
  //speedy = ((analogRead(Y_pin)-518)/-518)*255;
  if(speedx < 0){speedx*=(-1);}


    while(Serial.available()>2){
      speedx=Serial.read();
      delay(10);
    speedy= Serial.read();
    }
    delay(10);
    
    
    
    //automaticPilot
/*    if(Automatic()== 0){ // Turn Right
      turn(speedx,speedy)
      }
     else{turn(speedy,speedx)}// Turn left*/
    car.getintoSpeed(pin_x, pin_y,speedx,speedy); //this potentially returnable
}

Don't write code like this - it is almost impossible to read

void getintoSpeed(int pinx ,int piny ,int x,int y){m_speedx =x; m_speedy =y;turnOnMotors(pinx,piny);}

Put each part on its own line like this

void getintoSpeed(int pinx ,int piny ,int x,int y){
    m_speedx =x; 
    m_speedy =y;
    turnOnMotors(pinx, piny);
}

I don't see the point of the two lines

    m_speedx =x; 
    m_speedy =y;

they don't seem to do anything.

...R

Robin2:
I don't see the point of the two lines

    m_speedx =x; 

m_speedy =y;



they don't seem to do anything.

...R

They set the private class variables associated with 'car'

Admittedly, that class is horrible.

I'm guessing the read problem is reading the x and y speeds from Serial as Serail.read() only returns the char read, not some sort of integer typed into the Serial monitor.

I was hoping your Reply #2 would have included a new version of your program with the code laid out so it is easy to read.

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Hi,
What model Arduino are you using?
How are you powering the motors?
How are you driving the motors?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

are you sure your your motors are connected and controllable by pins 10 & 11?