Where to put other motor code with Cyberon Voice Recognition?

I am using Cyberon Voice Recognition to achieve the voice recognition part of my project. It works amazingly well. I'm shocked with how well it works. But now where do I put the rest of my code? I would like to control a servo motor but I do not understand where I would put the servo code so it triggers when I say the voice command. If I have multiple voice commands how do I trigger different actions to happen depending on what voice command I say? The documentation, at least on the Arduino site, is very lacking and I could not find any simple examples for me to follow.

Post your code here.

When you recognize a command, set a flag for "command x heard." Execute the heard-command in sequence. Clear the flag. If the device needs time to finish its task, use an event timer to give the device slices of time toward completing its task until it is finished.

1 Like

Found some code on github (https://github.com/nthornton00/arduino-voice-motion-sensor/blob/main/Info.txt) of someone using the Cyberon voice commands to control an arduino and tried adapting their code for my use. Which is the code you see below. Unfortunately the code below is not moving my servo motor like I hoped it would. Can you help me debug?

#include <Arduino.h>
#include <DSpotterSDK_MakerHL.h>
#include <LED_Control.h>
// #include <Arduino_APDS9960.h>

// The DSpotter License Data.
#include "CybLicense.h"
#define DSPOTTER_LICENSE g_lpdwLicense

// The DSpotter Keyword Model Data.
#if defined(TARGET_ARDUINO_NANO33BLE) || defined(TARGET_PORTENTA_H7) || defined(TARGET_NICLA_VISION)
// For ARDUINO_NANO33BLE and PORTENTA_H7
#include "Model_L1.h"  // The packed level one model file.
// For NANO_RP2040_CONNECT
#elif defined(TARGET_NANO_RP2040_CONNECT)
#include "Model_L0.h"  // The packed level zero model file.
#endif
#define DSPOTTER_MODEL g_lpdwModel

#define COMMAND_LED_GREEN  10000
#define COMMAND_LED_RED    10001
#define COMMAND_LED_BLUE   10002
#define COMMAND_LED_OFF    10003

#define BOX_SERVO 12 //led connected to pin 12 on breadboard
//bool sensorOn = true; //boolean flag to turn on proximity sensor when true or turn it off when false
int pos = 0; //position of servo, 0 = first position, range is 0-179
//unsigned long currentMillis; // current interaction with proximity sensor in milliseconds
//unsigned long previousMillis = 0; //previous interaction with proximity sensor in milliseconds
const long interval = 1000; //delay between servo position and returning to 0 position
const long idle = 10000; //time before servo will return to 0 position

static DSpotterSDKHL g_oDSpotterSDKHL;

void VRCallback(int nFlag, int nID, int nScore, int nSG, int nEnergy) {
  if (nFlag == DSpotterSDKHL::InitSuccess) {
    Serial.println("Voice Recognition Initialization Successful!");
  } 
    else if (nFlag == DSpotterSDKHL::GetResult) {
    switch (nID) {
      case COMMAND_LED_GREEN:
        LED_RGB_Green();
        Serial.println("Sensor is OFF");
        // sensorOn = false;
        break;
      case COMMAND_LED_RED:
        LED_RGB_Red();
        Serial.println("Sensor is ON");
        // sensorOn = true;
        break;
      case COMMAND_LED_BLUE:
        LED_RGB_Blue();
        Serial.println("Motor at 90 degrees");
        pos = 90;
        digitalWrite(BOX_SERVO, pos);
        break;
      case COMMAND_LED_OFF:
        LED_RGB_Off();
        Serial.println("Motor at 0 degrees");
        pos = 0;
        digitalWrite(BOX_SERVO, pos);
        break;
      default:
        break;
    }
  } else if (nFlag == DSpotterSDKHL::ChangeStage) {
    switch (nID) {
      case DSpotterSDKHL::TriggerStage:
        LED_RGB_Off();
        LED_BUILTIN_Off();
        break;
      case DSpotterSDKHL::CommandStage:
        LED_BUILTIN_On();
        break;
      default:
        break;
    }
  } else if (nFlag == DSpotterSDKHL::GetError) {
    if (nID == DSpotterSDKHL::LicenseFailed) {
      //Serial.print("DSpotter license failed! The serial number of your device is ");
      //Serial.println(DSpotterSDKHL::GetSerialNumber());
    }
    g_oDSpotterSDKHL.Release();
    while (1);  //hang loop
  } else if (nFlag == DSpotterSDKHL::LostRecordFrame) {
  }
}

void setup() {
  //breadboard servo
  pinMode(BOX_SERVO, OUTPUT);

  // Init LED control
  LED_Init_All();

  // Init Serial output for show debug info
  Serial.begin(9600);
  while (!Serial); //hang loop if Serial is not running properly
  DSpotterSDKHL::ShowDebugInfo(true);

  // Init VR engine & Audio
  if (g_oDSpotterSDKHL.Init(DSPOTTER_LICENSE, sizeof(DSPOTTER_LICENSE), DSPOTTER_MODEL, VRCallback) != DSpotterSDKHL::Success)
    return; //if VR not initialized properly, return

  // //Error Message For Initializing Proximity Sensor
  // if (!APDS.begin()) {
  //   Serial.println("Error initializing APDS9960 sensor!");
  // }

  // // set the LEDs pins as outputs for Proximity Sensor
  // pinMode(LEDR, OUTPUT);
  // pinMode(LEDG, OUTPUT);
  // pinMode(LEDB, OUTPUT);


  // // turn all the LEDs off
  // digitalWrite(LEDR, 90);
  // digitalWrite(LEDG, 90);
  // digitalWrite(LEDB, 90);
}



void loop() {
  // Do VR
  g_oDSpotterSDKHL.DoVR();

  // //Proximity Sensor Code
  // if (APDS.proximityAvailable() && sensorOn == true) {
  //   int proximity = APDS.readProximity();
  //   if (proximity <= 245) { //if user distance is <= 245 mm 
  //     //blue led
  //     digitalWrite(LEDB, 0);
  //     digitalWrite(LEDR, 90);
  //     digitalWrite(LEDG, 90);
      
  //     currentMillis = millis(); //stopwatch in milliseconds
  //     if (currentMillis - previousMillis >= interval) { //slight delay so light can not be spammed
  //       // save the last time you blinked the LED
  //       previousMillis = currentMillis;
  //       //turns lights on and off based on last status
  //       if(pos == 0)
  //       pos = 90;
  //       else{
  //         pos = 0;
  //       }
  //       digitalWrite(BOX_SERVO, pos);
  //     }
  //   } else { //if user distance is > 245 mm 
  //     //green led
  //     digitalWrite(LEDG, 0);
  //     digitalWrite(LEDR, 90);
  //     digitalWrite(LEDB, 90);
  //     lightDelay();
  //   }
  // } //end of proximity sensor code
} //end of loop

//function to automatically turn off lights if interactions between the proximity sensor are too far in between
// void lightDelay() {
//   currentMillis = millis(); //stopwatch in milliseconds
//   if (currentMillis - previousMillis >= idle && pos == 90) {
//     previousMillis = currentMillis;
//     pos = 0;
//     digitalWrite(BOX_SERVO, pos); //turns LED off
//   }
// }

Why not? What servo?

The digitalWrite() can't outputs 90, it can't outputs anything but 0 or 1.

It seems that you have to learn how to move servo first. Use the examples to the Servo.h library.