快来释放你的洪荒之力,手势传感器制作相扑游戏

2016-09-021.7万
AI 快速预览详细 收起
本文介绍了如何使用Arduino Leonardo主控板和APDS-9960手势传感器制作一个互动相扑游戏。项目所需材料包括Arduino Leonardo主控板、两块APDS-9960手势传感器和若干杜邦线。通过I2C接口连接手势传感器,并使用中断4(引脚7)来处理手势事件。最终效果是玩家可以通过挥动手掌控制游戏角色进行相扑比赛。这个项目适合12岁以上的青少年参与,能够锻炼他们的编程和硬件技能。


创客小伙伴们,快来释放你的洪荒之力吧,手势传感器制作相扑游戏玩法:让妈妈再爱我一次
快来释放你的洪荒之力,手势传感器制作相扑游戏图1快来释放你的洪荒之力,手势传感器制作相扑游戏图2快来释放你的洪荒之力,手势传感器制作相扑游戏图3

在手势传感器前,不断的挥动巴掌,打得都不认识你妈!比谁的巴掌来的更猛烈一点,然后在显示器上将对方推出擂台。
好了,来看看我们的游戏界面和传感器吧
快来释放你的洪荒之力,手势传感器制作相扑游戏图4快来释放你的洪荒之力,手势传感器制作相扑游戏图5

现在我们来讲一讲这个的制作教程吧
首先感谢DF的Luna赠送的一块Arduino leonardo主控板和两块手势传感器
制作材料
1、arduino leonardo主控板(DFRobot Leonardo)   两块(我这边只有一块,另一块拿arduino micro代替,两个用法基本一样)
快来释放你的洪荒之力,手势传感器制作相扑游戏图6快来释放你的洪荒之力,手势传感器制作相扑游戏图7

2、手势传感器(APDS-9960传感器)                     两块
快来释放你的洪荒之力,手势传感器制作相扑游戏图8

3、杜邦线若干

接线
接线方式:
手势传感器使用的I2C接口
arduino leonardo和 micro的I2C接口是,对于的引脚分别是2(SDA)和3(SCL)
外部中断分别是,3(中断0)、2(中断1)、0(中断2)、1(中断3)和7(中断4)
由于2,3被I2C接口使用0,1被串口使用,所以我这里用了7(中断4)
快来释放你的洪荒之力,手势传感器制作相扑游戏图9快来释放你的洪荒之力,手势传感器制作相扑游戏图10

编程

Arduino :我们在arduino leonardo主板分别烧写代码
  1. /****************************************************************
  2. GestureTest.ino
  3. APDS-9960 RGB and Gesture Sensor
  4. Shawn Hymel @ SparkFun Electronics
  5. May 30, 2014
  6. [url=https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor]https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor[/url]
  7. Tests the gesture sensing abilities of the APDS-9960. Configures
  8. APDS-9960 over I2C and waits for gesture events. Calculates the
  9. direction of the swipe (up, down, left, right) and displays it
  10. on a serial console.
  11. To perform a NEAR gesture, hold your hand
  12. far above the sensor and move it close to the sensor (within 2
  13. inches). Hold your hand there for at least 1 second and move it
  14. away.
  15. To perform a FAR gesture, hold your hand within 2 inches of the
  16. sensor for at least 1 second and then move it above (out of
  17. range) of the sensor.
  18. Hardware Connections:
  19. IMPORTANT: The APDS-9960 can only accept 3.3V!
  20.   
  21. Arduino Pin  APDS-9960 Board  Function
  22.   
  23. 3.3V         VCC              Power
  24. GND          GND              Ground
  25. A4           SDA              I2C Data
  26. A5           SCL              I2C Clock
  27. 2            INT              Interrupt
  28. Resources:
  29. Include Wire.h and SparkFun_APDS-9960.h
  30. Development environment specifics:
  31. Written in Arduino 1.0.5
  32. Tested with SparkFun Arduino Pro Mini 3.3V
  33. This code is beerware; if you see me (or any other SparkFun
  34. employee) at the local, and you've found our code helpful, please
  35. buy us a round!
  36. Distributed as-is; no warranty is given.
  37. *******************Modify the description**********************
  38. 3.3V-5V     VCC       Power
  39. Void loop () to increase serial port statement causes the wave, will not stop
  40. Library function will be waved in recognition of the changed
  41. ****************************************************************/
  42. #include <Wire.h>
  43. #include <SparkFun_APDS9960.h>
  44. // Pins
  45. #define APDS9960_INT    7 // Needs to be an interrupt pin
  46. // Constants
  47. // Global Variables
  48. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  49. int isr_flag = 0;
  50. void setup() {
  51.   Keyboard.begin();
  52.   // Initialize Serial port
  53.   Serial.begin(9600);
  54.   Serial.println();
  55.   Serial.println(F("--------------------------------"));
  56.   Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  57.   Serial.println(F("--------------------------------"));
  58.    
  59.   // Initialize interrupt service routine
  60.   attachInterrupt(4, interruptRoutine, FALLING);
  61.   // Initialize APDS-9960 (configure I2C and initial values)
  62.   if ( apds.init() ) {
  63.     Serial.println(F("APDS-9960 initialization complete"));
  64.   } else {
  65.     Serial.println(F("Something went wrong during APDS-9960 init!"));
  66.   }
  67.    
  68.   // Start running the APDS-9960 gesture sensor engine
  69.   if ( apds.enableGestureSensor(true) ) {
  70.     Serial.println(F("Gesture sensor is now running"));
  71.   } else {
  72.     Serial.println(F("Something went wrong during gesture sensor init!"));
  73.   }
  74. }
  75. void loop() {
  76.   if( isr_flag == 1 ) {
  77.     handleGesture();
  78.       if(digitalRead(APDS9960_INT) == 0){
  79.       apds.init();
  80.       apds.enableGestureSensor(true);
  81.     }
  82.     isr_flag = 0;
  83.   }
  84. }
  85. void interruptRoutine() {
  86.   isr_flag = 1;
  87. }
  88. void handleGesture() {
  89.     if ( apds.isGestureAvailable() ) {
  90.     switch ( apds.readGesture() ) {
  91.       case DIR_UP:
  92.         Keyboard.write(65);
  93.         break;
  94.       case DIR_DOWN:
  95.         Serial.println("DOWN");
  96.         break;
  97.       case DIR_LEFT:
  98.         Keyboard.write(65);
  99.         break;
  100.       case DIR_RIGHT:
  101.         Serial.println("RIGHT");
  102.         break;
  103.       case DIR_NEAR:
  104.         Serial.println("NEAR");
  105.         break;
  106.       case DIR_FAR:
  107.         Serial.println("FAR");
  108.         break;
  109.       default:
  110.         Serial.println("NONE");
  111.     }
  112.   }
  113. }
复制代码
  1. /****************************************************************
  2. GestureTest.ino
  3. APDS-9960 RGB and Gesture Sensor
  4. Shawn Hymel @ SparkFun Electronics
  5. May 30, 2014
  6. [url=https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor]https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor[/url]
  7. Tests the gesture sensing abilities of the APDS-9960. Configures
  8. APDS-9960 over I2C and waits for gesture events. Calculates the
  9. direction of the swipe (up, down, left, right) and displays it
  10. on a serial console.
  11. To perform a NEAR gesture, hold your hand
  12. far above the sensor and move it close to the sensor (within 2
  13. inches). Hold your hand there for at least 1 second and move it
  14. away.
  15. To perform a FAR gesture, hold your hand within 2 inches of the
  16. sensor for at least 1 second and then move it above (out of
  17. range) of the sensor.
  18. Hardware Connections:
  19. IMPORTANT: The APDS-9960 can only accept 3.3V!
  20.   
  21. Arduino Pin  APDS-9960 Board  Function
  22.   
  23. 3.3V         VCC              Power
  24. GND          GND              Ground
  25. A4           SDA              I2C Data
  26. A5           SCL              I2C Clock
  27. 2            INT              Interrupt
  28. Resources:
  29. Include Wire.h and SparkFun_APDS-9960.h
  30. Development environment specifics:
  31. Written in Arduino 1.0.5
  32. Tested with SparkFun Arduino Pro Mini 3.3V
  33. This code is beerware; if you see me (or any other SparkFun
  34. employee) at the local, and you've found our code helpful, please
  35. buy us a round!
  36. Distributed as-is; no warranty is given.
  37. *******************Modify the description**********************
  38. 3.3V-5V     VCC       Power
  39. Void loop () to increase serial port statement causes the wave, will not stop
  40. Library function will be waved in recognition of the changed
  41. ****************************************************************/
  42. #include <Wire.h>
  43. #include <SparkFun_APDS9960.h>
  44. // Pins
  45. #define APDS9960_INT    7 // Needs to be an interrupt pin
  46. // Constants
  47. // Global Variables
  48. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  49. int isr_flag = 0;
  50. void setup() {
  51.   Keyboard.begin();
  52.   // Initialize Serial port
  53.   Serial.begin(9600);
  54.   Serial.println();
  55.   Serial.println(F("--------------------------------"));
  56.   Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  57.   Serial.println(F("--------------------------------"));
  58.    
  59.   // Initialize interrupt service routine
  60.   attachInterrupt(4, interruptRoutine, FALLING);
  61.   // Initialize APDS-9960 (configure I2C and initial values)
  62.   if ( apds.init() ) {
  63.     Serial.println(F("APDS-9960 initialization complete"));
  64.   } else {
  65.     Serial.println(F("Something went wrong during APDS-9960 init!"));
  66.   }
  67.    
  68.   // Start running the APDS-9960 gesture sensor engine
  69.   if ( apds.enableGestureSensor(true) ) {
  70.     Serial.println(F("Gesture sensor is now running"));
  71.   } else {
  72.     Serial.println(F("Something went wrong during gesture sensor init!"));
  73.   }
  74. }
  75. void loop() {
  76.   if( isr_flag == 1 ) {
  77.     handleGesture();
  78.       if(digitalRead(APDS9960_INT) == 0){
  79.       apds.init();
  80.       apds.enableGestureSensor(true);
  81.     }
  82.     isr_flag = 0;
  83.   }
  84. }
  85. void interruptRoutine() {
  86.   isr_flag = 1;
  87. }
  88. void handleGesture() {
  89.     if ( apds.isGestureAvailable() ) {
  90.     switch ( apds.readGesture() ) {
  91.       case DIR_UP:
  92.         Keyboard.write(66);
  93.         break;
  94.       case DIR_DOWN:
  95.         Serial.println("DOWN");
  96.         break;
  97.       case DIR_LEFT:
  98.         Keyboard.write(66);
  99.         break;
  100.       case DIR_RIGHT:
  101.         Serial.println("RIGHT");
  102.         break;
  103.       case DIR_NEAR:
  104.         Serial.println("NEAR");
  105.         break;
  106.       case DIR_FAR:
  107.         Serial.println("FAR");
  108.         break;
  109.       default:
  110.         Serial.println("NONE");
  111.     }
  112.   }
  113. }
复制代码

电脑端编程采用processing编程
此处需要在processing文件中添加资源文件,图片和文字工具
快来释放你的洪荒之力,手势传感器制作相扑游戏图11快来释放你的洪荒之力,手势传感器制作相扑游戏图12

然后输入代码
  1. int x=50;
  2. int y=0;
  3. PFont f;  
  4. PImage ren;
  5. PImage background1;
  6. void setup() {
  7.   size(600, 350);
  8.    smooth();
  9.   ren = loadImage("2.png");
  10.   background1 = loadImage("1.jpg");
  11.   f=loadFont("Aharoni-Bold-48.vlw");
  12.   textFont(f,48);
  13. }
  14. void draw() {
  15.   background(background1);
  16.   image(ren,x,y);
  17.   if(x<-150||x>250){
  18.    textSize(48);   //字体大小
  19.   fill(255,0,0);    //文本颜色
  20.   text("YOU WIN",200,100);
  21.   
  22. }
  23. }
  24. void keyPressed() {
  25. if(key == 'a' || key == 'A'){
  26.     x += 30;
  27. }
  28. if(key == 'b' || key == 'B'){
  29.     x -= 30;
  30. }
  31.   
  32. redraw();
  33.    
  34. }
复制代码

资源文件全部署好的代码和符合格式的图片

最后的成果
快来释放你的洪荒之力,手势传感器制作相扑游戏图13快来释放你的洪荒之力,手势传感器制作相扑游戏图14快来释放你的洪荒之力,手势传感器制作相扑游戏图15快来释放你的洪荒之力,手势传感器制作相扑游戏图16快来释放你的洪荒之力,手势传感器制作相扑游戏图17快来释放你的洪荒之力,手势传感器制作相扑游戏图18快来释放你的洪荒之力,手势传感器制作相扑游戏图19快来释放你的洪荒之力,手势传感器制作相扑游戏图20快来释放你的洪荒之力,手势传感器制作相扑游戏图21

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(6)
svw
svw
有创意!
hnyzcj
hnyzcj
很好玩的游戏。
dbc0301
dbc0301
arduino leonardo的中断不是只有从0到3一共4个中断吗?
为啥会有中断4?
dbc0301
dbc0301回复kaka
百度。。。
好吧,官网上确实有说,看来还是官方的准确。
:lol
kaka
kaka
作者
回复dbc0301
你是在哪里查的,我是在arduino的官网查的,也经过测试,确实存在中断4
共4条回复,已加载2条,点击查看更多共4条回复,已加载全部
dsweiliang
dsweiliang
厉害
kaka
kaka
作者

luna
luna
楼主好有才~看上去好好玩啊!而且还是2个人可以玩的好游戏~
luna
luna回复kaka
是吗?啥样的盒子啊?是不是透光度不高?
kaka
kaka
作者
回复luna
对了,我用3D打印机打印了盒子,可是将手势传感器放进盒子里面,感觉不是很灵敏了
共3条回复,已加载2条,点击查看更多共3条回复,已加载全部
- 没有更多了 -