Go Down

Topic: Arduino Lightsaber (Read 147887 times) previous topic - next topic

danboe70

Put the MPU on some jumper wires.


(this board is set up for pixel, rgb, and string blade development)
thats how I have it at the moment. once I start including my delays things stop detecting a clash or swing

jbkuma

If your in constant delays you can't detect things.. you need to compare times instead.

start humming, set humStart to millis().
if (millis() - humStart) > humTime, start humming again.

You cause the same to set an isHumming flag to decide if something else should happen.

your interrupting effects can use delays as long as you don't want them interrupted, then execute the the hum at the end of the delay.  Alternatively do the same thing and/or set isSwinging, isClashing flags

danboe70

#1247
May 25, 2017, 09:21 pm Last Edit: May 27, 2017, 08:46 am by danboe70
If your in constant delays you can't detect things.. you need to compare times instead.

start humming, set humStart to millis().
if (millis() - humStart) > humTime, start humming again.

You cause the same to set an isHumming flag to decide if something else should happen.

your interrupting effects can use delays as long as you don't want them interrupted, then execute the the hum at the end of the delay.  Alternatively do the same thing and/or set isSwinging, isClashing flags
According to your post I have changed now my code juts to get somthing running. My routine for the saberOn() looks now similar to your idea and similar to jakeS0ft's  example.

Code: [Select]

/* code removed for readability */


This code increased the functionality a lot. Thanks for the ideas.
However, I am facing now the problem, that 2-3 swings and clashes are detected and then nothing works. The arduino seems to freece. Just pressing the resetbutton helped.
Any ideas? I am realy sorry for my noobish questioning.



[EDIT] 27-05-2017

I have adapted my code and played with the times. It seems to work now perfectly as I wanted it to be from the beginning. However the freecing of the entire Arduino stays.
I experience this freece as follows:
- Boot sequence completes: I have the sound and all the other components initialized
- I press my on button and the saber turns on. Light works, Flicker works, even the hum sound works.
- I fire some clash and swing events. After a period of time, no swing or clash is detected. The Flicker stops working. The LED, however, mantains turned on at the last DAC value. Sometimes even the reset button does not solve this fault state. I was not capable to recreate this situation. It happens randomly and I have no clue where to start searching.


I´ll append my code in severals sections for overview:
Code: [Select]

#define PLAY_DURATION           100
#define SOUND_DELAY_TIME        100
#define SWING_SUPPRESS_TIME     600
#define CLASH_SUPPRESS_TIME     200
#define SWING_DEBOUCE_INTERVAL  350

#define HUM_SUPRESS_TIME        300
#define HUM_DELAY               60


Code: [Select]

/**
 *      @brief    This function is executed when the saber is ON. Random Values for the glow effect are executed
 */
void saberIsOn() {
  unsigned long lClashTimeDelta;
  unsigned long lSwingTimeDelta;
  unsigned long lHumTimeDelta;
  
  /* apply flicker to the blade */
  apBlade->ApplyFlicker(2);

  if(millis() - gMotionUpdateTime > 10){
    
    apMotion->Update();
    gMotionUpdateTime = millis();  

    isClash();

    isSwing();
  }
  else
  {
    isHum();
  }
}



Code: [Select]

void isClash()
{
  unsigned long lClashTimeDelta;
  /* CLASH MOMENT */
  if(apMotion->IsClash()) {
    #ifdef SPI_PLAYER
      tmrpcm.play("lsabCl.wav");
    #endif
    lClashTimeDelta = millis() - gLastClashTime;
    if(lClashTimeDelta > CLASH_SUPPRESS_TIME)
    {
      /* Record the time of the clash event */
      gLastClashTime = millis();

      #ifdef DFP_PLAYER
        gpSoundPlayer->PlayFile(2);
        delay(SOUND_DELAY_TIME);
      #endif
    }
  }
}


Code: [Select]

void isSwing()
{
  unsigned long lClashTimeDelta;
  unsigned long lSwingTimeDelta;
  /* SWING TIME */
  if(apMotion->IsSwing()) {  
    #ifdef SPI_PLAYER
      tmrpcm.play("lsabSw.wav");
    #endif
    lClashTimeDelta = millis() - gLastClashTime;
    lSwingTimeDelta = millis() - gLastSwingTime;
    if(lClashTimeDelta > SWING_SUPPRESS_TIME && lSwingTimeDelta > SWING_DEBOUCE_INTERVAL)
    {
      gLastSwingTime = millis();
      #ifdef DFP_PLAYER
        gpSoundPlayer->PlayFile(7);
        delay(SOUND_DELAY_TIME);
      #endif
    }
  }
}


Code: [Select]


void isHum()
{
  unsigned long lClashTimeDelta;
  unsigned long lSwingTimeDelta;
  unsigned long lHumTimeDelta;
  /* motion is no Swing and no clash, therefore hum the dum*/
    #ifdef SPI_PLAYER
      if(!tmrpcm.isPlaying()) {
        tmrpcm.play("lsabHu.wav");
      }
    #endif
    #ifdef DFP_PLAYER
      lClashTimeDelta = millis() - gLastClashTime;
      lSwingTimeDelta = millis() - gLastSwingTime;
      lHumTimeDelta   = millis() - gLastHumTime;
      if(lHumTimeDelta > HUM_SUPRESS_TIME && lClashTimeDelta > SWING_SUPPRESS_TIME && lSwingTimeDelta > SWING_DEBOUCE_INTERVAL)
      {
        gLastHumTime = millis();
        gpSoundPlayer->PlayFile(6);
        delay(HUM_DELAY);
      }
    #endif
}

JakeSoft

According to your post I have changed now my code juts to get somthing running. My routine for the saberOn() looks now similar to your idea and similar to jakeS0ft's  example.
 ...

One thing I notice is that if you were to use the DIYinoSoundPlayer object as intended you wouldn't need to keep track of restarting your hum sound. If you just call this:

Code: [Select]

gpSoundPlayer->PlaySound(eeHumSnd, 0);


The hum sound will loop forever after one call instead of you having to relaunch it. This only works if you parameterized the sound player correctly, of course, which hopefully you have done in your main .ino sketch file which you aren't showing.

Calling gpSoundPlayer->PlayFile() is a kind of a hack I left as a public method mainly for testing and debugging purposes. Really, it should be a private method and nobody should be using it in their sketch.

danboe70

#1249
May 28, 2017, 10:16 am Last Edit: May 28, 2017, 10:21 am by danboe70
One thing I notice is that if you were to use the DIYinoSoundPlayer object as intended you wouldn't need to keep track of restarting your hum sound. If you just call this:

Code: [Select]

gpSoundPlayer->PlaySound(eeHumSnd, 0);


The hum sound will loop forever after one call instead of you having to relaunch it. This only works if you parameterized the sound player correctly, of course, which hopefully you have done in your main .ino sketch file which you aren't showing.

Calling gpSoundPlayer->PlayFile() is a kind of a hack I left as a public method mainly for testing and debugging purposes. Really, it should be a private method and nobody should be using it in their sketch.
Ok....well I must admit I have no clue how to configure the soundPlayer object. I have taken your example looked over it and just adapted very little.

Code: [Select]

    memset(&gSoundMap, 0, sizeof(DIYinoSoundMap));
    gSoundMap.Features.FontIdsPerFont = 0;
    gSoundMap.Features.HumSoundsPerFont = 1;
    gSoundMap.Features.PowerUpSoundsPerFont = 1;
    gSoundMap.Features.PowerDownSoundsPerFont = 1;
    gSoundMap.Features.ClashSoundsPerFont = 1;
    gSoundMap.Features.SwingSoundsPerFont = 1;
    gSoundMap.Features.LockupSoundsPerFont = 0;
    gSoundMap.Features.BlasterSoundsPerFont = 0;
    gSoundMap.Features.ForceSoundsPerFont = 0;
    gSoundMap.Features.CustomSoundsPerFont = 0;
    gSoundMap.Features.MenuSounds = 0;
  
    //Sound locations
    gSoundMap.Locations.BaseAddr = 0;
    gSoundMap.Locations.BlasterBase = 35;
    gSoundMap.Locations.BootBase = 1;
    gSoundMap.Locations.ClashBase = 2;
    gSoundMap.Locations.SwingBase = 7;
    gSoundMap.Locations.LockupBase = 0;
    gSoundMap.Locations.PowerupBase = 5;
    gSoundMap.Locations.PowerdownBase = 4;
    gSoundMap.Locations.HumBase = 6;
    gSoundMap.Locations.FontIdBase = 0;
    gSoundMap.Locations.CustomBase = 39;
    gSoundMap.Locations.MenuBase = 1;


My SD-Card has the following structure:
- /mp3/0001_bootsnd.wav (boot sound)
- /mp3/0002_lsabCl.wav (clash sound)
- /mp3/0003_lsabHu.wav (long hum sound - not used)
- /mp3/0004_lsabOff.wav (power off sound)
- /mp3/0005_lsabOn.wav (power on sound)
- /mp3/0006_.wav (used hum sound)
- /mp3/0007_lsabSw.wav (swing sound)

I have read somewhere, it was necessary to copy the files one by one.
On the other hand, these base-values seem to bee physical adresses inside the FLASH. Am I correct?
Now my question is: How to I get those adresses to insert them correctly into the soundPlayer config?

JakeSoft

I have read somewhere, it was necessary to copy the files one by one.
On the other hand, these base-values seem to bee physical adresses inside the FLASH. Am I correct?
Now my question is: How to I get those adresses to insert them correctly into the soundPlayer config?
The physical address is the order in which the files were copied to the SD card or FLASH. You are already using the right addresses when you are calling the PlayFile() method. Your example code looks correct at first glance assuming the numbers on your files equate to their order on the SD card.

danboe70

The physical address is the order in which the files were copied to the SD card or FLASH. You are already using the right addresses when you are calling the PlayFile() method. Your example code looks correct at first glance assuming the numbers on your files equate to their order on the SD card.
The call
Code: [Select]

gpSoundPlayer->PlaySound(eeBootSnd, 0);

Ok. This sequence now works.
Am I corrrect to asume, that I have to call only once this line for the hum-Sound and this Hum is just interupted by the other sounds?

JakeSoft

The call
Code: [Select]

gpSoundPlayer->PlaySound(eeBootSnd, 0);

Ok. This sequence now works.
Am I corrrect to asume, that I have to call only once this line for the hum-Sound and this Hum is just interupted by the other sounds?

Yes. That call should interrupt whatever sound is playing and then play the hum sound in a loop forever or until you make another call to play a different sound.

danboe70

#1253
Jun 01, 2017, 12:29 pm Last Edit: Jun 02, 2017, 09:17 am by danboe70
Yes. That call should interrupt whatever sound is playing and then play the hum sound in a loop forever or until you make another call to play a different sound.
Thank you for your help. Things start to work now pretty well. I have the hum-sound started inmediatly after the turnSaberOn-sound....this is perfectly interrupted by the clash sound. Once completed the clash sound, the hum is started again.
Nevertheless, I run into troubles when inserting the detection of a swing. Everything gets pretty slow and at a undefined point everything hangs up.
I have uploaded my entire project on my git-repo. Link: https://github.com/danboe90/ArduSaber

Please feel free to point out everything you would change to improve functionality.


[EDIT] 02-06-2017
As I realy wasn´t shure if I was doing things right, I tried your FX-Saber OS on my setup.
I changed the sound-config to meet my setup. Everything works except for the following things:
1. I have no hum-sound
2. the hang up still ocurres. I am questioning now, if it has to do with the power supply. Is it possible that the soundmodule pulls to much current and causes the arduino to hang up? I used some 200µF Condensators to stabilize the voltage, but this did not improve the situation. Hang up still occures.

Protonerd

[EDIT] 02-06-2017
As I realy wasn´t shure if I was doing things right, I tried your FX-Saber OS on my setup.
I changed the sound-config to meet my setup. Everything works except for the following things:
1. I have no hum-sound
2. the hang up still ocurres. I am questioning now, if it has to do with the power supply. Is it possible that the soundmodule pulls to much current and causes the arduino to hang up? I used some 200µF Condensators to stabilize the voltage, but this did not improve the situation. Hang up still occures.
:) FX-SaberOS is still not officially released. Maybe for the time being you can still use LightSaberOS (aka LSOS). Although that Git does not have support for HP-LEDs.

Anyway, I expect that FX-SaberOS will be launched soon, so stay tuned.

jbkuma

Are you connecting the INT from the MPU? Some early diagrams from LSOS included this, but it is not necessary and it can create odd behavior if not used properly. In particular I have experienced similar hang issues.

Go Up