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
}