SparkFun RGB and Gesture Sensor e o Processing

12787-01

Aqui está uma ligação simples entre o sensor da SparkFun RGB and Gesture Sensor APDS-9960 e o Processing. É necessário instalar o exemplo que está na página da Sparkfun. Fui testado no processing 2.1 e com uma board arduino uno. Fui também utilizado o exemplo do Arduino que está na página da Sparkfun.

//no arduino
#include <Wire.h>
#include <SparkFun_APDS9960.h>
#define APDS9960_INT    2

SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  attachInterrupt(0, interruptRoutine, FALLING);

  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }

  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();
    isr_flag = 0;
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
  if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.write("U"); //up
        Serial.write("\n");
        break;
      case DIR_DOWN:
        Serial.write("D"); // down
        Serial.write("\n");
        break;
      case DIR_LEFT:
        Serial.write("L"); //left
        Serial.write("\n");
        break;
      case DIR_RIGHT:
        Serial.write("R"); //right
        Serial.write("\n");
        break;
      case DIR_NEAR:
        Serial.write("N"); //near
        Serial.write("\n");
        break;
      case DIR_FAR:
        Serial.write("F"); //far
        Serial.write("\n");
        break;
      default:
        Serial.write("NULL"); //null
        Serial.write("\n");
    }
  }
}
//no processing
import processing.serial.*; 
Serial porta;
int valor = 0;
int aX=200, aY=200;
int veloci=20;

void setup() {
  size(400, 400);
  porta = new Serial(this, "COM12", 9600);
}

void draw() {
  background(255,255,255);
  while (porta.available () > 0) {
    valor = porta.read();
    if (valor=='R') {   
      println("right");
      aX=aX+veloci;
    }
    if (valor=='L') {   
      println("left");
      aX=aX-veloci;
    }
    if (valor=='U') {   
      println("up");
      aY=aY-veloci;
    }
    if (valor=='D') {   
      println("down");
      aY=aY+veloci;
    }
  }
  ellipse(aX, aY, 50, 50);
}


+infos(sparkfun): LINK

Tags : , , , ,

0 thoughts on “SparkFun RGB and Gesture Sensor e o Processing”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.