Need help making lights and a servo move with audio

Hi i am new to arduino and i need help to the probblem i am having. I need to be able to when i push a button a quick audio file plays and at the same time the lights and servo motor move.
This is the code so far.

#include <ServoTimer2.h>
#include <SimpleSDAudio.h>
const int buttonPin = 1;
const int led2Pin = 7;
const int led3Pin = 6;
const int led4Pin = 5;
const int led5Pin = 4;
const int led6Pin = 3;
const int led7Pin = 2;
int buttonState = 0;
int servoPin = 8;
ServoTimer2 Servo1;

void setup() {

const int BUTTON_PIN = 1;
const int LED_1PIN = 7;
const int LED_2PIN = 6;
const int LED_3PIN = 5;
const int LED_4PIN = 4;
const int LED_5PIN = 3;
const int LED_6PIN = 2;
Servo1.attach(servoPin);
Serial.begin(9600);
pinMode(1,INPUT);
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
SdPlay.setSDCSPin(10);
if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))
{ while(1); }
if(!SdPlay.setFile("8.wav")){;}
}

void loop(){

buttonState = digitalRead(buttonPin);
delay (0);
if (buttonState == LOW) {

      SdPlay.play();

while(!SdPlay.isStopped()){;}

digitalWrite(7,LOW);
delay(20);
digitalWrite(7,HIGH);
delay(20);
digitalWrite(7,LOW);
delay(20);
digitalWrite(6,HIGH);
digitalWrite(5,LOW);
delay(20);
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
delay(20);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
delay(20);
digitalWrite(4,HIGH);
digitalWrite(3,LOW);
delay(20);
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
delay(20);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
delay(20);
digitalWrite(2,HIGH);
delay(20);
digitalWrite(2,LOW);
delay(20);
Servo1.write(1500);
delay(0);
Servo1.write(900);
delay(1500);
Servo1.write(1500);
delay(1000);

} else {

digitalWrite(7,LOW);
delay(200);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
delay(0);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
delay(0);
digitalWrite(2,LOW);
delay(0);}
Servo1.write(1500);
delay(0);
}

So what is the schematic of your circuit, hand drawn is fine Fritzing is not.

Please post your code according to the instructions in the link you got when first making your post.

You must give your servos time to reach the position you set a delay of zero will not cut it.

You have an awful lot of delays, never a good idea.

This is a photo of the circuit (i know it's a mess).


I also got rid of all the delays that where set to zero.
Any idea of how to get the lights and servos to go with the audio thought??

Limiting the scope of constants like that is almost never a good idea

Please remember to use code tags when posting code

Nope.

What does that mean???

It means you can only use those names inside the setup function, but you hardly ever use the names anyway, so modifying your code in the future (aka next week) is going to be more tricky.

Having two sets of names for the same pins is simply perverse.

Ok thanks, good to know for the future.
Do you know how to get the lights and servos to go at the same time as the audio file???

I'd probably go with a 2D array of times and angles if if doing a quick-and-dirty, but I don't have any idea what it is you want to achieve.

It might be but it is not a schematic and so does not tell us how you have wired it up.

What do you mean? After the file has started playing or get them to move in time to the music?

Is the file playing with the code you posted? I would be surprised if it were because you don't give the command to play a file name to use. It might just default to the first one it finds.
In order to get the audio to drive any effects you have to pipe it in from the output of the SD player.

Bet that made absoloutly no difference. What you should have done is to put a sensible value in them.

When I push the button I want the lights and audio file to go at the same time. Right now the lights go then the audio then the servo.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.