Arduino Code for Hand gesture detection

For Realtime detection, I have used 5 features and 100 samples in a 50Hz sampling rate.
 avatar
Sasanka
c_cpp
7 days ago
3.6 kB
6
Indexable
#include <Arduino.h>
#include "gesture_model.h"   // emlearn-generated model header

// === CONFIGURATION ===
#define NUM_FEATURES 5       // number of FSR sensors
#define NUM_SAMPLES 100      // samples per gesture
#define SAMPLING_RATE 50     // Hz
#define SAMPLE_INTERVAL (1000 / SAMPLING_RATE)  // in milliseconds

// Assign analog pins for 5 FSR sensors
int fsrPins[NUM_FEATURES] = {A0, A1, A2, A3, A6};

// Gesture class names
const char* gestureNames[5] = {"close", "ok", "open", "peace", "thumbs-up"};

// Store baseline (neutral open-hand readings)
int baseline[NUM_FEATURES] = {0};

// === Function to calibrate baseline ===
void calibrateBaseline() {
  Serial.println("Calibrating baseline... Keep your hand relaxed (open).");
  long sums[NUM_FEATURES] = {0};

  for (int s = 0; s < NUM_SAMPLES; s++) {
    for (int i = 0; i < NUM_FEATURES; i++) {
      sums[i] += analogRead(fsrPins[i]);
    }
    delay(SAMPLE_INTERVAL);
  }

  for (int i = 0; i < NUM_FEATURES; i++) {
    baseline[i] = sums[i] / NUM_SAMPLES;
  }

  Serial.println("Baseline calibration complete!");
  Serial.println("Baseline values:");
  for (int i = 0; i < NUM_FEATURES; i++) {
    Serial.print("FSR");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(baseline[i]);
  }
  Serial.println();
}

// === Function to capture gesture samples and prepare features ===
void captureSamples(int16_t features[NUM_FEATURES]) {
  long sums[NUM_FEATURES] = {0};

  Serial.println("Collecting 100 samples...");

  for (int s = 0; s < NUM_SAMPLES; s++) {
    unsigned long startTime = millis();

    for (int i = 0; i < NUM_FEATURES; i++) {
      int sensorValue = analogRead(fsrPins[i]);
      sums[i] += sensorValue;
    }

    // Maintain 50 Hz sampling rate
    while (millis() - startTime < SAMPLE_INTERVAL);
  }

  // Calculate mean and subtract baseline
  for (int i = 0; i < NUM_FEATURES; i++) {
    int meanVal = sums[i] / NUM_SAMPLES;
    int diff = meanVal - baseline[i];  // center around baseline
    features[i] = (int16_t)diff;
  }

  // Print for debugging
  Serial.println("Mean feature values (raw difference):");
  for (int i = 0; i < NUM_FEATURES; i++) {
    Serial.print("FSR");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(features[i]);
  }
  Serial.println("Data collection complete.\n");
}

// === Setup ===
void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Use 12-bit ADC if available (Nano 33 BLE Sense supports it)
  #if defined(ARDUINO_ARCH_NRF52840)
    analogReadResolution(12);
  #endif

  Serial.println("=== Hand Gesture Detection ===");
  Serial.println("Press ENTER, then type 'start' to classify a gesture.");
  Serial.println();

  delay(2000);
  calibrateBaseline();
}

// === Loop ===
void loop() {
  static String command = "";

  // Check for serial command
  if (Serial.available() > 0) {
    char c = Serial.read();

    if (c == '\n' || c == '\r') {
      if (command == "start") {
        int16_t features[NUM_FEATURES] = {0};
        captureSamples(features);

        // Run prediction
        int predictedClass = gesture_model_predict(features, NUM_FEATURES);

        Serial.print("✅ Detected Gesture: ");
        Serial.println(gestureNames[predictedClass]);
        Serial.println("-----------------------------");
      }

      command = "";  // reset
      Serial.println("Type 'start' to detect another gesture.");
    } 
    else {
      command += c;
    }
  }
}
Editor is loading...
Leave a Comment