Trouble with Loops

Hello,

I have scoured the internet looking for a particular loop program. I probably have seen it, but just not in an example I can related to.

I would like to have my program start with a sound, and then loop into another sound from that point on. The initial sound, Darth Vader’s helmet sealing (about 3 to 4 seconds), and then his breathing (4 1/2 seconds intervals) thereafter until I turn it off.

Can anybody please help me with this?

I am using an Arduino Pro Mini 168 & 328 5v and have the following sketch to work with; (I know I can trim it down a bit!)

THANK YOU!

//DARTH VADER HELMET SEAL SOUND AND BREATHING EFFECTS
//1 TIME SEAL AND THEN HIS BREATHING EVERY 4 SECONDS

int outputSeal = 10;
int outputBreathing = 11;

void setup()
{
pinMode(outputSeal, OUTPUT);
pinMode(outputBreathing, OUTPUT);
}
// PROGRAM LOOP
void loop()
{
digitalWrite(outputSeal, HIGH);
digitalWrite(outputBreathing, LOW);
delay(4000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(4000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);

digitalWrite(outputSeal, LOW);
digitalWrite(outputBreathing, HIGH);
delay(3000);
}

You could simply put the helmet closing at the end of "setup()"

Setup is simply code that runs before the "loop()" code runs. There are no restrictions.

If the pin state not change, you don't need to repeat a digitalWrite command.

Is this using DFPlayerMini? Why do you have two pins for playing audio?

If the audio playback device is a DFPlayerMini, all you need to start a track is a momentary grounding of the DFPlayerMini "NEXT" pin (a long press on "NEXT" pin will decrease the volume). So I will guess a LOW on "10" or "11" will start the audio. You can increase the volume with a long press on "PREVIOUS" pin

//DARTH VADER HELMET SEAL SOUND AND BREATHING EFFECTS
//1 TIME SEAL AND THEN HIS BREATHING EVERY 4 SECONDS

int outputSeal = 10;
int outputBreathing = 11;

void setup() {
  pinMode(outputSeal, OUTPUT);
  pinMode(outputBreathing, OUTPUT);

  delay(2000); // delay the start by two seconds
  digitalWrite(outputSeal, LOW); // if DFPlayerMini, grounding the "NEXT" pin starts the audio
  delay(50); // short press (a long press will decrease the volume)
  digitalWrite(outputSeal, HIGH);
  // delay(4000); // no need for a delay... let the audio play out
}

void loop() {
  digitalWrite(outputBreathing, LOW);
  delay(50);
  digitalWrite(outputBreathing, HIGH);
  delay(4000); // adjust this value to allow audio to play out before repeating
}

Thank you all for your help.

I tried several different variations of the sketch and I keep running into the same problem. So here is a better explanation of what I hope to achieve.

I would like this to run the DFPlayerMini first, as xfpd said. After that initial start, I want the board to go to a Vader sound effect module I have. I would have it run every 4 1/4 seconds like you hear in the Empire Strikes Back movie. It would be a great feature to my costume!

But here's my problem(s), I have uploaded many sketches to my Pro Mini's, both 326p & 186, 5v, 16MHz boards.

First, I'm not sure what "programmer" I should use. Such as AVR ISP, AVRISP mkII, Arduino as ISP, and so on. I also tried the 'RESET' button trick, but to no avail!

Second, every program I have uploaded have all had the same results, both lights turn on and only 'dim' every few seconds, and not alternate 4 seconds as put in the sketch. I am using a 9 volt battery on the 'raw' pin as the power source, and I am using red and green LED'S to simulate the sound equipment.

I know what I am doing with the LED's, resistors, capacitors, etc. as I have made dozens of flashing units for various Enterprises, and 1/25 scaled models in the past. It's just the board I am having problems with.

Also, I thought it was my soldering of the pins to the boards, but I tried it without solder, and it still runs into the same problem!

I am really new to this and was told these boards would really help me, but it's not looking to good for me right now.

Again, thank you for any help you can supply!

I am using a Nano and a DFPlayer mini in "standalone" mode. This link shows how I wired the DFPlayerMini...

I have my Nano ground to the DFPlayerMini ground, and Nano pin 10 connected to DFPlayerMini "Next" pin to simulate a button press. I uploaded two audio files, vader breathing and the seal (actually, I could not find the "seal" audio, so I am using Old Spice whistle).

The code does not use the wiring to play the audio. It takes pin 10 from HIGH to LOW for a brief period, then back HIGH signaling "NEXT" audio track. I loaded the audio files in reverse order so the sketch has to "skip" the first audio to get to the "seal" audio.

If this plays in the wrong order, press the reset button. I think the DFPlayerMini remembers "last track played"...

// promini https://learn.sparkfun.com/tutorials/using-the-arduino-pro-mini-33v/all

// 0001.mp3 vader
// 0002.mp3 seal

#define NEXT 10
byte gap = 55;

void setup() {
  // Serial.begin(9600);
  pinMode(NEXT, HIGH);  // HIGH = button not pressed

  // skip past vader breathing
  digitalWrite(NEXT, LOW);   // LOW = button pressed
  delay(gap);                // short press = next track
  digitalWrite(NEXT, HIGH);  // HIGH = button released

  delay(gap);  // pause between button presses

  // play mask seal
  digitalWrite(NEXT, LOW);   // LOW = button pressed
  delay(gap);                // short press = next track
  digitalWrite(NEXT, HIGH);  // HIGH = button released

  delay(3500);  // delay time for "seal" audio
}

void loop() {
  // Serial.println(1);

  // play vader
  digitalWrite(NEXT, LOW);   // LOW = button pressed
  delay(gap);                // short press = next track
  digitalWrite(NEXT, HIGH);  // HIGH = button released

  delay(4600);  // delay time for "vader" audio

  // skip past seal
  digitalWrite(NEXT, LOW);   // LOW = button pressed
  delay(gap);                // short press = next track
  digitalWrite(NEXT, HIGH);  // HIGH = button released

  delay(gap);  // pause between button presses
}

Just want to let all of you know that I found 2 mistakes that caused my problems.

First, being new to this I put a line that was suggested by a person before the loop. It wasn't until I studied it, is when I found that I turned an "input" to "low" before the delay! After finding that and fixing it, it started working and then it didn't. After moving the board around, I found out that my bread board was bad. So after fixing that, the programs worked!

I want to thank everyone for taking the time out to help me. I just got my DF Player Mini's in so I'm looking forward to working on that.

Again, thank you!
Dark_Lord_1

:+1:

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