I am using a joystick to wirelessly control two motors; when I move the joystick in the forward direction or any other direction then it runs with a full speed. I want to do an increment in my motor speed so when I move the joystick then it should run and increases the speed step by step with respect to the joystick movement. Please tell me how to do it ...
Here is the code that I m using
int VRx=A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy=A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw=2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;
void setup() {
Serial.begin(4800);
pinMode(VRx,INPUT);
pinMode(VRy,INPUT);
pinMode(Sw,INPUT);
}
void loop() {
unsigned long tempA=0; // MOTOR A motor speed value start from zero
unsigned long tempA1=0; //FORMULA
unsigned long tempA2=0; //FORMULA
Aval=analogRead(VRx);
tempA = Aval;
Serial.println (tempA);
Serial.println(" JOYSTICK NEUTRAL POSITION");
Serial.print("VRX: ");
Serial.print(analogRead(VRx));
Serial.print(" | ");
Serial.print("VRY: ");
Serial.print(analogRead(VRy));
Serial.println(" | ");
unsigned long tempB=0; // MOTOR B motor speed value start from zero
unsigned long tempB1=0; // Formula
unsigned long tempB2=0; // Formula
Bval=analogRead(VRy);
tempB = Bval;
Serial.println (tempB);
//FORWARD DONE SUCCESSFULLY
//===========================================================================//
if ((tempB>550) && (tempA>450 && tempA<550)){
tempB1 = (((tempB-MID)*100)/MID);
char st[20]; // Send command to the buffer Vx as a statement for motor A
char st1[20]; // Send command Vy as a statement for motor B
sprintf(st,"<A+%03lu>",tempB1);
sprintf(st1,"<B+%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
}
//BACKWARD DONE SUCCESSFULLY
//===========================================================================//
if ((tempB<450) && (tempA>450 && tempA<550)){
tempB1= (((MID - tempB)*100)/MID);
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempB1);
sprintf(st1,"<B-%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
}
//NEUTRAL DONE SUCCESSFULLY
//===========================================================================//
if ((tempB>450 && tempB<550) && (tempA>450 && tempA<550)){
char st[20];
Serial.println ("<A+000>");
Serial.println ("<B+000>");
}
//PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
//===========================================================================//
if ((tempA<450) && (tempB>450 && tempB<550)){
tempA1=(((MID-tempA)*100)/MID);
char st[20];
char st1[20];
sprintf(st1,"<B+%03lu>",tempA1);
Serial.println (st1);
}
//PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
//===========================================================================//
if ((tempA>550) && (tempB>450 && tempB<550))
{
tempA1 = (((tempA-MID)*100)/MID);
char st[20];
char st1[20];
sprintf(st,"<A+%03lu>",tempA1);
Serial.println (st);
}
// FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
//=============================================================================================================//
if ((tempA>550) && tempB>550)
{
tempA1 = ((tempB * 100)/MAX);
tempB1 = (((tempA - MID)*100)/(MAX - MID));
tempB2 = (tempA1 - ((tempB1 * tempA1)/100));
char st[20];
char st1[20];
sprintf(st,"<A+%03lu>",tempA1);
sprintf(st1,"<B+%03lu>",tempB2);
Serial.println (st);
Serial.println (st1);
}
// FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
//==========================================================================================================//
if ((tempA<450) && (tempB>550))
{
tempB1=((tempB * 100)/ MAX );
tempA1 = (100 - ((tempA * 100)/(MID)));
tempA2 = (tempB1 - ((tempA1 * tempB1)/100));
char st[20];
char st1[20];
sprintf(st,"<A+%03lu>",tempA2);
sprintf(st1,"<B+%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
}
// REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
//====================================================================================================================================//
if (tempA>550 && tempB<450)
{
tempA1 = (((MID - tempB) * 100) / (MID) );
tempB1 = (((tempA - MID)*100)/(MAX - MID));
tempB2 = (tempA1 - ((tempB1 * tempA1)/100 ));
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempA1);
sprintf(st1,"<B-%03lu>",tempB2);
Serial.println (st);
Serial.println (st1);
}
// REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
// ===================================================================================================================================//
if (tempA<450 && tempB<450)
{
tempB1= (((MID - tempB) * 100)/(MID));
tempA1= (100 - ((tempA * tempB1)/(MID)));
tempA2= (tempB1 - ((tempA1 * tempB1)/100));
char st[20];
char st1[20];
sprintf(st,"<A-%03lu>",tempA2);
sprintf(st1,"<B-%03lu>",tempB1);
Serial.println (st);
Serial.println (st1);
}
}
It's not clear what you want help with. Your code does not control motors, only prints some values to serial monitor. I think these numbers represent the desired motor speeds?
tempB1 = (((tempB-MID)*100)/MID);
Do code lines, like the one above, fail to achieve the speed increment you need? If not, please describe in more detail what you see on serial monitor and what you expected to see.
When showing output from Serial monitor, please copy the text and paste it into your post between code tags.
yes it shows the values on the percentage on the serial monitor with respect to the values changes with the motor speed according to the joystick movement.