Arduino_JoyStick

The joystick module is an extra module so requires a connection to the Arduino mainboard with wire jumpers. Three pins (pin number 9, 68 and 69) are used and 5V power should be supplied. The joystick module can detect which position the joystick faces (CENTER, LEFT, RIGHT, UP and DOWN).

Predefined Functions in Library
int analogRead(pin);
Set the address/mode (output) and initialize the joystick module.

  • parameter :
    pin : pin number of the module
  • return value : 0~1023 analogue value
/* joystick.ino */
#define JOY_X 68
#define JOY_Y 69
#define JOY_SEL 9

int xCenterPos = 0; 
int yCenterPos = 0; 
int xyTH = 100;

void setup() 
{ 
  pinMode(JOY_X, INPUT); 
  pinMode(JOY_Y, INPUT); 
  pinMode(JOY_SEL, INPUT);
  xCenterPos = analogRead(JOY_X); 
  yCenterPos = analogRead(JOY_Y);
  Serial.begin(115200);
}

void loop() 
{
  if(analogRead(JOY_X) < xCenterPos - xyTH)  { Serial.print("LEFT");} 
  else if(analogRead(JOY_X) > xCenterPos + xyTH) { Serial.print("RIGHT");}   
  else { Serial.print("CENTER");}
  Serial.print(" ");
  if(analogRead(JOY_Y) < yCenterPos - xyTH) { Serial.println("UP");} 
  else if(analogRead(JOY_Y) > yCenterPos + xyTH) { Serial.println("DOWN");} 
  else { Serial.println("CENTER");}

  if(digitalRead(JOY_SEL) == LOW) { Serial.println("Button Pushed");}
  delay(500);
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 家长课堂上,老师说,给孩子们养一盆花吧。 家长们纷纷说道 :OK OK 紧接着,老师时候,记得花要在雨鞋里养着哈。...
    怪物爸爸阅读 217评论 0 0
  • 元认知能力就是对自己思考过程进行思考的能力。元认知能力是可以通过后天训练习得的,也是升级大脑系统的一个非常好的手段...
    潇小娴阅读 313评论 0 0
  • 我很想跟自己说过去就过去了,来日方长,但是心里还是有一丝遗憾。 20160611端午...
    慢跑黄阅读 322评论 0 1
  • 初识八月八,分离十一月八,如此特殊。 只言片语,校门口见,仿佛阳光照进了我的 世界,从此,我心底再无阴霾!
    叶心子阅读 250评论 0 0