Go Down

Topic: tone.h melody when led is ON (Read 3237 times) previous topic - next topic

Nik_90

Hello, I'm Italian, it is possible to make a melody to arduino ONLY when the LED is on?! Let me explain:

I connected a LED that lights up with photoresistor, if the LED is dark.

I wish that when the LED is on starts a tune, but as soon as the LED turns off the melody stops suddenly!

I'm finding some difficulties because I have basically implemented <tune.h> the library, and the melody part peacefully as soon as I turn on the LED lights up, but then to make sure that the LED turns off I have to wait until the end of the melody! smiley-eek-blue

That is how I found that if the function to play the melody RTTTL need to finish to complete the whole ..

LINK WITH CODE THAT I HAVE USED:
http://code.google.com/p/arduino-tone/source/browse/trunk/examples/RTTTL/RTTTL.pde?r=4

As soon as the song ends the LED turns off .. but I would like to return possibly as soon as the light, the LED goes out, and the melody stops.

Thanks in advance!

johnwasser

If you want the turning off of the LED to be more responsive, change play_rtttl(char *p) to check after every note to see if the LED should be turned off.  If you turn off the LED, return from the play_rtttl(char *p) function to end the melody.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Nik_90


If you want the turning off of the LED to be more responsive, change play_rtttl(char *p) to check after every note to see if the LED should be turned off.  If you turn off the LED, return from the play_rtttl(char *p) function to end the melody.


ok.. but, when i can chek if the led is on or off ?!

this is the complete code:
Code: [Select]

#include <Tone.h>
#include <LiquidCrystal.h>
int controllo_lum(int);

// inizializzo display LCD
LiquidCrystal lcd (12, 11, 5, 4, 3, 2);


Tone tone1;

//setto i PIN
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
int LEDpin = 6;          // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;

#define OCTAVE_OFFSET 0

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
};
//CANZONI DISPINIBILI IN RTTTL

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 *song = "Indiana:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d6,8p,8e6,1f.6,g,8p,8g,e.6,8p,d6,8p,8g,e.6,8p,d6,8p,8g,f.6,8p,e6,8p,8d6,2c6";

void setup(void)
{
 
  Serial.begin(9600);
  tone1.begin(7);          //porta a cui e connesso lo speaker
  lcd.begin(16, 2);        //setto colonne e righe display
   lcd.setCursor(0,0);      //setto il cursore sopra a sinistra
  lcd.print("CIAO");
}

void loop()
{
  photocellReading = analogRead(photocellPin); 

  //Serial.print("Analog reading = ");
  //Serial.println(photocellReading);     // the raw analog reading

  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0

  photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
 
  photocellReading = constrain(photocellReading, 600, 1023); //<-- il 970 potete cambiarlo

  LEDbrightness = map(photocellReading, 600, 1023, 0, 255);
 
  analogWrite(LEDpin, LEDbrightness);
  delay(100);
   play_rtttl(song);
  }
#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;
  int luce;
  luce = LEDbrightness;

  // 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
  }

  Serial.print("ddur: "); Serial.println(default_dur, 10);

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

  Serial.print("doct: "); Serial.println(default_oct, 10);

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

  Serial.print("bpm: "); Serial.println(bpm, 10);

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

  Serial.print("wn: "); Serial.println(wholenote, 10);


  // 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) && (luce>1))
    {
      Serial.print("Playing: ");
      Serial.print(scale, 10); Serial.print(' ');
      Serial.print(note, 10); Serial.print(" (");
      Serial.print(notes[(scale - 4) * 12 + note], 10);
      Serial.print(") ");
      Serial.println(duration, 10);
      tone1.play(notes[(scale - 4) * 12 + note]);
      delay(duration);
      tone1.stop();
    }
    else
    {
    return;
   //Serial.print("Pausing: ");
   //Serial.println(duration, 10);
    //delay(duration);
    }
    }
   
  }


where i can check the led?!

I am a beginner with arduino  :(

Thanks !!

johnwasser

Something like this:
Code: [Select]

boolean setBrightness()
  {
  photocellReading = analogRead(photocellPin); 
  photocellReading = 1023 - photocellReading;
  photocellReading = constrain(photocellReading, 600, 1023); //<-- il 970 potete cambiarlo 
  LEDbrightness = map(photocellReading, 600, 1023, 0, 255);
  analogWrite(LEDpin, LEDbrightness);
  return LEDbrightness > 0;  //  Return 'true' if the LED is on
  }

void loop()
  {
  if (setBrightness())
    play_rtttl(song);
  }


[...]
  if (note)
  {
    //  Play note
    tone1.play(notes[(scale - 4) * 12 + note]);
    delay(duration);
    tone1.stop();
  }
  else
  {
    // Play rest
    delay(duration);
  }

  if (!setBrightness())
    return;    // LED is off, time to stop playing the melody
[...]
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Nik_90


Something like this:
Code: [Select]

boolean setBrightness()
  {
  photocellReading = analogRead(photocellPin); 
  photocellReading = 1023 - photocellReading;
  photocellReading = constrain(photocellReading, 600, 1023); //<-- il 970 potete cambiarlo 
  LEDbrightness = map(photocellReading, 600, 1023, 0, 255);
  analogWrite(LEDpin, LEDbrightness);
  return LEDbrightness > 0;  //  Return 'true' if the LED is on
  }

void loop()
  {
  if (setBrightness())
    play_rtttl(song);
  }


[...]
  if (note)
  {
    //  Play note
    tone1.play(notes[(scale - 4) * 12 + note]);
    delay(duration);
    tone1.stop();
  }
  else
  {
    // Play rest
    delay(duration);
  }

  if (!setBrightness())
    return;    // LED is off, time to stop playing the melody
[...]



tanks, I have tried in this mode:
Code: [Select]

#include <Tone.h>
#include <LiquidCrystal.h>
int controllo_lum(int);

// inizializzo display LCD
LiquidCrystal lcd (12, 11, 5, 4, 3, 2);


Tone tone1;

//setto i PIN
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
int LEDpin = 6;          // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;

#define OCTAVE_OFFSET 0

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
};

//CANZONI DISPINIBILI IN RTTTL

MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d";

void setup(void)
{
 
  Serial.begin(9600);
  tone1.begin(7);          //porta a cui e connesso lo speaker
  lcd.begin(16, 2);        //setto colonne e righe display
   lcd.setCursor(0,0);      //setto il cursore sopra a sinistra
  lcd.print("CIAO");
}

void loop()
{
  photocellReading = analogRead(photocellPin); 

  //Serial.print("Analog reading = ");
  //Serial.println(photocellReading);     // the raw analog reading

  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0

  photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
 
  photocellReading = constrain(photocellReading, 600, 1023); //<-- il 970 potete cambiarlo

  LEDbrightness = map(photocellReading, 600, 1023, 0, 255);
 
  analogWrite(LEDpin, LEDbrightness);
  delay(100);
   {
  if (LEDbrightness)
  {
  play_rtttl(song);
  }
   }
}

 


#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;
  int luce;
  luce = LEDbrightness;

  // 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
  }

  Serial.print("ddur: "); Serial.println(default_dur, 10);

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

  Serial.print("doct: "); Serial.println(default_oct, 10);

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

  Serial.print("bpm: "); Serial.println(bpm, 10);

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

  Serial.print("wn: "); Serial.println(wholenote, 10);


  // now begin note loop
  while((*p) && (luce>1))
  {
    // 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)
    {
      Serial.print("Playing: ");
      Serial.print(scale, 10); Serial.print(' ');
      Serial.print(note, 10); Serial.print(" (");
      Serial.print(notes[(scale - 4) * 12 + note], 10);
      Serial.print(") ");
      Serial.println(duration, 10);
      tone1.play(notes[(scale - 4) * 12 + note]);
      delay(duration);
      tone1.stop();
    }
    else
    {
   
   Serial.print("Pausing: ");
   Serial.println(duration, 10);
    delay(duration);
   
    }
   
    if(!LEDbrightness)
    {
      return;
    }
   
  }
  }

ma quando la melodia inzia, anche se dopo nella stanza ce luce, il led non si spenge, perchè la melodia deve finire di suanare per intero!  =(

johnwasser

Your code does not change LEDbrightness except inb loop().  Checking LEDbrightness after each note (or rest) won't cause the music to stop because LEDbrightness will have the same value it had when you started playing the melody.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Nik_90


Your code does not change LEDbrightness except inb loop().  Checking LEDbrightness after each note (or rest) won't cause the music to stop because LEDbrightness will have the same value it had when you started playing the melody.


So what?! how can I fix this?! Any ideas? :(

Thanks ..

johnwasser



Your code does not change LEDbrightness except inb loop().  Checking LEDbrightness after each note (or rest) won't cause the music to stop because LEDbrightness will have the same value it had when you started playing the melody.


So what?! how can I fix this?! Any ideas? :(

Thanks ..


See reply #3.  Move the analogRead() code out of loop() and put it in a function you can call between notes.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Nik_90




Your code does not change LEDbrightness except inb loop().  Checking LEDbrightness after each note (or rest) won't cause the music to stop because LEDbrightness will have the same value it had when you started playing the melody.


So what?! how can I fix this?! Any ideas? :(

Thanks ..


See reply #3.  Move the analogRead() code out of loop() and put it in a function you can call between notes.


You're fantastic! :) Works perfectly! :) I did not realize I had to use an other function that managed if the LED was turned on or off .. xD
Now is perfect!! I did not realize your sketches .. (maybe because I saw it late at night .. xD)

Now, one thing, if I wanted to save the point where the melody stops, the situation is complicated right?! :)

thanks again!

johnwasser


Now, one thing, if I wanted to save the point where the melody stops, the situation is complicated right?! :)


Yes.  Complicated.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Go Up