Hi everyone. I want to do as my first project a kind of Xmas lights with music. I have a buzzer on pin 9 and a pair of leds on pin 3. The thing I want to do is to do the both things at the same time, I mean to play the music and let the leds do the fade effect at the same time, but I don’t know how to do it. Here is my code:
#include <toneAC.h>
//Declarando notas y tiempos
float TIm = 987.767, TImb = 932.328, LAm = 880.000, LAmb = 830.609, SOm = 783.991, FAms = 739.989, FAm = 698.456, MIm = 659.255, MImb = 622.254, REm = 587.330, DOms = 554.365, DOm = 523.251, TI = 493.883, TIb = 466.164, LA = 440.000, LAb = 415.305, SO = 391.995, FAs = 369.994, FA = 349.228, MI = 329.628, MIb = 311.127, RE = 293.665, DOs = 277.183, DO = 261.626, ti = 246.942, tib = 233.082, la = 220.000, lab = 207.652, so = 195.998, fas = 184.997, fa = 174.614, mi = 164.814, mib = 155.563, re = 146.832;
float R = 4, Bp = 3, B = 2, Np = 1.5, N = 1, C = 0.5, sC = 0.25;
//////////
//'Jingle Bells'
float jnotas [] = { //Notas
MI, MI, MI, MI, MI, MI, MI, SO, DO, RE, MI, FA, FA, FA, FA, FA, MI, MI, MI, MI, RE, RE, MI, RE, SO, MI, MI, MI, MI, MI, MI, MI, SO, DO, RE, MI, FA, FA, FA, FA, FA, MI, MI, MI, SO, SO, FA, RE, DO};
float jtiempos [] = { //Tiempos
N, N, B, N, N, B, N, N, N, N, R, N, N, N, N, N, N, N, N, N, N, N, N, B, B, N, N, B, N, N, B, N, N, N, N, R, N, N, N, N, N, N, N, N, N, N, N, N, R};
int jtotal = (tizeof(jnotas)/tizeof(jnotas[0]) - 1); //Cantidad total de notas (a partir del cero)
//////////
//'Santa Claus is coming to town'
float snotas [] = { //Notas
MI, FA, SO, SO, SO, LA, TI, DOm, DOm, MI, FA, SO, SO, SO, LA, SO, FA, FA, MI, SO, DO, MI, RE, FA, ti, DO, SO, MI, FA, SO, SO, SO, LA, TI, DOm, DOm, MI, FA, SO, SO, SO, LA, SO, FA, FA, MI, SO, DO, MI, RE, FA, ti, DO, DOm, REm, DOm, TI, DOm, LA, LA, DOm, REm, DOm, TI, DOm, LA, LA, REm, MIm, REm, DOms, REm, TI, TI, TI, DOm, REm, DOm, TI, LA, SO, SO, MI, FA, SO, SO, SO, LA, TI, DOm, DOm, MI, FA, SO, SO, SO, LA, SO, FA, FA, MI, SO, DO, MI, RE, FA, ti, DO};
float stiempos [] = { //Tiempos
C, C, N, Np, C, C, C, N, B, C, C, N, N, N, C, C, N, B, N, N, N, N, N, B, N, Bp, N, C, C, N, Np, C, C, C, N, B, C, C, N, N, N, C, C, N, B, N, N, N, N, N, B, N, Bp, N, N, N, N, N, N, B, N, N, N, N, N, N, B, N, N, N, N, N, N, B, N, N, N, N, N, N, B, N, C, C, N, Np, C, C, C, N, B, C, C, N, N, N, C, C, N, B, N, N, N, N, N, B, N, R};
int stotal = (tizeof(snotas)/tizeof(snotas[0]) - 1); //Cantidad total de notas (a partir del cero)
//////////
void setup(){}
void RepCanciones() {
//Reproducir 'Jingle Bells'
for(int j = 0; j<=jtotal; j++)
{
int jduracion = (jtiempos[j] * 1000) / 6;
toneAC(jnotas[j]);
delay(jduracion);
noToneAC();
delay(jduracion);
}
//////////
//Reproducir 'Santa Claus is coming to town'
for(int s = 0; s<=stotal; s++)
{
int sduracion = (stiempos[s] * 1000) / 6;
toneAC(snotas[s]);
delay(sduracion);
noToneAC();
delay(sduracion);
}
//////////
}
void Ledes(){
for(int counter = 1; counter<=4; counter++) {
for(int fade1 = 0; fade1<=100; fade1++) {
analogWrite(3, fade1);
delay(70);
}
for(int fade2 = 100; fade2>0; fade2--) {
analogWrite(3, fade2);
delay(70);
}
}
for(int titila = 1; titila<=8; titila++) {
digitalWrite(3, HIGH);
delay(70);
digitalWrite(3, LOW);
delay(70);
}
}
void loop(){
RepCanciones();
Ledes();
}
(There are some things in Spanish, that’s because I’m from Argentina).
Do you know how to do it?
Thanks in advance.