“The code below plays jingle bells on a mini speaker, I just need help ob how to
ID: 2293344 • Letter: #
Question
“The code below plays jingle bells on a mini speaker, I just need help ob how to sincronise the led so that the led plays along with jingle bells. Arduino Problem.”byte speaker=11;
int C = 1047; int D = 1175; int E = 1319; int F = 1397; int G = 1568; int A = 1760; int B = 1976;
int Quarter = 200; int Half = 450; int Whole=900; void setup() { pinMode(speaker, OUTPUT);
int ScaleNotes[] = {E, E, E, E, E, E, E, G, C, D , E}; int NoteLengths[] = {Quarter, Quarter, Half, Quarter, Quarter, Half, Quarter, Quarter, Quarter, Quarter, Whole}; for (int index = 0; index < 11; index++) { note(ScaleNotes[index], NoteLengths[index]); } } void loop() { } void note(int frequency, int NoteDuration) { tone(speaker, frequency); delay(NoteDuration); noTone(speaker); delay(50); }
gle Bells, AndLED. The music f e that flushes automaticall get away from the toilet be of this on Canvas nd add emati
Explanation / Answer
Please check, i have add led funtionality to the above code.
Code:
byte speaker=11;
byte led_pin= 13; // assign LEd pin a name
int C = 1047;
int D = 1175;
int E = 1319;
int F = 1397;
int G = 1568;
int A = 1760;
int B = 1976;
int Quarter = 200;
int Half = 450;
int Whole=900;
void setup()
{
pinMode(speaker, OUTPUT);
pinMode(led_pin, INPUT); // Intilize the led pin as input
int ScaleNotes[] = {E, E, E, E, E, E, E, G, C, D , E};
int NoteLengths[] = {Quarter, Quarter, Half, Quarter, Quarter, Half, Quarter, Quarter, Quarter, Quarter, Whole};
for (int index = 0; index < 11; index++)
{
note(ScaleNotes[index], NoteLengths[index]);
}
}
void loop()
{
}
void note(int frequency, int NoteDuration)
{
digitalWrite(led_pin, HIGH); // led on before palying tone
tone(speaker, frequency);
delay(NoteDuration);
digitalWrite(led_pin, LOW); // led off after palying tone
noTone(speaker);
delay(50);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.