2016-9-2 09:59:48 [显示全部楼层]
7268浏览
查看: 7268|回复: 13

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

[复制链接]
本帖最后由 kaka 于 2016-9-2 10:01 编辑

创客小伙伴们,快来释放你的洪荒之力吧,手势传感器制作相扑游戏玩法:让妈妈再爱我一次
快来释放你的洪荒之力,手势传感器制作相扑游戏图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

luna  初级技神

发表于 2016-9-2 10:28:32

楼主好有才~看上去好好玩啊!而且还是2个人可以玩的好游戏~
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2016-9-2 10:44:36

luna 发表于 2016-9-2 10:28
楼主好有才~看上去好好玩啊!而且还是2个人可以玩的好游戏~

谢谢luna的补充,是的,是可以2个人一起玩的对战游戏,这个才是重点,看谁甩别人巴掌的速度更快,千万不能让马蓉知道,不然,宝宝的脸上不知道又要多少巴掌了
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2016-9-2 10:50:22

luna 发表于 2016-9-2 10:28
楼主好有才~看上去好好玩啊!而且还是2个人可以玩的好游戏~

对了,我用3D打印机打印了盒子,可是将手势传感器放进盒子里面,感觉不是很灵敏了
回复

使用道具 举报

luna  初级技神

发表于 2016-9-2 10:54:53

kaka 发表于 2016-9-2 10:50
对了,我用3D打印机打印了盒子,可是将手势传感器放进盒子里面,感觉不是很灵敏了 ...

是吗?啥样的盒子啊?是不是透光度不高?
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2016-9-2 11:30:05

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

使用道具 举报

dsweiliang  初级技神

发表于 2016-9-2 17:16:56

厉害
回复

使用道具 举报

dbc0301  高级技匠

发表于 2016-9-2 21:09:26

arduino leonardo的中断不是只有从0到3一共4个中断吗?
为啥会有中断4?
回复

使用道具 举报

hnyzcj  版主

发表于 2016-9-3 06:15:12

很好玩的游戏。
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2016-9-5 10:27:57

dbc0301 发表于 2016-9-2 21:09
arduino leonardo的中断不是只有从0到3一共4个中断吗?
为啥会有中断4?

External Interrupts: 3 (interrupt 0), 2 (interrupt 1), 0 (interrupt 2), 1 (interrupt 3) and 7 (interrupt 4).是4个中断
回复

使用道具 举报

svw  初级技匠

发表于 2016-9-6 09:41:50

有创意!
回复

使用道具 举报

dbc0301  高级技匠

发表于 2016-9-6 21:29:56

kaka 发表于 2016-9-5 10:27
External Interrupts: 3 (interrupt 0), 2 (interrupt 1), 0 (interrupt 2), 1 (interrupt 3) and 7 (inte ...

型号                      int.0        int.1        int.2        int.3        int.4        int.5
UNO\Ethernet        2        3                                    
Mega2560              2        3        21        20        19        18
Leonardo                3        2        0        1                  
Due                      所有IO口均可
我查了啊,却是是4个。。。
回复

使用道具 举报

kaka  高级技师
 楼主|

发表于 2016-9-7 13:59:41

dbc0301 发表于 2016-9-6 21:29
型号                      int.0        int.1        int.2        int.3        int.4        int.5
UN ...

你是在哪里查的,我是在arduino的官网查的,也经过测试,确实存在中断4
回复

使用道具 举报

dbc0301  高级技匠

发表于 2016-9-11 22:54:17

kaka 发表于 2016-9-7 13:59
你是在哪里查的,我是在arduino的官网查的,也经过测试,确实存在中断4

百度。。。
好吧,官网上确实有说,看来还是官方的准确。
:lol
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail