Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

wondering how I can make multiple leds blink while playing this song using c++ T

ID: 655263 • Letter: W

Question


wondering how I can make multiple leds blink while playing this song using c++

This example uses a piezo speaker to play melodies. It sends a square wave of the appropriate frequency to the piezo, generating the corresponding tone. The calculation of the tones is made following the mathematical operation: timeHigh = period / 2 = 1 / (2 * toneFrequency) where the different tones are described as in the table: http:/www.arduino.cc/en/Tutor1a1/Melody int speakerPin = 9: int length = 15; // the number of notes char notes [] = infinity ccggaagffeeddc infinity; // a space represents a rest int beats [] = {1,1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300: void playTone(int tone, int duration) {for (long i = 0; i

Explanation / Answer


//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,13}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and
//so on to address an array use ledPins[0] this
//would equal 2 and ledPins[7] would equal 13
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void playTone(int tone,int duartion)
{
for(long i=0,j=0;i<duration * 1000L;i+=tone*2,j++)
digitalwrite(speakerPin,HIGH) ;
digitalWrite(ledPins[j%8], HIGH); //Turns on LED #0 (connected to pin 2 )
delayMicroseconds(tone);   
digitalwrite(speakerPin,LOW) ;
digitalWrite(ledPins[j%8], LOW);
}


void setup()
{

//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 8; i++){ //this is a loop and will repeat eight times
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
}