kaka 发表于 2016-9-2 09:59:48

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

本帖最后由 kaka 于 2016-9-2 10:01 编辑

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


在手势传感器前,不断的挥动巴掌,打得都不认识你妈!比谁的巴掌来的更猛烈一点,然后在显示器上将对方推出擂台。
好了,来看看我们的游戏界面和传感器吧


现在我们来讲一讲这个的制作教程吧
首先感谢DF的Luna赠送的一块arduino leonardo主控板和两块手势传感器
制作材料
1、arduino leonardo主控板(DFRobot Leonardo)   两块(我这边只有一块,另一块拿arduino micro代替,两个用法基本一样)


2、手势传感器(APDS-9960传感器)                     两块


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)


编程

Arduino :我们在arduino leonardo主板分别烧写代码
/****************************************************************
GestureTest.ino
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

Tests the gesture sensing abilities of the APDS-9960. Configures
APDS-9960 over I2C and waits for gesture events. Calculates the
direction of the swipe (up, down, left, right) and displays it
on a serial console.

To perform a NEAR gesture, hold your hand
far above the sensor and move it close to the sensor (within 2
inches). Hold your hand there for at least 1 second and move it
away.

To perform a FAR gesture, hold your hand within 2 inches of the
sensor for at least 1 second and then move it above (out of
range) of the sensor.

Hardware Connections:

IMPORTANT: The APDS-9960 can only accept 3.3V!

Arduino PinAPDS-9960 BoardFunction

3.3V         VCC            Power
GND          GND            Ground
A4         SDA            I2C Data
A5         SCL            I2C Clock
2            INT            Interrupt

Resources:
Include Wire.h and SparkFun_APDS-9960.h

Development environment specifics:
Written in Arduino 1.0.5
Tested with SparkFun Arduino Pro Mini 3.3V

This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful, please
buy us a round!

Distributed as-is; no warranty is given.

*******************Modify the description**********************
3.3V-5V   VCC       Power
Void loop () to increase serial port statement causes the wave, will not stop
Library function will be waved in recognition of the changed
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    7 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {
Keyboard.begin();

// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));
   
// Initialize interrupt service routine
attachInterrupt(4, interruptRoutine, FALLING);

// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
} else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
}
   
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
} else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
}
}

void loop() {
if( isr_flag == 1 ) {
    handleGesture();
      if(digitalRead(APDS9960_INT) == 0){
      apds.init();
      apds.enableGestureSensor(true);
    }

    isr_flag = 0;
}
}

void interruptRoutine() {
isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
      Keyboard.write(65);
      break;
      case DIR_DOWN:
      Serial.println("DOWN");
      break;
      case DIR_LEFT:
      Keyboard.write(65);
      break;
      case DIR_RIGHT:
      Serial.println("RIGHT");
      break;
      case DIR_NEAR:
      Serial.println("NEAR");
      break;
      case DIR_FAR:
      Serial.println("FAR");
      break;
      default:
      Serial.println("NONE");
    }
}
}
/****************************************************************
GestureTest.ino
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

Tests the gesture sensing abilities of the APDS-9960. Configures
APDS-9960 over I2C and waits for gesture events. Calculates the
direction of the swipe (up, down, left, right) and displays it
on a serial console.

To perform a NEAR gesture, hold your hand
far above the sensor and move it close to the sensor (within 2
inches). Hold your hand there for at least 1 second and move it
away.

To perform a FAR gesture, hold your hand within 2 inches of the
sensor for at least 1 second and then move it above (out of
range) of the sensor.

Hardware Connections:

IMPORTANT: The APDS-9960 can only accept 3.3V!

Arduino PinAPDS-9960 BoardFunction

3.3V         VCC            Power
GND          GND            Ground
A4         SDA            I2C Data
A5         SCL            I2C Clock
2            INT            Interrupt

Resources:
Include Wire.h and SparkFun_APDS-9960.h

Development environment specifics:
Written in Arduino 1.0.5
Tested with SparkFun Arduino Pro Mini 3.3V

This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful, please
buy us a round!

Distributed as-is; no warranty is given.

*******************Modify the description**********************
3.3V-5V   VCC       Power
Void loop () to increase serial port statement causes the wave, will not stop
Library function will be waved in recognition of the changed
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    7 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {
Keyboard.begin();

// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));
   
// Initialize interrupt service routine
attachInterrupt(4, interruptRoutine, FALLING);

// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
} else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
}
   
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
} else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
}
}

void loop() {
if( isr_flag == 1 ) {
    handleGesture();
      if(digitalRead(APDS9960_INT) == 0){
      apds.init();
      apds.enableGestureSensor(true);
    }

    isr_flag = 0;
}
}

void interruptRoutine() {
isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
      Keyboard.write(66);
      break;
      case DIR_DOWN:
      Serial.println("DOWN");
      break;
      case DIR_LEFT:
      Keyboard.write(66);
      break;
      case DIR_RIGHT:
      Serial.println("RIGHT");
      break;
      case DIR_NEAR:
      Serial.println("NEAR");
      break;
      case DIR_FAR:
      Serial.println("FAR");
      break;
      default:
      Serial.println("NONE");
    }
}
}
电脑端编程采用processing编程
此处需要在processing文件中添加资源文件,图片和文字工具


然后输入代码
int x=50;
int y=0;
PFont f;
PImage ren;
PImage background1;
void setup() {
size(600, 350);
   smooth();
ren = loadImage("2.png");
background1 = loadImage("1.jpg");
f=loadFont("Aharoni-Bold-48.vlw");
textFont(f,48);
}

void draw() {
background(background1);
image(ren,x,y);
if(x<-150||x>250){
   textSize(48);   //字体大小
fill(255,0,0);    //文本颜色
text("YOU WIN",200,100);

}
}

void keyPressed() {
if(key == 'a' || key == 'A'){
    x += 30;
}
if(key == 'b' || key == 'B'){
    x -= 30;
}

redraw();
   
}
资源文件全部署好的代码和符合格式的图片
最后的成果


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


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
页: [1]
查看完整版本: 快来释放你的洪荒之力,手势传感器制作相扑游戏