Sony's Developer World forum

    • Home
    • Forum guidelines

    Upcoming maintenance 

     

    Dear Customers and Partners.

    This website will be undergoing scheduled maintenance on June 14, 2023. Please be aware there may be disruption to the developer portal website and associated services during the scheduled maintenance period.

    This upgrade is essential to ensure the continued performance, reliability, and security of Developer World.

    We apologize for any inconvenience.

     

     

    Can't use a while loop after an if statement?

    Spresense
    2
    2
    1201
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      theanton205 last edited by

      As weird as this sounds, my spresense terminates the program in Arduino after an if statement is met. Below is my code, where it waits until a value from my accelerometer is met to then play audio. However, in order to play this audio, we need to writeFrames in a loop. So, I used a while loop to do so. This while loop, defined in the function below as "plays()" works outside of the if statement. I'm at a loss, any thoughts?

      #include <Adafruit_MPU6050.h>
      #include <Adafruit_Sensor.h>
      #include <Wire.h>
      #include <SDHCI.h>
      #include <Audio.h>
      
      Adafruit_MPU6050 mpu;
      
      int x = 0;
      int y = 0;
      int z = 0;
      
      SDClass theSD;
      AudioClass *theAudio;
      
      File myFile;
      bool go = true;
      bool ErrEnd = false;
      int counter = 0;
      
      static void audio_attention_cb(const ErrorAttentionParam *atprm)
      {
        puts("Attention!");
        
        if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
          {
            ErrEnd = true;
         }
      }
      
      void plays() {
        while (go) {
          int err = theAudio->writeFrames(AudioClass::Player0, myFile);
          if (err == AUDIOLIB_ECODE_FILEEND) {
            Serial.println("File ended!\n");
            go = false;
          }
        }
      }
      
      void setup(void) {
        Serial.begin(115200);
      
        // Try to initialize!
        if (!mpu.begin()) {
          Serial.println("Failed to find MPU6050 chip");
          while (1) {
            delay(10);
          }
        }
        Serial.println("MPU6050 Found!");
        mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
        mpu.setGyroRange(MPU6050_RANGE_500_DEG);
        mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
      
        delay(100);
      
        while (!theSD.begin())
          {
            Serial.println("Insert SD card.");
          }
      
        // start audio system
        theAudio = AudioClass::getInstance();
      
        theAudio->begin(audio_attention_cb);
      
        puts("initialization Audio Library");
      
        theAudio->setRenderingClockMode(AS_CLKMODE_NORMAL);
        theAudio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
      
        err_t err = theAudio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_MONO);
      
        if (err != AUDIOLIB_ECODE_OK)
          {
            printf("Player0 initialize error\n");
            exit(1);
          }
      
        myFile = theSD.open("ask_help.mp3");
      
        if (!myFile)
          {
            printf("File open error\n");
            exit(1);
          }
        printf("Open! 0x%08lx\n", (uint32_t)myFile);
      
        err = theAudio->writeFrames(AudioClass::Player0, myFile);
        theAudio->setVolume(120);
        theAudio->startPlayer(AudioClass::Player0);
      
      }
      
      void loop() {
      
        sensors_event_t a, g, temp;
        mpu.getEvent(&a, &g, &temp);
      
        x = g.gyro.x;
        y = g.gyro.y;
        z = g.gyro.z;
      
        // Serial.print("Rotation X: ");
        // Serial.print(x);
        // Serial.print(", Y: ");
        // Serial.print(y);
        // Serial.print(", Z: ");
        // Serial.print(z);
        // Serial.println(" rad/s");
      
        if(x >= 8 || x <= -8){
          Serial.println("plays!");
          //plays();
        }
          
        myFile.close();
        myFile = theSD.open("ask_help.mp3");
        go = true;
      }
      
      C 1 Reply Last reply Reply Quote
      • C
        CamilaSouza DeveloperWorld @theanton205 last edited by

        @theanton205

        Do you mean that if the loop function is like below then the program works?
        (plays() outside if statement)

        void loop() {

        sensors_event_t a, g, temp;
        mpu.getEvent(&a, &g, &temp);

        x = g.gyro.x;
        y = g.gyro.y;
        z = g.gyro.z;

        plays();

        myFile.close();
        myFile = theSD.open("ask_help.mp3");
        go = true;
        }

        1 Reply Last reply Reply Quote
        • First post
          Last post
        Developer World
        Copyright © 2021 Sony Group Corporation. All rights reserved.
        • Contact us
        • Legal