Trouble understanding code

Thread Starter

Killerbee65

Joined May 15, 2017
256
Hello all! I am currently having trouble with some code I found online. All it really does is play the r2d2 noises i need for a project (since I couldn't find it any other way) the problem is the code is hard to understand, I'm a amateur, and I want to know how to program it so I can use a button to activate each sound. I'll upload the code in a seperate comment since I dont have it available on this device.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00); 
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  }  
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
If someone finds any code that can replicate r2d2 noises please let me know, I am really short on time but any and all advice is welcomed!
 

Papabravo

Joined Feb 24, 2006
21,159
The function for R2D2 noises looks like Arduino code with two parameters, which are frequency and volume. Both sequences use 1050 for the frequency. For the time being lets assume that means 1050 Hz which on the piano scale is close to C6 or two octaves above middle C. A relative volume is chosen from the buzzVols array starting at index 9 and working down to 0. That output is held for 1.28 milliseconds = 20 * 64 μsecs. then we switch to a different volume level for twice as much time, 2.56 milliseconds, then we decrement the index and repeat the process. So we have 20 instances of a constant frequency output at different alternating volume levels. I'm guessing that the arithmetic on the value pulled from buzzVols array in the second case is going to be ((buzzVols/8)*4), but I could be wrong about that. The lack of parentheses could mean the expression is evaluated left to right, but I'm not certain about that.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Have you searched on R2D2 sounds? There are quite a few hits, a lot with just playback, but some more descriptive of how it was done. Like this one: https://flypaper.soundfly.com/produce/5-iconic-star-wars-sounds-demystified/

I suspect Lucas Studios/Disney won't give you the code. But maybe you can come up with something similar based on knowing how it was done.
I have, most sources just use a mp3 shield and since I am out of time and can't really afford that option. I'll give the link a try though thank you so much!
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
I am out of time because yes it is a school project for an engineering course and my teacher is also on crunch time but it's ok I dont mean to pressure anyone just a little insight.
Also the project was funded and mine is at the forefront of being shown off to the "higher-ups" in our school for future funding of the course.
 

Papabravo

Joined Feb 24, 2006
21,159
well I like to think all my projects are meaningful to me, but about the code what would be a good way to integrate a button to activate the noises?
Well of course they are, but I was talking about the audience.

In a loop in the main program look for a change on an I/O pin from either 0 to 1 or 1 to 0, do the noise and then go back to monitoring the pin.
You might wan to wait for the state to change back to the original state before you trigger a repeat.
 

jpanhalt

Joined Jan 18, 2008
11,087
Back to your question... Have you thought about the link I gave? Have you considered using a PRN (pseudo random number) as a seed for the sound?

Do you have anything besides cut and paste to show for your efforts?
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Back to your question... Have you thought about the link I gave? Have you considered using a PRN (pseudo random number) as a seed for the sound?

Do you have anything besides cut and paste to show for your efforts?
I have not considered prn because I did not know it was a thing before you showed me the link. As for what I have to show, other than research, this is the only thing I could find and incorporate into my project without the use of expensive equipment or the use of additional equipment.
 

BobaMosfet

Joined Jul 1, 2009
2,110
Code:
#include "Volume3.h"

#define speakerPin 9

void setup() {
  // put your setup code here, to run once:
  wolfWhistle();
  delay(1000);
  R2D2();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void gameboy(){
  vol.tone(speakerPin,1025,1023); // pa
  delay(70);
  uint16_t v = 1000;
  while(v > 0){
    vol.tone(speakerPin, 2090, v); // ting!
    delay(10);
    v-=10;
  }
}

void wolfWhistle() {
  int f = 122; // starting frequency
  int v = 0;   // starting volume
  while (f < 4000) {  // slide up to 4000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(1);
  }
  vol.noTone();
  delay(100); // wait a moment
  f = 122; // starting frequency
  v = 0;   // starting volume
  while (f < 3000) { // slide up to 3000Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f += 25;
    delay(2);
  }
  while (f > 125) { // slide down to 125Hz
    vol.tone(speakerPin, f, v);
    v = 1023 * (f / 4000.00);
    f -= 25;
    delay(2);
  }
  vol.noTone(); // end tone production
}

void R2D2() {
  int beeps[] = {1933, 2156, 1863, 1505, 1816, 1933, 1729, 2291};
  int buzzVols[] = {144, 180, 216, 252, 252, 252, 252, 216, 180, 144};

  int i = 9;
  while (i >= 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }

  delay(35);

  i = 0;
  while (i < 8) {
    int v = 0;
    while (v < 250) { // 12.5 mS fade up time
      vol.tone(speakerPin, beeps[i], v*4);
      v += 10;
      delayMicroseconds(2*64);
    }
    delay(20);
    v = 250;
    while (v > 0) { // 12.5 mS fade down time
      vol.tone(speakerPin, beeps[i], v*4);
      v -= 10;
      delayMicroseconds(5*64);
    }
    vol.noTone();
    delay(25);
    i++;
  }

  int f = 2466;
  while (f < 2825) {
    vol.tone(speakerPin, f, 1023);
    f += 3;
    delay(1);
  } 
  f = 2825;
  int v = 255;
  while (f > 2000) {
    vol.tone(speakerPin, f, v*4);
    f -= 6;
    v -= 1;
    delay(1);
  }
  vol.noTone();
  delay(35);

  i = 10;
  while (i > 0) {
    vol.tone(speakerPin, 1050, buzzVols[i]*4);
    delayMicroseconds(20*64);
    vol.tone(speakerPin, 1050, buzzVols[i] / 8*4);
    delayMicroseconds(40*64);
    i--;
  }
  vol.noTone();
}
Try flowcharting. This is what professionals do to understand code. The create a flow-chart showing it's logic. Then it becomes understandable.

Free, easy to use (very good) online flow-charting tool- you can save and print your flowcharts, too:

https://app.diagrams.net/
 

Deleted member 115935

Joined Dec 31, 1969
0
Its the old question, what do you know , need to know.

Its Arduino code,
Arduino has tow parts, the setup that runs once at start up, and the main that loops forever.

In your startup,

wolfWhistle();
delay(1000);
R2D2();
delay(1000);

Your running wolfWhistle, which Im guesisng make a wolf whistle noise,
then your waiting a second,
then your running R2D2, which I guess makes a R2D2 type sound
then you wait another second,
and thats it.

The main loop does nothing.

volume3 is an add on arduino library,
https://github.com/connornishijima/arduino-volume3

On the git hub for the library, you will find instrucitions on how to make all sorts of noises / sounds.

As for how to detect a press button,
again there are Aruino helps for that

many tutorials on the arduino web site, this is about the easiest
https://www.arduino.cc/en/tutorial/button
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Its the old question, what do you know , need to know.

Its Arduino code,
Arduino has tow parts, the setup that runs once at start up, and the main that loops forever.

In your startup,

wolfWhistle();
delay(1000);
R2D2();
delay(1000);

Your running wolfWhistle, which Im guesisng make a wolf whistle noise,
then your waiting a second,
then your running R2D2, which I guess makes a R2D2 type sound
then you wait another second,
and thats it.

The main loop does nothing.

volume3 is an add on arduino library,
https://github.com/connornishijima/arduino-volume3

On the git hub for the library, you will find instrucitions on how to make all sorts of noises / sounds.

As for how to detect a press button,
again there are Aruino helps for that

many tutorials on the arduino web site, this is about the easiest
https://www.arduino.cc/en/tutorial/button
I understand there are sources that show me how to program a button into the code but it is difficult for me for this specific code. I am not asking for a straight answer just a little guidance on the where and how to do it. I have worked on programs like playing sounds using buttons but they were much more understandable and even came with the button code.
 
Top