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++
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
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.