Go Down

Topic: Control DC Motor with Joystick (Read 105 times) previous topic - next topic

swadejaxsun

Hi,

I'm trying to control the 3-6V DC motor with the joystick.

The sample code for the joystick works alone. The DC motor (fan) example works on its own. So it's all wired correctly.

I literally copy/pasted the code from these two examples to build my code (with some mods). I've been over the code several times now, and can't figure out why the motor won't respond to the joystick input.

I'm getting readings from the joystick that I can see in the serial. If I wipe my code clean of all of the joystick information, and just have the motor analogWrite for the motor control, no response.

Here is my code, I'd appreciate any help.

Code: [Select]
// Arduino pin numbers
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; // analog pin connected to Y output
#define ENABLE 5
#define DIRA 3
#define DIRB 4

void setup() {
  //joystick
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);

   //---set pin direction for fan
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);
}

void loop() {
  if((analogRead(Y_pin) > 250) && (analogRead(Y_pin) < 515)){
    analogWrite(ENABLE,128);
    Serial.println("low");
  }
  else if(analogRead(Y_pin) < 250){
    analogWrite(ENABLE,255);
    Serial.println("high");
  }
  else if((analogRead(Y_pin) > 530) && (analogRead(Y_pin) < 750)){
    analogWrite(ENABLE,128);
  }
  else if(analogRead(Y_pin) > 750){
    analogWrite(ENABLE,255);
  }
 
  Serial.println(analogRead(Y_pin));

}

pert

If you run it with Serial Monitor open, are you seeing the expected output?

I would recommend adding some additional Serial.println() calls to make sure you can see everything that is happening.

JCA34F


Go Up