Book Image

Arduino Electronics Blueprints

Book Image

Arduino Electronics Blueprints

Overview of this book

Table of Contents (17 chapters)
Arduino Electronics Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding an LED bar graph display for selected sound


Besides displaying the names of the sound files on a serial monitor, a lighting indicator can be used for positive visual feedback. The idea behind this new feature is to provide a unique visual effect for the random function. The random patterns generated within the software can be seen using solid state indicators like LEDs. The visual random patterns using LEDs will look best when viewed in a darkroom. The Proof of Concept (POC) that is being presented in this section is more of a design challenge to take the knowledge obtained from the previous hardware and software changes for building a unique and visually appealing device.

To help develop the LED bar graph display POC, a possible breadboard layout is shown in the following figure:

As shown in the breadboard, five single LEDs along with 330 ohm series resistors are used to monitor the associate sound files stored on the SD card. The idea behind the breadboard wiring is to show that digital output pins are needed to operate (drive) the LEDs. The digital pins D3, D4, D5, D7, and D8 are used as output drivers for operating the five LEDs.

A circuit schematic diagram shows a better wiring perspective of the LEDs, series resistors, and their attachment to the Arduino:

Although the wiring diagram shows red LEDs, other colors maybe used for the random function visual effect of the code. Also, the five LEDs can be replaced with an LED bar graph display, such as Sparkfun Electronics' catalog number COM-09937. Although the LED bar graph display has 10 individual LEDs in one package, only five are required for this project.

Once the circuit breadboard wiring is complete, a new code is required to operate the LED bar graph display. The following code blocks are required for the Random Function WAV sketch. These example code blocks are templates to help modify the sound effects machine for visual display of the sounds randomly being selected by the code. The digital pins shown in the code will need to be declared, as shown in the code block; the constant names chosen reflect the optoelectronic component wired to the designated digital pins on the Arduino:

int LEDBar1 = 3; // segment 1 of the LED Bar Graph display
int LEDBar2 = 4; // segment 2 of the LED Bar Graph display
int LEDBar3 = 5; // segment 3 of the LED Bar Graph display
int LEDBar4 = 7; // segment 4 of the LED Bar Graph display
int LEDBar5 = 8; // segment 5 of the LED Bar Graph display

Note that the declaration of constant names and variables are placed above the void setup() section of the code. Next, the constant names need to be configured as digital outputs for the Arduino:

void setup(){
 pinMode(LEDBar1,OUTPUT);  //Define LEDBar1 as digital output.
 pinMode(LEDBar2,OUTPUT);  //Define LEDBar2 as digital output
 pinMode(LEDBar3,OUTPUT);  //Define LEDBar3 as digital output
 pinMode(LEDBar4,OUTPUT);  //Define LEDBar4 as digital output
 pinMode(LEDBar5,OUTPUT);  // Define LEDBar5 as digital output
}

The final step of adding the LED bar graph display to the random function WAV code is to operate the LEDs when the assigned sound file is selected:

void loop(){
  randNumber = random(5); // set max random number

  if (randNumber == 5){ // if number is 5 play file "6.wav"
    audio.play("6.wav");
    Serial.println("Playing 6.wav");
    digitalWrite(LEDBar1, HIGH);
    delay(5000);

  } else if(randNumber == 4){ // if number is 4 play file "4.wav"
    audio.play("4.wav");
    Serial.println("Playing 4.wav");
    digitalWrite(LEDBar1, LOW);
    digitalWrite(LEDBar2, HIGH);
    delay(5000);

  } else if(randNumber == 3){ // if number is 3 play file "5.wav"
    audio.play("5.wav");
    Serial.println("Playing 5.wav");
    digitalWrite(LEDBar2, LOW);
    digitalWrite(LEDBar3, HIGH);
    delay(5000);

  } else if(randNumber == 2){ // if number is 2 play file "3.wav"
    audio.play("3.wav");
    Serial.println("Playing 3.wav");
    digitalWrite(LEDBar3, LOW);
    digitalWrite(LEDBar4, HIGH);
    delay(5000);

  } else if (randNumber == 0){ // if number is 0 play "1.wav"
    audio.play("1.wav");
    Serial.println("Playing 1.wav");
    digitalWrite(LEDBar4, LOW);
    digitalWrite(LEDBar5, HIGH);
    delay(5000);

  } else if (randNumber == 1){ // if number is 1 play "2.wav"
    audio.play("2.wav");
    Serial.println("Playing 2.wav");
    digitalWrite(LEDBar5, LOW);
    delay(5000);
  }

}

With these suggested changes to the Random Function WAV code, the sound effects machine will have a visually appealing look as WAV files are being played randomly.