Помогите зависает цикл

Нет ответов
Tvis
Offline
Зарегистрирован: 09.08.2015

я особо не шарю в програмировании так что ругать не стоит.. еще только учусь.

Делал пищалку на rtttl  дабы вот библиотека

Но использовал чуть другой код дабы не подлючать библиотеку (с ней пропадали некоторые звуки изза IRremote). не помню где нашел..

Теперь суть: хочу сделать с пульта несколько мелодий.. вроде немного поковырялся и получилось! но вот беда играет только 1 раз и потом наглухо стоит и не реагирует ни на чего совершенно.. использовал 2 кнопки пульта(ИК) первая кнопка играет первую мелодию (song), Вторая играет (song1).. да все работает но 1 раз после загрузки ардуино.. помогите что не так? Код прилогаю


// A fun sketch to demonstrate the use of the Tone library.
// By Brett Hagman
// bhagman@roguerobotics.com
// www.roguerobotics.com

// To mix the output of the signals to output to a small speaker (i.e. 8 Ohms or higher),
// simply use 1K Ohm resistors from each output pin and tie them together at the speaker.
// Don't forget to connect the other side of the speaker to ground!

// You can get more RTTTL (RingTone Text Transfer Language) songs from
// http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation
#include <IRremote.h>

#define OCTAVE_OFFSET 0
#define TONE_PIN 12

#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951

int notes[] = { 0,
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};

char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6";
char *song1 = "Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6,8c6,a.,8c6,8f6,8e6,8d6,8d#6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a#,8a,8f";







IRrecv irRessPin6(6);
decode_results irRessPin6Res;
int times1 = 500;
int irRessPin6OV = 0;
int irRessPin6TP = 0;
bool irRessPin6FFA25D = 0;
bool irRessPin6FFE21D = 0;
bool irRessPin6FF629D = 0;
bool irRessPin6FF22DD = 0;
bool irRessPin6FF02FD = 0;
bool irRessPin6FFC23D = 0;
bool irRessPin6FFE01F = 0;
bool irRessPin6FFA857 = 0;
bool irRessPin6FF906F = 0;
bool irRessPin6FF6897 = 0;
bool irRessPin6FF9867 = 0;
bool irRessPin6FFB04F = 0;
bool irRessPin6FF30CF = 0;
bool irRessPin6FF18E7 = 0;
bool irRessPin6FF7A85 = 0;



void setup()
{
 Serial.begin(9600);
 irRessPin6.enableIRIn();
}

#define isdigit(n) (n >= '0' && n <= '9')

void play_rtttl(char *p)
{
 // Absolutely no error checking in here

 byte default_dur = 4;
 byte default_oct = 6;
 int bpm = 63;
 int num;
 long wholenote;
 long duration;
 byte note;
 byte scale;

 // format: d=N,o=N,b=NNN:
 // find the start (skip name, etc)

 while(*p != ':') p++;    // ignore name
 p++;                     // skip ':'

 // get default duration
 if(*p == 'd')
 {
   p++; p++;              // skip "d="
   num = 0;
   while(isdigit(*p))
   {
     num = (num * 10) + (*p++ - '0');
   }
   if(num > 0) default_dur = num;
   p++;                   // skip comma
 }

 

 // get default octave
 if(*p == 'o')
 {
   p++; p++;              // skip "o="
   num = *p++ - '0';
   if(num >= 3 && num <=7) default_oct = num;
   p++;                   // skip comma
 }

 

 // get BPM
 if(*p == 'b')
 {
   p++; p++;              // skip "b="
   num = 0;
   while(isdigit(*p))
   {
     num = (num * 10) + (*p++ - '0');
   }
   bpm = num;
   p++;                   // skip colon
 }

 

 // BPM usually expresses the number of quarter notes per minute
 wholenote = (60 * 1000L / bpm) * 4;  // this is the time for whole note (in milliseconds)



 // now begin note loop
 while(*p)
 {
   // first, get note duration, if available
   num = 0;
   while(isdigit(*p))
   {
     num = (num * 10) + (*p++ - '0');
   }
   
   if(num) duration = wholenote / num;
   else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after

   // now get the note
   note = 0;

   switch(*p)
   {
     case 'c':
       note = 1;
       break;
     case 'd':
       note = 3;
       break;
     case 'e':
       note = 5;
       break;
     case 'f':
       note = 6;
       break;
     case 'g':
       note = 8;
       break;
     case 'a':
       note = 10;
       break;
     case 'b':
       note = 12;
       break;
     case 'p':
     default:
       note = 0;
   }
   p++;

   // now, get optional '#' sharp
   if(*p == '#')
   {
     note++;
     p++;
   }

   // now, get optional '.' dotted note
   if(*p == '.')
   {
     duration += duration/2;
     p++;
   }
 
   // now, get scale
   if(isdigit(*p))
   {
     scale = *p - '0';
     p++;
   }
   else
   {
     scale = default_oct;
   }

   scale += OCTAVE_OFFSET;

   if(*p == ',')
     p++;       // skip comma for next note (or we may be at the end)

   // now play the note

   if(note)
   {
     
     tone(TONE_PIN, notes[(scale - 4) * 12 + note]);
     delay(duration);
     noTone(TONE_PIN);
     
   }
   else
   {
     
     delay(duration);
     
   }
 }
}

void loop()
{

 if (irRessPin6.decode(&irRessPin6Res)) {
irRessPin6FillVar(&irRessPin6Res);
irRessPin6.resume(); } else {irRessPin6ResVar(); }




if (irRessPin6FFA25D){play_rtttl(song);}
if (irRessPin6FF629D){play_rtttl(song1);}
//Дальше тестовые строчки для дальнейшего!
if (irRessPin6FFE21D){Serial.print("3");}
if (irRessPin6FF22DD){Serial.print("4");}
if (irRessPin6FF02FD){Serial.print("5");}
if (irRessPin6FFC23D){Serial.print("6");}
if (irRessPin6FFE01F){Serial.print("7");}
if (irRessPin6FFA857){Serial.print("8");}
if (irRessPin6FF906F){Serial.print("9");}
if (irRessPin6FF6897){Serial.print("10");}
if (irRessPin6FF9867){Serial.print("11");}
if (irRessPin6FFB04F){Serial.print("12");}
if (irRessPin6FF30CF){Serial.print("13");}
if (irRessPin6FF18E7){Serial.print("14");}
if (irRessPin6FF7A85){Serial.print("15");}











}
void irRessPin6FillVar(decode_results *irRessPin6Res) {
irRessPin6OV=int (irRessPin6Res->value); 
if(irRessPin6OV == -1) {irRessPin6OV = irRessPin6TP;} else {irRessPin6TP = irRessPin6OV ;}
if (irRessPin6OV ==int( 0xFFA25D)) {irRessPin6FFA25D = 1;}
if (irRessPin6OV ==int( 0xFFE21D)) {irRessPin6FFE21D = 1;}
if (irRessPin6OV ==int( 0xFF629D)) {irRessPin6FF629D = 1;}
if (irRessPin6OV ==int( 0xFF22DD)) {irRessPin6FF22DD = 1;}
if (irRessPin6OV ==int( 0xFF02FD)) {irRessPin6FF02FD = 1;}
if (irRessPin6OV ==int( 0xFFC23D)) {irRessPin6FFC23D = 1;}
if (irRessPin6OV ==int( 0xFFE01F)) {irRessPin6FFE01F = 1;}
if (irRessPin6OV ==int( 0xFFA857)) {irRessPin6FFA857 = 1;}
if (irRessPin6OV ==int( 0xFF906F)) {irRessPin6FF906F = 1;}
if (irRessPin6OV ==int( 0xFF6897)) {irRessPin6FF6897 = 1;}
if (irRessPin6OV ==int( 0xFF9867)) {irRessPin6FF9867 = 1;}
if (irRessPin6OV ==int( 0xFFB04F)) {irRessPin6FFB04F = 1;}
if (irRessPin6OV ==int( 0xFF30CF)) {irRessPin6FF30CF = 1;}
if (irRessPin6OV ==int( 0xFF18E7)) {irRessPin6FF18E7 = 1;}
if (irRessPin6OV ==int( 0xFF7A85)) {irRessPin6FF7A85 = 1;}

}
void irRessPin6ResVar() {
irRessPin6OV= 0;
irRessPin6FFA25D = 0;
irRessPin6FFE21D = 0;
irRessPin6FF629D = 0;
irRessPin6FF22DD = 0;
irRessPin6FF02FD = 0;
irRessPin6FFC23D = 0;
irRessPin6FFE01F = 0;
irRessPin6FFA857 = 0;
irRessPin6FF906F = 0;
irRessPin6FF6897 = 0;
irRessPin6FF9867 = 0;
irRessPin6FFB04F = 0;
irRessPin6FF30CF = 0;
irRessPin6FF18E7 = 0;
irRessPin6FF7A85 = 0;

}