“Joystick控制直流电机”的版本间的差异
(→设备) |
(→调试) |
||
第45行: | 第45行: | ||
*打开Arduino IDE,把原来IDE里的代码都删除,将下列代码复制到IDE中。 | *打开Arduino IDE,把原来IDE里的代码都删除,将下列代码复制到IDE中。 | ||
<source lang="cpp"> | <source lang="cpp"> | ||
+ | #define Pin_X A1 | ||
+ | #define Pin_Y A0 | ||
+ | //D6,D8控制1A,1B的电机 | ||
+ | #define OUT1A 6 | ||
+ | #define OUT1B 8 | ||
+ | //D5,D7控制2A,2B的电机 | ||
+ | #define OUT2A 5 | ||
+ | #define OUT2B 7 | ||
+ | |||
+ | int value, data; | ||
+ | |||
+ | void setup() { | ||
+ | // put your setup code here, to run once: | ||
+ | Serial.begin(9600); //串口初始化 | ||
+ | pinMode(Pin_X, INPUT); | ||
+ | pinMode(Pin_Y, INPUT); | ||
+ | pinMode(OUT1A, OUTPUT); | ||
+ | pinMode(OUT1B, OUTPUT); | ||
+ | pinMode(OUT2A, OUTPUT); | ||
+ | pinMode(OUT2B, OUTPUT); | ||
+ | } | ||
+ | |||
+ | int uiStep() //切歌 | ||
+ | { | ||
+ | if (analogRead(Pin_Y) < 100) //Y-up | ||
+ | { | ||
+ | delay(50); | ||
+ | if (analogRead(Pin_Y) < 100) | ||
+ | return 1; | ||
+ | } | ||
+ | if (analogRead(Pin_Y) > 900) //Y-down | ||
+ | { | ||
+ | delay(50); | ||
+ | if (analogRead(Pin_Y) > 900) | ||
+ | return 2; | ||
+ | } | ||
+ | if (analogRead(Pin_X) < 100) | ||
+ | { | ||
+ | delay(50); | ||
+ | if (analogRead(Pin_X) < 100) //X-Right | ||
+ | return 3; | ||
+ | } | ||
+ | if (analogRead(Pin_X) > 900) //X-Left | ||
+ | { | ||
+ | delay(50); | ||
+ | if (analogRead(Pin_X) > 900) | ||
+ | return 4; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if (uiStep() == 1) | ||
+ | { | ||
+ | value += 5; | ||
+ | if (value > 255) | ||
+ | value = 255; | ||
+ | } | ||
+ | else if (uiStep() == 2) | ||
+ | { | ||
+ | value -= 5; | ||
+ | if (value < 0) | ||
+ | value = 0; | ||
+ | } | ||
+ | else if (uiStep() == 3) | ||
+ | data = 0; | ||
+ | else if (uiStep() == 4) | ||
+ | data = 1; | ||
+ | motor_sta(data, value); | ||
+ | |||
+ | Serial.print(data); | ||
+ | Serial.print(" "); | ||
+ | Serial.println(value); | ||
+ | } | ||
+ | void motor_sta(int num, int fadeValue) | ||
+ | { | ||
+ | if (num == 0) | ||
+ | { | ||
+ | analogWrite(OUT1A, fadeValue); | ||
+ | digitalWrite(OUT1B, LOW); | ||
+ | analogWrite(OUT2A, fadeValue); | ||
+ | digitalWrite(OUT2B, LOW); | ||
+ | } | ||
+ | else if (num == 1) | ||
+ | { | ||
+ | digitalWrite(OUT1A, LOW); | ||
+ | analogWrite(OUT1B, fadeValue); | ||
+ | digitalWrite(OUT2A, LOW); | ||
+ | analogWrite(OUT2B, fadeValue); | ||
+ | } | ||
+ | } | ||
</source> | </source> | ||
*选择正确的板卡和COM端口 | *选择正确的板卡和COM端口 |
2015年11月24日 (二) 05:36的版本
Language | English |
---|
目的通过Joystick摇杆来控制直流电机的转动。 原理通过检测Joystick摇杆在水平和垂直两个方向上的模拟量来做出控制。在水平方向上控制电机的速度,用PWM来调节速度。在垂直方向上控制电机的方向,通过控制高低电平信号。 设备
准备
调试
#define Pin_X A1
#define Pin_Y A0
//D6,D8控制1A,1B的电机
#define OUT1A 6
#define OUT1B 8
//D5,D7控制2A,2B的电机
#define OUT2A 5
#define OUT2B 7
int value, data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //串口初始化
pinMode(Pin_X, INPUT);
pinMode(Pin_Y, INPUT);
pinMode(OUT1A, OUTPUT);
pinMode(OUT1B, OUTPUT);
pinMode(OUT2A, OUTPUT);
pinMode(OUT2B, OUTPUT);
}
int uiStep() //切歌
{
if (analogRead(Pin_Y) < 100) //Y-up
{
delay(50);
if (analogRead(Pin_Y) < 100)
return 1;
}
if (analogRead(Pin_Y) > 900) //Y-down
{
delay(50);
if (analogRead(Pin_Y) > 900)
return 2;
}
if (analogRead(Pin_X) < 100)
{
delay(50);
if (analogRead(Pin_X) < 100) //X-Right
return 3;
}
if (analogRead(Pin_X) > 900) //X-Left
{
delay(50);
if (analogRead(Pin_X) > 900)
return 4;
}
return 0;
}
void loop() {
if (uiStep() == 1)
{
value += 5;
if (value > 255)
value = 255;
}
else if (uiStep() == 2)
{
value -= 5;
if (value < 0)
value = 0;
}
else if (uiStep() == 3)
data = 0;
else if (uiStep() == 4)
data = 1;
motor_sta(data, value);
Serial.print(data);
Serial.print(" ");
Serial.println(value);
}
void motor_sta(int num, int fadeValue)
{
if (num == 0)
{
analogWrite(OUT1A, fadeValue);
digitalWrite(OUT1B, LOW);
analogWrite(OUT2A, fadeValue);
digitalWrite(OUT2B, LOW);
}
else if (num == 1)
{
digitalWrite(OUT1A, LOW);
analogWrite(OUT1B, fadeValue);
digitalWrite(OUT2A, LOW);
analogWrite(OUT2B, fadeValue);
}
}
软件调试
#define PI 3.1415926
#define JoystickX_PIN A1 //Joystick的X轴输出
#define JoystickY_PIN A0 //Joystick的Y轴输出
#define PIN_MOTORA 5 //电机输出引脚A
#define PIN_MOTORB 6 //电机输出引脚B
valueX = (float)(analogRead(JoystickX_PIN)-512);
valueY = (float)(analogRead(JoystickY_PIN)-512);
valueL = sqrt(sq(valueX)+sq(valueY));
angle = asin(valueX/valueL)*180/PI; //通过反三角函数计算摇杆的角度
motorSpeed = (int)(angle*255/180); //根据角度计算电机的转速
if(motorSpeed > 0) //角度为正,电机正转
{
digitalWrite(PIN_MOTORA, LOW);
analogWrite(PIN_MOTORB, motorSpeed);
}
else if(motorSpeed < 0) //角度为负,电机反转
{
digitalWrite(PIN_MOTORB, LOW);
analogWrite(PIN_MOTORA, abs(motorSpeed));
}
else //角度为零,电机停止
{
digitalWrite(PIN_MOTORA, LOW);
digitalWrite(PIN_MOTORB, LOW);
}
delay(500);
结果将Joystick的摇杆推到不同方向,可以看到电机的转速及方向也会跟着变化。 视频 |