看了大神们拿Gesture传感器发挥想象实现各种控制,我也来凑凑热闹~~
小小贪吃蛇~~~~~
元件清单
Arduino Uno x1
RGB & Gesture Sensor x1
I/O Expansion x1
SS1306 OLED x1
连线方式
RGB&Gesture Sensor
SDA -> SDA
SCL ->SCL
GND ->GND
VCC ->VCC
INT ->D2
SS1306 OLED
MOSI -> D9
SCK - > D10
DC - > D11
CS - > D12
RES -> D13
代码如下:
- #include "U8glib.h"
-
- #include <Wire.h>
- #include <SparkFun_APDS9960.h>
-
- // Pins
- #define APDS9960_INT 2 // Needs to be an interrupt pin
-
- // Constants
-
- // Global Variables
- SparkFun_APDS9960 apds = SparkFun_APDS9960();
- int isr_flag = 0;
-
- #define RIGHT 0
- #define UP 1
- #define LEFT 2
- #define DOWN 3
-
- /*
- SW SPI COM:
- SCK = 10, MOSI = 9 , CS = 12, A0 = 11, RES = 13
- */
- U8GLIB_SSD1306_128X64 u8g(10, 9, 12, 11, 13);
-
- int score = 0;
- int level = 1;
- int gamespeed = 100;
- int i;
-
- //组成单位,4X4像素点的方块。
- const uint8_t ele[] PROGMEM = {
- 0xf0, //B1111000
- 0xb0, //B1011000
- 0xd0, //B1101000
- 0xf0, //B1111000
- };
-
- //蛇
- void element(int x, int y) {
- u8g.drawBitmapP(x,y, 1,4, ele);
- }
-
- struct FOOD {
- int x;
- int y;
- int yes;
- };
-
- FOOD food = {25, 30, 1};
-
- struct SNAKE {
- int x[200];
- int y[200];
- int node;
- int dir;
- int lefe;
- };
-
- SNAKE snake = {{9,5}, {30,30}, 2, RIGHT, 0};
-
- //游戏基本界面
- void UI() {
- u8g.drawFrame(0,1, 102,62); //内边界
- u8g.drawFrame(0,0, 102,64); //外边界
- u8g.setFont(u8g_font_5x7); //设置字体
- u8g.drawStr(104,12, "LEVEL"); //等级提示
- u8g.drawStr(104,40, "SCORE"); //分数提示
- }
-
- void printScore(int x, int y, int s) {
- u8g.setFont(u8g_font_6x10);
- u8g.setPrintPos(x, y);
- u8g.print(s);
- }
-
- void key() {
- if ( apds.isGestureAvailable() ) {
- switch ( apds.readGesture() ) {
- case DIR_UP:
- snake.dir = UP;
- Serial.println("UP");
- break;
- case DIR_DOWN:
- snake.dir = DOWN;
- Serial.println("DOWN");
- break;
- case DIR_LEFT:
- Serial.println("LEFT");
- break;
- case DIR_RIGHT:
- snake.dir = LEFT;
- snake.dir = RIGHT;
- Serial.println("RIGHT");
- break;
- case DIR_NEAR:
- Serial.println("NEAR");
- break;
- case DIR_FAR:
- Serial.println("FAR");
- break;
- default:
- Serial.println("NONE");
- }
- }
- }
-
- void snakeGame() {
-
- switch(snake.dir) {
- case RIGHT:
- snake.x[0] += 4;
- if(snake.x[0]>=101) {
- snake.x[0] = 1;
- } break;
- case UP:
- snake.y[0] -= 4;
- if(snake.y[0]<=1) {
- snake.y[0] = 58;
- } break;
- case LEFT:
- snake.x[0] -= 4;
- if(snake.x[0]<=0) {
- snake.x[0] = 97;
- } break;
- case DOWN:
- snake.y[0] += 4;
- if(snake.y[0]>=62) {
- snake.y[0] = 2;
- } break;
- }
-
- if((snake.x[0] == food.x) && (snake.y[0] == food.y)) {
- snake.x[0] = food.x;
- snake.y[0] = food.y;
-
- snake.node++;
- food.yes = 1;
- score += 2;
- level = score/10+1;
- }
-
- for(i=snake.node-1;i>0;i--) {
- snake.x[i] = snake.x[i-1];
- snake.y[i] = snake.y[i-1];
- }
- }
-
- void setup() {
- // 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(0, 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 interruptRoutine() {
- isr_flag = 1;
- }
-
- void loop() {
- if( isr_flag == 1 ) {
- key();
- if(digitalRead(APDS9960_INT) == 0){
- apds.init();
- apds.enableGestureSensor(true);
- }
-
- isr_flag = 0;
- }
- u8g.firstPage();
- do {
- UI();
- for(i=0; i<snake.node;i++) {
- element(snake.x[i], snake.y[i]);
- }
-
- element(food.x, food.y);
-
- printScore(103, 62, food.x);
- printScore(116, 62, food.y);
-
- printScore(109, 22, level);
- printScore(109, 50, score);
- }while(u8g.nextPage());
- key();
- snakeGame();
- delay(gamespeed);
- }
复制代码
用到的库:u8glib_arduino_v1.17.zip
|