Go Down

Topic: Controlling dual motor driver with gesture sensor (Read 1 time) previous topic - next topic

Ctravis

Hey. guys i am new to the forum and arduino. I have a question. I am working on a project of my class. I am making a tracked chasis robot that is controlled by a gesture sensor that senses  "up" "down" "left" "right " movements.

These are the parts i am using:

Arduino UNO
Tamiya Tracked vehicle kit
Twin Gearbox with 2 motors
Sparkfun dual motor driver
Sparkfun APDS-9960 Gesture sensor

I want to use the Library that come with the gesture sensor but i am not sure how to incorporate it into my existing code. Any help would be appreciated. Let me know what other info is needed

Here is the code i have so far

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

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

// Constants

// Global Variables
//Define the gesture sensor here
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

//motor A connected between A01 and A02
//motor B connected between B01 and B02


//Define standby mode
int STBY = 10; //standby

//second we need to define the motors
//each motor has forward and backward motion. direction depends on which int you call.

//motor Left
int PWMA = 3; //Speed control
int AIN1 = 9; //forward motion
int AIN2 = 8; //backwards motion

//motor Right
int PWMB = 5; //Speed Control
int BIN1 = 11; //forward motion
int BIN2 = 12; //backwards motion



//now define the inputs and outputs

void setup() {
//standby
  pinMode(STBY, OUTPUT);

//motor Left
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

//motor Right
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
 
 // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);
 
 // 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 System 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 recoginition activated. Sensor is now running. Welcome Clinton."));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        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");
    }
  }
 
  if (apds.readGesture == 'RIGHT') {
  digitalWrite (BIN1, HIGH);
  }
}

Go Up