0

Hi, I have been working to produce tones on a speaker and it works with the following code

    int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3, NOTE_B3, NOTE_C4};
    ...
    ...
    tone(pin, melody[pitch]);

After I included my addressable LED strip sketch codes, the speaker does not produce the same tone as normally it does. It sounded distorted this time round.

void loop()
{
  strip.Update(); //NeoPatterns strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete);
}

Class:

// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
  public:

// Member Variables:
    ...
    ...

void (*OnComplete)();  // Callback on completion of pattern

// Constructor - calls base-class constructor to initialize strip
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
  : Adafruit_NeoPixel(pixels, pin, type)
{
  OnComplete = callback;
}

// Update the pattern
void Update()
{
  if ((millis() - lastUpdate) > Interval) // time to update
  {
    lastUpdate = millis();
    switch (ActivePattern)
    {
      case RAINBOW_CYCLE:
        RainbowCycleUpdate();
        break;
      default:
        break;
    }
  }
}

// Initialize for a RainbowCycle
void RainbowCycle(uint8_t interval, direction dir = FORWARD)
{
  ActivePattern = RAINBOW_CYCLE;
  Interval = interval;
  TotalSteps = 255;
  Index = 0;
  Direction = dir;
}

// Update the Rainbow Cycle Pattern
    void RainbowCycleUpdate()
    {
      for (int i = 0; i < numPixels(); i++)
      {
        setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
      }
      show(); // When I comment this line, the speaker will work properly. What is happening?
      Increment();
    }

1 Answer 1

0

The tone() command is interrupt driven.

To get the tight timing needed by the LED strips interrupts are disabled while sending the data.

That means the tone() command stops while you're updating the LED strips and starts again afterwards.

The end result will be the sound is corrupted - maybe sounding like it's bubbly or underwater or just "rough".

The two basically can't work together and I'm not sure what to suggest to make them work together on a low-end MCU like the AVR.

4
  • What about using this library? forum.arduino.cc/index.php?topic=143940.0
    – Zainau
    Commented Sep 4, 2015 at 14:43
  • or are there any possible workaround to play a sound together with the LED strip? My sketch will consist of a sensor,a servo and a LED strip.
    – Zainau
    Commented Sep 4, 2015 at 14:44
  • That one also uses interrupts.
    – Majenko
    Commented Sep 4, 2015 at 14:44
  • Either only update the led strip in between tones, or build your own tone function that changes the PWM frequency on one of the PWM pins (probably 9 or 10, since timer1 would be the most useful for that).
    – Gerben
    Commented Sep 4, 2015 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.