Joystick Controls DC Motor

来自Microduino Wikipedia
1304410487@qq.com讨论 | 贡献2015年9月2日 (三) 02:59的版本 (Created page with "{{Language| Joystick Controls DC Motor}} {| style="width: 800px;" |- | ==Objective== To control rotation of DC motor by Joystick. ==Principle== By detecting Joystick analog v...")
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索
Language English

Objective

To control rotation of DC motor by Joystick.

Principle

By detecting Joystick analog values in X- and Y-axes, you can control the rotation and direction of the DC motor.

Equipment

Module Number Function
Microduino-CoreUSB 1 Core board
Microduino-Sensorhub 1 Sensor pinboard
Microduino-Joystick 1 Joystick sensor
Microduino-Robot 1 Robot control board
Microduino-Motor 1 DC motor drive board
Motor 1

Hardware Buildup

  • Setup 1:Stack CoreUSB, Robot, Motor and Sensorhub.
  • Setup 2:Connect Joystick to Sensorhub to A0-A1 pin of Sensorhub and motor to OUTA-OUT1B pin of Stepper.

Software Debugging

  • Define pin of Joystick and motor control.
#define PI 3.1415926
#define JoystickX_PIN A1   //Joystick X-axis output 
#define JoystickY_PIN A0   //Joystick Y-axis output 
#define PIN_MOTORA 5    //Motor output
#define PIN_MOTORB 6    // Motor output pin
  • Calculate the angle where Joystick lies, according to which you can control rotation speed and decide rotation direction based on positive or negative value of the angle.
    valueX = (float)(analogRead(JoystickX_PIN)-512);    
    valueY = (float)(analogRead(JoystickY_PIN)-512);
    valueL = sqrt(sq(valueX)+sq(valueY));
    angle = asin(valueX/valueL)*180/PI;    // Calculate the angle of Joystick by inverse trigonometric function 
    motorSpeed = (int)(angle*255/180);    // Calculate rotation speed according to the angle. 
    if(motorSpeed > 0)                    // If the angle is positive, then the motor rotates clockwise. 
    {
      digitalWrite(PIN_MOTORA, LOW);
      analogWrite(PIN_MOTORB, motorSpeed);
    }
    else if(motorSpeed < 0)               //If the angle is negative, then the motor rotates anti-clockwise. 
    {
      digitalWrite(PIN_MOTORB, LOW);
      analogWrite(PIN_MOTORA, abs(motorSpeed));
    }
    else                                  //If the angle is zero, then the motor stops rotation. 
    {
      digitalWrite(PIN_MOTORA, LOW);
      digitalWrite(PIN_MOTORB, LOW);
    }
    delay(500);

Program

[MicroduinoJoystickMotor]

Result

Push Joystick to different angle, you can see the rotation speed and direction will change accordingly.

Video