Nano RP2040 Connect with voice recognition model won't work when not plugged into serial monitor

Hello, firstly I am fairly new to coding and anything arduino, I am trying to make a system that uses the voice recognition module to move some servos while running off of an external power supply. My problem however, is that whenever the arduino is plugged into the external power supply and not the serial monitor the code simply does not function. It works perfectly fine when the serial monitor is plugged in and I know that the power supply is working properly but the code just seems not to work.
Here is the code I am using if that helps at all.

#include <Arduino_LSM6DSOX.h>
#include <Arduino.h>
#include <DSpotterSDK_MakerHL.h>
#include <LED_Control.h>
#include <Servo.h>
#include "CybLicense_1729631303.h"

const unsigned long doubleTapTime = 300; // Time in milliseconds for double-tap detection
const unsigned long cooldownTime = 3000;  // Cooldown time in milliseconds after a successful double tap
unsigned long lastTapTime = 0; // To track the last tap time
unsigned long lastActionTime = 0; // To track the last action time
int tapCount = 0; // Count the number of taps

#define DSPOTTER_LICENSE g_lpdwLicense

// The DSpotter Keyword Model Data.
#if defined(TARGET_ARDUINO_NANO33BLE) || defined(TARGET_PORTENTA_H7) || defined(TARGET_NICLA_VISION)
#include "Model_1729631303.h" // The packed level one model file.
#elif defined(TARGET_NANO_RP2040_CONNECT)
#include "Model_1729631303.h" // The packed level zero model file.
#endif
#define DSPOTTER_MODEL g_lpdwModel

// The VR engine object. Only one instance can exist.
static DSpotterSDKHL g_oDSpotterSDKHL;

Servo myservo1; // Declare servo objects
Servo myservo2;

bool servoposition() {
    return myservo1.read() >= 90; 
}

// Callback function for VR engine
void VRCallback(int nFlag, int nID, int nScore, int nSG, int nEnergy) {
    if (nFlag == DSpotterSDKHL::InitSuccess) {
        // Initialization successful
    } else if (nFlag == DSpotterSDKHL::GetResult) {
        switch(nID) {
            case 100:
                Serial.println("The Trigger was heard");
                break;
            case 10000:
                Serial.println("The Command -open- was heard");
                myservo1.write(15);
                myservo2.write(165);
                break; // Make sure to break here
            case 10001:
                Serial.println("The Command -close- was heard");
                myservo1.write(150);
                myservo2.write(30);
                break; // Make sure to break here
            case 10002:
                Serial.println("The Command -share- was heard");
                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) {
            // Handle license failure
        }
        g_oDSpotterSDKHL.Release();
        while(1); // Hang loop
    } else if (nFlag == DSpotterSDKHL::LostRecordFrame) {
        // Handle lost record frame
    }
}

void setup() {
    // Init LED control
    LED_Init_All();

    // Init Serial output for debug info
    Serial.begin(9600);
    while (!Serial);
    DSpotterSDKHL::ShowDebugInfo(true);

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

    if (!IMU.begin()) {
        Serial.println("Failed to initialize the IMU!");
        while (1);
    }

    // Attach servos here
    myservo1.attach(10);
    myservo2.attach(9);
}

void Doubletap() {
    // Check if cooldown is active
    if (millis() - lastActionTime < cooldownTime) {
        return; // Ignore new input during cooldown
    }

    // Check if a tap is detected
    if (detectTap()) {
        unsigned long currentTime = millis();

        // Check if enough time has passed since the last tap
        if (currentTime - lastTapTime < doubleTapTime) {
            tapCount++;
        } else {
            tapCount = 1; // Reset count for a new tap sequence
        }

        lastTapTime = currentTime;

        // Check for double-tap
        if (tapCount == 2) {
            performAction(); // Call the function on double tap
            lastActionTime = millis(); // Set the last action time
            tapCount = 0; // Reset the count after the action
        }
    }
}

// Function to detect a tap using Z-axis accelerometer data
bool detectTap() {
    float x, y, z; // Declare variables to hold acceleration data
    if (IMU.accelerationAvailable()) {
        IMU.readAcceleration(x, y, z); // Read X, Y, Z axes

        // Check for a significant change in Z-axis acceleration
        if (abs(z) > 1) {
            delay(50); // Simple debounce delay
            return true; // Tap detected
        }
    }
    return false; // No tap detected
}

// Function to perform on double-tap
void performAction() {
    Serial.println("Double tap detected! Performing action...");
    
    if (servoposition()) {
        myservo1.write(15);
        myservo2.write(165);
        delay(1000);
    } else {
        myservo1.write(150);
        myservo2.write(30);
        delay(1000);
    }
}

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

Think about what this line does.

2 Likes

Does that not just start the debugging info for when it is plugged into the serial monitor? Or is it just bricking my whole code in the setup phase when it isn’t plugged in to the serial monitor?

Google?

Arduino reference?

Thank you! I finally got some time to work on the project and it works while connected to my power bank, thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.