LED +Servo misbehaving w/ Adafruit MP3 shield attached

Hello,

I’m new to coding with the Arduino, and have been running into an odd problem with a project I’m building. With one button push, I am trying to:

  • turn off one set of lights (white)
  • turn on one set of lights (red)
  • turn on a continuous rotation servo
  • turn on a traditional servo (starting at 30-degrees and rotating to 90-degrees and back)
  • start to play an mp3 via the Adafruit MP3 Shield

After a few seconds this should all stop and return to normal (white lights on, everything else off).

The shield appears to be functioning correctly and the song plays and stops as needed. The red light and continuous rotation servo also behave as expected. The trouble lies with the regular servo and the white lights.

When the button is pushed, the regular servo tries to rotate well beyond its limit, and each time the board is reset, it rotates what appears to be another 30 degrees. When the mp3 shield was not in the loop, it behaved as expected (started at its 30-degree mark and then rotated to 90, etc).

The white lights actually begin to flicker instead of turn off completely when the button is pushed. When the mp3 shield isn’t involved, they turn off as expected.

Other deets:

  • The lights are connected to everything via a relay and have their own external power supply. The Arduino is turning the relay switches on and off. The white lights are wired so that when no power is supplied by the Arduino to the relay, the lights are on. Red is set up the opposite.
  • Servo with issues is a Hitec HS-322HD Deluxe

Any help you can send my way would be greatly appreciated. I’m working on a deadline, and can’t seem to fish out what’s happening here.

Here’s the code I’m working with so far:

//Libraries to include
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Servo.h>

//assign servos
Servo continuousServo; 
Servo regServo;

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)

#define SHIELD_RESET  -1
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  Adafruit_VS1053_FilePlayer(SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

const int whiteLightPin = 12;  
const int redLightPin = 5;

int buttonValue = 0;

void setup() {
    (! musicPlayer.begin());
    SD.begin(CARDCS);
    musicPlayer.setVolume(8,8);
    musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

    //set up button
    pinMode(A0, INPUT);

    //set up lights
    pinMode(redLightPin, OUTPUT);  // Set Pin connected to red lights as an OUTPUT
    pinMode(whiteLightPin, OUTPUT);
    digitalWrite(redLightPin, HIGH);  // Set Pin turn red lights OFF
    digitalWrite(whiteLightPin, HIGH);

    //set up servos
    continuousServo.attach(13);
    regServo.attach(11);
    regServo.write(30);
    continuousServo.write(94);
    
  }
  
void loop() {
  
  buttonValue = digitalRead(A0);
  
  if (buttonValue == HIGH) {
    
    musicPlayer.startPlayingFile("1.mp3");
    delay(6050);
    
    digitalWrite(redLightPin, LOW);
    digitalWrite(whiteLightPin, LOW);
    continuousServo.write(91);
    regServo.write(90);
    delay(10000);
    
    musicPlayer.stopPlaying();

    buttonValue = 1 - buttonValue;
    digitalWrite(redLightPin, HIGH);
    digitalWrite(whiteLightPin, HIGH);
    continuousServo.write(94);
    regServo.write(30);
    
  }

}

Hi and welcome.

Post a schematic please. Hand drawn is ok as long as it is neat enough to read. Also details of power supply to the circuit. Are you powering all these leds and 2 servos from the 5V output of the Arduino for example?

Paul

Hi Paul,

Thanks for replying. The Arduino is only powering the button and the relay. Everything else (leds & servos) has its own battery supply. Working on a sketch now.

I have never drawn a schematic before, so please excuse my crude first attempt at one of these. See drawing attached below.

Few more details:

S = servo

CRS = continuous servo

LED lights came with their own battery packs that I have spliced in the relay

I wasn’t sure how to represent that I currently have the Arduino powering the button and the relay switch board. Hopefully I did okay.

I have gotten everything to work except the continuous servo and music now. Now the continuous servo and music turn on when the button is pushed and won’t turn off. I also don’t appear to be able to adjust the speed of the continuous servo at all.Any help would be GREATLY appreciated. Here’s the code at this point:

//Libraries to include
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Servo.h>

//assign servos
Servo continuousServo; 
Servo regServo;

#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)

#define SHIELD_RESET  -1
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  Adafruit_VS1053_FilePlayer(SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

const int redLightPin = 5;

int buttonValue = 0;

void setup() {
    (! musicPlayer.begin());
    SD.begin(CARDCS);
    musicPlayer.setVolume(8,8);
    musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

    //set up button
    pinMode(A0, INPUT);

    //set up lights
    pinMode(redLightPin, OUTPUT);  // Set Pin connected to red lights as an OUTPUT
    digitalWrite(redLightPin, HIGH);  // Set Pin turn red lights OFF
    pinMode(A3, OUTPUT); // Set pin for white lights
    digitalWrite(A3, HIGH); // turn white lights off

    //set up servos
    pinMode(A1, OUTPUT);
    pinMode(A2, OUTPUT);
    continuousServo.attach(A1);
    regServo.attach(A2);
    regServo.write(30);
    continuousServo.write(90);
    
  }
  
void loop() {
  
  buttonValue = digitalRead(A0);
  
  if (buttonValue == HIGH) {
    
    musicPlayer.startPlayingFile("1.mp3");
    delay(6050);
    
    digitalWrite(redLightPin, LOW);
    digitalWrite(A3, LOW);
    continuousServo.write(94);
    regServo.write(90);
    delay(10000);
    


    buttonValue = 1 - buttonValue;
    digitalWrite(redLightPin, HIGH);
    digitalWrite(A3, HIGH);
    continuousServo.write(90);
    regServo.write(30);
    
  }

  musicPlayer.stopPlaying();

}
    continuousServo.attach(A1);
    regServo.attach(A2);

Your diagram (not yet really a schematic, but not bad for a first attempt!) shows pins 11, 12 & 13, not A1 A2 & A3… please clarify.

Ack. Sorry about that. I changed the pins from the diagram to be as follows:

A0 - button A1 - continuous servo A2 - regular servo A3 - white lights 5 - red lights

When you say "won't turn off", do you mean when you release the button?

Your code looks like it will stop the servos, music and lights after around 16s.

That's what I'd like to happen (everything stop after 16 seconds), but that's not what it's doing.

After the 16 seconds, the music continues to play, and the continuous servo rotates at max speed.