BB8 Lighting and sound help needed...Please

As you will soon see…I am in no way a program but I am interested in learning and also wanting to produce a great project. I am wanting to be able to push a button and have two things happen at once. I want the lights to blink in a preset fashion which are located in the Case lines and a sound file to play. Then I would like the lights to return to a set pattern. I have tried to combine two different sets of code and I am not having much luck getting the two things to happen at once. I did get the sounds to play and then the lights work but that is it. I have posted the code below and would really like some pointers on how to make this work. This is not my code and I have pulled it from several sources. The sound works prefect when and of the 9 buttons are pushed but none of the lights change when the buttons are pushed, I would like the lights to have a different patter for each of the 9 buttons. Please let me know what I can do to get this to work. Should I ditch this code and start over?

SJC-BB8_Dome_RGB_v1.ino (21.3 KB)

Tried to put this in code tags for you but it was over twice the char limit. Lot of typing there.

It looks like you lock your loop down in your sound routine until your sound is finished playing.

P.S. Where are you located?

-jim lee

That's what I was thinking. How would I not do that or how can I set the lights to start moving first and then play the sounds. After that it would go back to the normal lighting mode.

I am in North Alabama.

I want the lights to blink in a preset fashion which are located in the Case lines and a sound file to play. Then I would like the lights to return to a set pattern. I have tried to combine two different sets of code and I am not having much luck getting the two things to happen at once.

I’ve read this three times, and it still makes no sense. You have some functions that supposedly make the lights behave in the fashion(s) that you want. You need to refer to them.

“and then return to a set pattern” means NOTHING without defining what pattern you want it to return to, and under what conditions you want it to return to that pattern. Or what “return to a pattern” means.

I suspect that your coding difficulties are directly related to your inability to describe exactly what you want the Arduino to do.

  // Kick off a pattern
  Radar.SolidColorRadar(Radar.Color(255,0,0));
  Holo.RainbowCycle(25);
  Side.Fade(Side.Color(0,0,100), Side.Color(0,0,05), 100, 40, REVERSE);
  PSI.SoundPulse();

I can not see a relation between the comment and the code that follows. It looks to me like the code makes FOUR things happen, NOT a (single) thing, as the comment implies.

  if (Serial1.read() == ',') {

    while (Serial1.available() >= 6)  {
      for (i = 0; i < 7; i ++) {
        posarray[i] = Serial1.read(); //build array from incoming xbee data
      }
    }
  }
  
  val = Serial1.read(); // read it and store it in 'val'

If there is one byte of data to read, and it happens to be a comma, read 7 values if there are 6 or more to read.

There are three fundamental flaws in that snippet of code. First, you read even if there is no data to read. While you can get away with that because you base the next action on the result of that read being a specific character, it is still wrong.

Then, you assume that because you read a comma, there will be 6 or more bytes to read. That is a piss-poor assumption.

If, by some miracle, it happens to be true, you then read 7 of the 6 or more bytes. THAT is wrong.

But, suppose this IS your lucky day, and there are 20 bytes in the buffer. You read the comma, leaving 19.

Now, what do you want to do? Do you want to read 6 characters? Or 7? With the while statement, how many of the 19 will you actually read?

Having exhausted the serial buffer, reading 7 of the last 6 bytes, you then read AGAIN. Complete nonsense.

      case 'g':
        randomSeed(analogRead(0));

Two more problems here. First, analogRead() is NOT guaranteed to return any specific value, though it is possible that it will return 0 every time. Therefore, it is a poor choice for seeding the random number generator.

Second, the random number generated should be seeded ONCE, not every time you need a random number.

  sound = posarray[5]; //position 5 is the audio number for audio file to play..

Whet does this code and comment have to do with the code that follows it?

  //Determine which sound to play
  if (sound == 10) {
    playcomplete("1.WAV");
  }
  else if (sound == 1) {
    playcomplete("2.WAV");
  }

  else if (sound == 2) {
    playcomplete("3.WAV");
  }

Really? Why the hell wouldn’t you play n.wav, when the value is n?

  sound == 0; //play nothing next time through loop...

That is NOT what will happen! Using the equality operator there is most likely another mistake.

By the way, playcomplete() is a blocking function. That IS going to interfere with any plans you have to do two things at once.

And, it is completely unnecessary since all it does is call playfile() and then spin its wheels while wave.isplaying() is true.

You could, and should, call playfile() whenever you discover that wave.isplaying() returns false, if you want music to play continuously.

Though, to be honest, I do not understand what you do want.

What you want, I think, is very possible. But, it looks like your going to have to step back a bit and rethink exactly what you want. In english? Paper napkin sketches? white board? However you like to sort out things at your highest level.

Your algorithm, should have states telling what it's up to, right now. We are displaying "X". We are hearing "Y".

You code should consist of A) functions that either start or stop things, like light patterns or sounds. Start sound "Y" set state to SOUD_Y. B) Code that is called over and over than keeps your "things" happy. Feeding data to your sounds, changing lights.. C) Code that watches the user to see if the monkey hits a switch or wants something.

All these pieces need to keep track of (or at least know) what state you are in. Given the state you are in and the time, you should always know what to do.

First get this all working in your head. Then just transfer it to code.

Hope this helps.

-jim lee