Go Down

Topic: [Help] 2 DC motors cannot read 2 analog input (Read 662 times) previous topic - next topic

KassC

sorry for disturbing u guys, im new to arduino and im trying to make a robot car by using 2 dc motors and 2 joystick to control each dc motors

my equipments:
1) Arduino Uno
2) Keyestudio L298P Motor Driver Shield (ks0007)
3) 2 DC motors
4) 4 AA battery holder (google tell me its 6volt)

my problem:
so i have 2 joystick x value (JoyStick_X & JoyStick_X2), and the dc motors work fine and can turn both directions when testing using digitalWrite, but when i changed it to 2 analog value for each dc motors, 1 dc motors (Motor_A) cant turn reverse direction and can only go forward with 1 speed, but Motor_B still work fine can turn both direction and have different speed using pwm. But if only use 1 analog value for 2 dc motors, both motors work fine

i searched in google but seem like everyone only use 1 joystick to control 1 or more motors at once rather than using 2 joysticks for each motors

KassC

//Defining pin
//Analog Pin
int JoyStick_X = 0; //x
int JoyStick_Y = 1; //y

int JoyStick_X2 = 4; //x2
int JoyStick_Y2 = 5; //y2

//Digital Pin
int JoyStick_Z = 4; //Button 1
int JoyStick_Z2 = 2; //Button 2
int Button_LED_Z = 8; //Pin  output to Button_LED_Z
int Button_LED_Z2 = 7; //Pin  output to Button_LED_Z2
int Analog_LED_1 = 6; //pwm Pin  output to Analog_LED_1
int Analog_LED_2 = 9; //pwm Pin  output to Analog_LED_2

int Motor_A_Enable = 3; //pmw pin    
int Motor_A_DirA = 5;
int Motor_A_DirB = 10;
int Motor_B_Enable = 11; //pmw pin    
int Motor_B_DirA = 12;
int Motor_B_DirB = 13;


//analog value
int sensorValue_X ; // value read from the Joystick Value
int outputValue_X ; // value output to the PWM
int sensorValue_Y ; // value read from the Joystick Value
int outputValue_Y ; // value output to the PWM
int sensorValue_X2 ; // value read from the Joystick Value
int outputValue_X2 ; // value output to the PWM
int sensorValue_Y2 ; // value read from the Joystick Value
int outputValue_Y2 ; // value output to the PWM

void setup() {
 // put your setup code here, to run once:
 
 //Define Button LED's pin)
 pinMode(JoyStick_Z, INPUT);
 pinMode(JoyStick_Z2, INPUT);
 pinMode(Button_LED_Z, OUTPUT);
 pinMode(Button_LED_Z2, OUTPUT);

 pinMode(Motor_A_Enable, OUTPUT);
 pinMode(Motor_A_DirA, OUTPUT);
 pinMode(Motor_A_DirB, OUTPUT);
 pinMode(Motor_B_Enable, OUTPUT);
 pinMode(Motor_B_DirA, OUTPUT);
 pinMode(Motor_B_DirB, OUTPUT);
 
 Serial.begin(9600); // 9600 bps
}

void loop() {
 // put your main code here, to run repeatedly:
 //Press buttons to lit up different Button_LED
 
 if(digitalRead(JoyStick_Z2) == HIGH)
 {
 digitalWrite(Button_LED_Z2,HIGH);
 }
 else if(digitalRead(JoyStick_Z) == HIGH)
 {
 digitalWrite(Button_LED_Z,HIGH);
}
else if(digitalRead(JoyStick_Z) == HIGH && digitalRead(JoyStick_Z2) == HIGH)
 {
 digitalWrite(Button_LED_Z,HIGH);
 digitalWrite(Button_LED_Z2,HIGH);
}
else {
digitalWrite(Button_LED_Z,LOW);
digitalWrite(Button_LED_Z2,LOW);
}
delay(50);




 // read the analog in value:
 //PROBLEM HERE: here can only read 1 analog value

 sensorValue_X = analogRead(JoyStick_X);
 sensorValue_X >>= 1;
 sensorValue_X2 = analogRead(JoyStick_X2);
 sensorValue_X2 >>= 1;


if(sensorValue_X > 255){
   analogWrite(Motor_A_Enable,(sensorValue_X - 256));
   digitalWrite(Motor_A_DirB, LOW);  
   digitalWrite(Motor_A_DirA, HIGH);
   analogWrite(Analog_LED_1, (sensorValue_X - 256));

 }
 else
   if(sensorValue_X < 255){
     analogWrite(Motor_A_Enable,(255 - sensorValue_X));
     digitalWrite(Motor_A_DirA, LOW);
     digitalWrite(Motor_A_DirB, HIGH);
     analogWrite(Analog_LED_1, (255 - sensorValue_X));

   }
   else{
     digitalWrite(Motor_A_DirA, LOW);
     digitalWrite(Motor_A_DirB, LOW);
     digitalWrite(Analog_LED_1, LOW);
   }

if(sensorValue_X2 > 255){
   analogWrite(Motor_B_Enable,(sensorValue_X2 - 256));
   digitalWrite(Motor_B_DirB, LOW);
   digitalWrite(Motor_B_DirA, HIGH);
   analogWrite(Analog_LED_2, (sensorValue_X2 - 256));

 }
 else
   if(sensorValue_X2 < 255){
     analogWrite(Motor_B_Enable,(255 - sensorValue_X2));
     digitalWrite(Motor_B_DirA, LOW);
     digitalWrite(Motor_B_DirB, HIGH);
     analogWrite(Analog_LED_2, (255 - sensorValue_X2));

   }
   else{
     digitalWrite(Motor_B_DirA, LOW);
     digitalWrite(Motor_B_DirB, LOW);
     digitalWrite(Analog_LED_2, LOW);
   }



 
 delay(50);
}

KassC

Thankyou very much!

(sorry im not really know how to use forums so didnt insert the code at first)

justone

Code: [Select]
int JoyStick_X = 0; //x
int JoyStick_Y = 1; //y


You are using pins 0 and 1 which the serial uses. Stay away from them.

AWOL

Code: [Select]
int JoyStick_X = 0; //x
int JoyStick_Y = 1; //y


You are using pins 0 and 1 which the serial uses. Stay away from them.
But analogue pin zero is not the same as.digital pin zero.

KassC

i done the chassis of it so i think why not test it, my guess is the car will only move forward since Motor_A only rotate 1 direction. However surprisingly although Motor_A still cant rotate backward, but if i hold down 2 joystick in opposite direction the car goes backward, idk why, just think its some sort of miracle XD

PaulS

Code: [Select]
int JoyStick_X = 0; //x
int JoyStick_Y = 1; //y

int JoyStick_X2 = 4; //x2
int JoyStick_Y2 = 5; //y2

When you start adding suffixes to similar names, add suffixes to ALL such names. If it far too easy to type JoyStick_X where you meant to use JoyStick_X2, especially when cutting and pasting.

You have WAY too much code for someone that is not certain that the hardware is working.

Write some code to test ONE of the motors. Make the motor run at 0 speed for 1 second, then at half speed for one second, then at full speed for one second, then at half speed for one second, then at 0 speed for one second, then at half speed the other direction, then at full speed in that direction, then at half speed.

ONLY after that works, change the code to operate the other motor instead.

ONLY after that works, change the code to operate both motors.

ONLY after that works, change the code to use the joysticks.
The art of getting good answers lies in asking good questions.

KassC

i tried the testing code like u said before combining all these codes, this is my workflow:

1) test both joysticks with led & serial monitor, all ok
2) test 2 motors in digitalWrite, all ok and can rotate both directions (using keyestudio sample code)
3) connect Joystick_X to 1 motor, work fine
4) connect Joystick_X2 to 2nd motor, problem occurred, only 1 motor is able to detect Joystick_X's signal, another cannot detect Joystick_X2 signal and can only rotate in single direction

i also checked the code just in case i wrote something wrong, but the code seems no typing error, and im certain the hardware is working fine, it just i dont know why my code didn't work as expected

PaulS

Code: [Select]
//PROBLEM HERE: here can only read 1 analog value

 sensorValue_X = analogRead(JoyStick_X);
 sensorValue_X >>= 1;
 sensorValue_X2 = analogRead(JoyStick_X2);
 sensorValue_X2 >>= 1;

That is NONSENSE. You CAN read multiple values, although sometimes you want to discard the first reading when changing channels.

How do you KNOW what was read from each channel? USEFUL Serial.print() statements are missing from your code, and serial output is missing from your post.

I suspect that your crappy placement of { and your poor indenting are the cause of your problems.

EVERY { goes on a line BY ITSELF.

Use Tools + Auto Format to fix the piss-poor indenting.
The art of getting good answers lies in asking good questions.

Go Up