基于PS2手柄的Arduino遥控小车

基于PS2手柄的Arduino遥控小车前言本文利用PS2手柄和Arduino开发板制作了一个简易的遥控小车,利用蓝牙进行通信,可以实现前后左右的移动。(原理掌握之后可以自己拓展相关功能)一、零件1.ArduinoUNO开发板:ArduinoUNO是ArduinoUSB接口系列的最新版本,作为Arduino平台的参考标准模板。UNO的处理器核心是ATmega328,同时具有14路数字输入/输出口(其中6路可作为PWM输出),6路模拟输入,一个16MHz晶体振荡器,一个USB口,一个电源插座,一个ICSPheader和一个复位按钮。2

大家好,又见面了,我是你们的朋友全栈君。

前言

本文利用PS2手柄和Arduino开发板制作了一个简易的遥控小车,利用蓝牙进行通信,可以实现前后左右的移动。(原理掌握之后可以自己拓展相关功能)

一、零件

1.Arduino UNO开发板:

ArduinoUNO是ArduinoUSB接口系列的最新版本,作为Arduino平台的参考标准模板。UNO的处理器核心是ATmega328,同时具有14路数字输入/输出口(其中6路可作为PWM输出),6路模拟输入,一个16MHz晶体振荡器,一个USB口,一个电源插座,一个ICSPheader和一个复位按钮。
在这里插入图片描述

2.PS2手柄与接收器:

PS2 手柄由手柄与接收器两部分组成,手柄主要负责发送按键信息;接收器与单片机相连,用于接收手柄发来的信息,并传递给单片机,单片机也可通过接收器,向手柄发送命令,配置手柄的发送模式。
在这里插入图片描述
接收器的引脚功能如下图所示:
在这里插入图片描述
特别需要提醒的是:按下遥控器的 MODE 键,手柄可配置成“红灯模式”或者“绿灯模式”,当遥控器侧面的 “MODE LED”亮为红灯模式,反之为绿灯模式,“红灯模式”和“绿灯模式”的主要区别是摇杆模拟值得输出。

3.L298N驱动模块:

L298N,是一款接受高电压的电机驱动器,直流电机和步进电机都可以驱动。一片驱动芯片可同时控制两个直流减速电机做不同动作,在6V到46V的电压范围内,提供2安培的电流,并且具有过热自断和反馈检测功能。L298N可对电机进行直接控制,通过主控芯片的I/O输入对其控制电平进行设定,就可为电机进行正转反转驱动,操作简单、稳定性好,可以满足直流电机的大电流驱动条件。

在这里插入图片描述

4.面包板

面包板是由于板子上有很多小插孔,专为电子电路的无焊接实验设计制造的。由于各种电子元器件可根据需要随意插入或拔出,免去了焊接,节省了电路的组装时间,而且元件可以重复使用,所以非常适合电子电路的组装、调试和训练。

在这里插入图片描述

5.小车

由亚克力板、直流电机、小轮胎、电池盒、电池等搭建的小车。
在这里插入图片描述

二、代码

#include <PS2X_lib.h> //for v1.6
#define PS2_DAT 13 //14 
#define PS2_CMD 11 //15
#define PS2_SEL 10 //16
#define PS2_CLK 12 //17
int input1 = 5; //PIN 5(PWM)
int input2 = 6; //PIN 6(PWM)
int input3 = 9; //PIN 9(PWM)
int input4 = 3; //PIN 3(PWM)
#define pressures true
#define rumble true
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
// Reset func 
void (* resetFunc) (void) = 0;
void setup(){ 

Serial.begin(9600);
delay(500);  //added delay to give wireless ps2 module some time to startup, before configuring it
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){ 

Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}  
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType(); 
switch(type) { 

case 0:
Serial.println("Unknown Controller type found ");
break;
case 1:
Serial.println("DualShock Controller found ");
break;
case 2:
Serial.println("GuitarHero Controller found ");
break;
case 3:
Serial.println("Wireless Sony DualShock Controller found ");
break;
}
pinMode(input1,OUTPUT);
pinMode(input2,OUTPUT);                                                    
pinMode(input3,OUTPUT); 
pinMode(input4,OUTPUT);
}
void STOP(){ 

digitalWrite(input1,LOW);
digitalWrite(input2,LOW);  
digitalWrite(input3,LOW);
digitalWrite(input4,LOW);  
}//停止函数
void RUN(){ 

digitalWrite(input1,HIGH); 
digitalWrite(input2,LOW); 
digitalWrite(input3,LOW);
digitalWrite(input4,HIGH); 
analogWrite(input1,250);
analogWrite(input4,250);
}//可调速的前进函数
void LEFT(){ 

digitalWrite(input1,HIGH); 
digitalWrite(input2,LOW);  
digitalWrite(input3,HIGH); 
digitalWrite(input4,LOW); 
analogWrite(input1,200);
analogWrite(input3,200);
}//可调速的左转函数
void RIGHT(){ 

digitalWrite(input1,LOW);
digitalWrite(input2,HIGH);  
digitalWrite(input3,LOW);
digitalWrite(input4,HIGH); 
analogWrite(input2,200);
analogWrite(input4,200);
}//可调速的右转函数
void BACK(){ 

digitalWrite(input1,LOW); 
digitalWrite(input2,HIGH); 
digitalWrite(input3,HIGH);
digitalWrite(input4,LOW); 
analogWrite(input2,250);
analogWrite(input3,250);
}//可调速的后退函数
void loop() { 

if(error == 1){ 
 //skip loop if no controller found
resetFunc();
}
if(type == 2){ 
 //Guitar Hero Controller
ps2x.read_gamepad();          //read controller 
if(ps2x.ButtonPressed(GREEN_FRET))
Serial.println("Green Fret Pressed");
if(ps2x.ButtonPressed(RED_FRET))
Serial.println("Red Fret Pressed");
if(ps2x.ButtonPressed(YELLOW_FRET))
Serial.println("Yellow Fret Pressed");
if(ps2x.ButtonPressed(BLUE_FRET))
Serial.println("Blue Fret Pressed");
if(ps2x.ButtonPressed(ORANGE_FRET))
Serial.println("Orange Fret Pressed"); 
if(ps2x.ButtonPressed(STAR_POWER))
Serial.println("Star Power Command");
if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
Serial.println("Up Strum");
if(ps2x.Button(DOWN_STRUM))
Serial.println("DOWN Strum");
if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(ORANGE_FRET)) { 
     // print stick value IF TRUE
Serial.print("Wammy Bar Position:");
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
} 
}
else { 
 //DualShock Controller
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");      
if(ps2x.Button(PSB_PAD_UP)) { 
      //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
RUN();
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){ 

Serial.print("Right held this hard: ");
LEFT();
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){ 

Serial.print("LEFT held this hard: ");
RIGHT();
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){ 

Serial.print("DOWN held this hard: ");
BACK();
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}   
vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
if (ps2x.NewButtonState()) { 
        //will be TRUE if any button changes state (on to off, or off to on)
if(ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if(ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if(ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if(ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if(ps2x.Button(PSB_TRIANGLE))
Serial.println("Triangle pressed");   
STOP();      
}
if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");
if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
Serial.println("Square just released");     
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { 
 //print stick values if either is TRUE
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX 
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC); 
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC); 
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC); 
}     
}
delay(50);  
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131375.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号