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);
}
}