Pages: [1]   Go Down
Author Topic: How to stop program as soon as sensor conditions met?  (Read 1198 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know if this is the correct place to post this, or if I should be in etextiles, but since I am using a light sensor, I figured this would be as good a place as any.

I am using a protosnap lilypad with the light sensor and buzzer that is included on the board.  I have written my code so that if any light is detected the buzzer starts to play the Star Wars theme on the buzzer.

So far, it's working fine, but I was wondering, is there a way to get it to stop playing the music when the light sensor is covered?  What I mean is that if I cover the light sensor, it plays through the current song and then stops, but I want it to stop in the middle of the song as soon as the light is gone.

I will paste my code below (I am a beginner, I know there are better ways to handle the buzzer code, but I didn't understand all of the other code I found, so I made this work (I'm not a musician so trying to mathematically figure out tempo etc... didn't make much sense to me).  Any help would be appreciated, let me know if you need any other info from me, Thanks!

Code:
#include "pitches.h"
 
int sensorPin = A6; // Light Sensor connected to A6
int sensorValue; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

void setup() // run once, when the sketch starts
{
Serial.begin(9600); // initialize the serial port
pinMode(A6, INPUT); // sets analog pin A6 to be an input
pinMode(TestLED, OUTPUT); // sets analog pin 3 to be an output
}

void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // send that value to the computer
if (sensorValue < 10000 && sensorValue >= 100) //Light Detected, The higher the second sensorValue, the less sensitive it is to light.
{
digitalWrite(TestLED, HIGH); //turns on led connected to pin 3
 
  // Play the Star Wars Theme, using pitches.h include, the parenthesese have the Buzzer pin, note, and duration
  tone(7, NOTE_F4, 200);
  delay(200);
 
  tone(7, NOTE_F4, 200);
  delay(200);
 
  tone(7, NOTE_F4, 200);
  delay(200);
 
  tone(7, NOTE_AS4, 1200);
  delay(1200);
 
  tone(7, NOTE_F5, 600);
  delay(600);
 
  tone(7, NOTE_DS5, 200);
  delay(200);
 
  tone(7, NOTE_D5, 200);
  delay(200);
 
  tone(7, NOTE_C5, 200);
  delay(200);
 
  tone(7, NOTE_AS5, 1200);
  delay(1200);
 
  tone(7, NOTE_F5, 600);
  delay(600);
 
  tone(7, NOTE_DS5, 200);
  delay(200);
 
  tone(7, NOTE_D5, 200);
  delay(200);
 
  tone(7, NOTE_C5, 200);
  delay(200);
 
  tone(7, NOTE_AS5, 1200);
  delay(1200);
 
  tone(7, NOTE_F5, 600);
  delay(600);
 
  tone(7, NOTE_DS5, 200);
  delay(200);
 
  tone(7, NOTE_D5, 200);
  delay(200);
 
  tone(7, NOTE_DS5, 200);
  delay(200);

  tone(7, NOTE_C5, 900);
  delay(1200);
 
  noTone(7);


}
else //No Light Detected
{
digitalWrite(TestLED, LOW);
}
}

And the pitches.h code I included has the frequency all figured out for the notes to make it easier to write the music portion.  I will include it here too just in case that helps:

Code:
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#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
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I forgot to mention a few things.

The light sensor values - I have the threshold set fairly high so that I could test it in my lit room.  The second value (100) in that range would be lowered eventually (probably around 30) to make it much more sensitive to the light and dark values.  The aim was to have it not play the song in the dark, but then if there is any light to start playing the song.

What's this for - I am making this to be sewn into a knit sweater made for my son that looks like Chewbacca.  It's got a big collar, and I want to put it under the collar, and when he "pops" it up, light will hit the sensor and start playing the song.  If he flips the color down, it will stop playing.

It's working ok right now, but I didn't want to program the theme song in its entirety since it has to play through the whole song before it would stop, but if I could figure out the code that would stop it whenever the light sensor stops sensing light, then I would put the whole song in there.  I also thought about using different values in the sensor and having different songs play depending on the amount of light present.  Dim rooms play the Darth Vader theme, medium light plays the Imperial March, high light plays the theme song etc....

I also wish I knew enough about that buzzer to make it sound like R2D2, but I'm just starting so this is fun enough for now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know if this is the most appropriate approach with the Arduino, but I'd just put all the notes in an array, loop through it to play, and test the sensor in every iteration.
I've modified the code to reflect this approach, and also added comments for clarification smiley
(Code was only tested for compilation)

Code:
#include "pitches.h"

int sensorPin = A6; // Light Sensor connected to A6
int sensorValue = 0; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

//Auxiliary variables
int i;

/*
Array with all the notes, duration, and pause between notes.
First position is note, second is duration, third is pause.
*/
int music[18][3] =
{
  {NOTE_F4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_AS4, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_C5, 900, 1200}
};

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue < 10000 && sensorValue >= 100;
}

void setup() // run once, when the sketch starts
{
  Serial.begin(9600); // initialize the serial port
  pinMode(A6, INPUT); // sets analog pin A6 to be an input
  pinMode(TestLED, OUTPUT); // sets analog pin 3 to be an output
}

void loop() // run over and over again
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // send that value to the computer
  
  if(isLightDetected(sensorValue)) //Light Detected
  {
    digitalWrite(TestLED, HIGH); //turns on led connected to pin 3
    
    //Now just play through (iterate) the array
    for(i=0; i<18; i++)
    {
      tone(buzzer, music[i][0], music[i][1]);
      delay(music[i][2]);
      
       //Read latest sensor data
       sensorValue = analogRead(sensorPin);
      //Every time a note is played we test for sensor conditions
      if(!isLightDetected(sensorValue)) //If light is not detected
      {
        //Break out of the for loop, effectively stopping the music
        break;
      }
    }
  
    noTone(7);
  }
  else //No Light Detected
  {
    digitalWrite(TestLED, LOW);
  }
}
« Last Edit: March 23, 2014, 06:16:38 pm by TomS240 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help.  I just uploaded the code and it seems to still be acting the same way.  It starts playing when I shine a light on the sensor, but once the light is removed, it finishes play the whole song, then it stops. 

I don't understand all of your code you put in there (I'm still at a low level when it comes to this), but from what I understand, you were using a for loop to play each note, and after each note, have it check to see if the light is detected, and then kick out of the loop if it's not detected.  Would it be better to use a While loop instead of an if else loop in that situation?  I don't know exactly how to add that syntax to your code, but I wonder if that would make it act the way I want it to.  What do you think?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes you could use a while loop smiley
Please see updated code below:

**EDIT**
Just figured out why the music wasn't stopping. We also need to read the data from the sensor each time we test the conditions.
I've updated the code below (and also my post above), both should work now.

Code:
#include "pitches.h"

int sensorPin = A6; // Light Sensor connected to A6
int sensorValue = 0; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

//Auxiliary variables
int i = 0;

/*
Array with all the notes, duration, and pause between notes.
First position is note, second is duration, third is pause.
*/
int music[18][3] =
{
  {NOTE_F4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_AS4, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_C5, 900, 1200}
};

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue < 10000 && sensorValue >= 100;
}

void setup() // run once, when the sketch starts
{
  Serial.begin(9600); // initialize the serial port
  pinMode(A6, INPUT); // sets analog pin A6 to be an input
  pinMode(TestLED, OUTPUT); // sets analog pin 3 to be an output
}

void loop() // run over and over again
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // send that value to the computer
 
  if(isLightDetected(sensorValue)) //Light Detected
  {
    digitalWrite(TestLED, HIGH); //turns on led connected to pin 3
   
    //Initialize counter
    i = 0;
    //Now just play through (iterate) the array
    while(isLightDetected(sensorValue) && i < 18) //While light is detected AND i is below the total number of notes (18)
    {
      //Play a note
      tone(buzzer, music[i][0], music[i][1]);
      delay(music[i][2]);
     
      //Increment counter
      i++;
     
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
   
    noTone(7);
  }
  else //No Light Detected
  {
    digitalWrite(TestLED, LOW);
  }
}
« Last Edit: March 23, 2014, 06:15:50 pm by TomS240 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice, it works perfectly now.  Thanks so much for your help.  I'll start working on finishing the song now since it doesn't matter if it's that long.  I think I understand the logic behind your code, but it's still too advanced for me to write on my own.  Thanks again, you're great!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I wanted to have a few different conditions, would this could be easily adaptable?  Basically, if I wanted to have it play a different song depending on the amount of light that was being received.  So the current light detection is between 10000 and 100, but if I wanted to have something like:

Dark (no light) - 30 = no music

31 - 60 = Star Wars Theme

61 - 90 = Cantina Music
 
91 or above = Darth Vader Music

Would that be easy to seperate?  I see you use the boolean to check the light sensor, but isn't the boolean just a true false statement, so it wouldn't work for multiple conditions?  I had actually never seen boolean before in arduino until you had posted your code (like I said, I'm very new at this).  I might not be understanding that correctly.

I tried to edit your code a bit to see if I could figure this out, and I don't think I did it correctly (nothing plays).  I only have 2 conditions there (2 songs).  The second song (the Cantina) I added the notes, but have not gone through and done the timing yet so it won't sound very good yet, but I just wanted to test and see if I could get that logic to work.  Here is what I put:

Code:
#include "pitches.h"

int sensorPin = A6; // Light Sensor connected to A6
int sensorValue = 0; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

//Auxiliary variables
int i = 0;

/*
Array with all the notes, duration, and pause between notes.
First position is note, second is duration, third is pause.
*/
int StarWarsMainTheme[19][3] =
{
  {NOTE_F4, 200, 200}, 
  {NOTE_F4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_AS4, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_C5, 900, 1200}
};

int StarWarsCantina[59][3] =
{
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_A4, 200, 200}, 
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_F4, 200, 200},
};

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue < 10000 && sensorValue >= 100;
}

void setup() // run once, when the sketch starts
{
  Serial.begin(9600); // initialize the serial port
  pinMode(A6, INPUT); // sets analog pin A6 to be an input
}

void loop() // run over and over again
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // send that value to the computer
 
  if(isLightDetected(sensorValue < 10000 && sensorValue >= 100)) //Light Detected
  {
   
    //Initialize counter
    i = 0;
    //Now just play through (iterate) the array
    while(isLightDetected(sensorValue) && i < 19) //While light is detected AND i is below the total number of notes (18)
    {
      //Play a note
      tone(buzzer, StarWarsMainTheme[i][0], StarWarsMainTheme[i][1]);
      delay(StarWarsMainTheme[i][2]);
     
      //Increment counter
      i++;
     
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
   
    noTone(7);
  }
  if(isLightDetected(sensorValue < 99 && sensorValue >= 30)) //Light Detected
  {
   
    //Initialize counter
    i = 0;
    //Now just play through (iterate) the array
    while(isLightDetected(sensorValue) && i < 60) //While light is detected AND i is below the total number of notes (59)
    {
      //Play a note
      tone(buzzer, StarWarsCantina[i][0], StarWarsCantina[i][1]);
      delay(StarWarsCantina[i][2]);
     
      //Increment counter
      i++;
     
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
   
    noTone(7);
  }
 
  else //No Light Detected
  {
    noTone(7);
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For this version I got rid of the "isLightDetected" function and replaced it with the "getMusicName" function.
This function will test sensor value and return the correct song name to play under those conditions.

Then I've modified the logic to play the song. There's basically one "if" statement to test what music we should play, then it iterates through the correct music array.

I've added comments so you can understand what's going on, and hopefully help you to develop similar logic in the future.

Again, I've only tested this code for compilation smiley

Code:
#include "pitches.h"

//Let's define some constants.
//These are just an easier way of visually identifying the song name, we could as well use integers directly.
#define NO_MUSIC 0
#define MAIN_THEME 1
#define CANTINA_THEME 2

//Variables
int sensorPin = A6; // Light Sensor connected to A6
int sensorValue = 0; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

//Auxiliary variables
int i = 0;
int musicToPlay = NO_MUSIC; //Lets initialize the song name

/*
Array with all the notes, duration, and pause between notes.
First position is note, second is duration, third is pause.
*/
int StarWarsMainTheme[19][3] =
{
  {NOTE_F4, 200, 200},  
  {NOTE_F4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_AS4, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_C5, 900, 1200}
};

int StarWarsCantina[59][3] =
{
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_A4, 200, 200},  
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_FS4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_G4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_D4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_GS4, 200, 200},
  {NOTE_A4, 200, 200},
  {NOTE_F4, 200, 200},
};

//This function helps us figure out which music to play, based on the sensor value
int getMusicName(int sensorValue)
{
  //In these conditions...
  if(sensorValue < 10000 && sensorValue >= 100)
  {
    //... play the main theme
    return MAIN_THEME;
  }
  else if(sensorValue < 99 && sensorValue >= 30) //Else, in these conditions
  {
    //Play the cantina theme
    return CANTINA_THEME;
  }
  
  //If we reach this point our function hasn't returned yet. This means none of the conditions are met, so no music.
  return NO_MUSIC;
}

void setup() // run once, when the sketch starts
{
  Serial.begin(9600); // initialize the serial port
  pinMode(A6, INPUT); // sets analog pin A6 to be an input
}

void loop() // run over and over again
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // send that value to the computer
  
  //Test what music we should play
  musicToPlay = getMusicName(sensorValue);
  
  //Reset counter before playing
  i = 0;
  
  //(There's actually a "prettier" way of doing this. I'll just keep it simple)
  //If the getMusicName function returned MAIN_THEME, we should play it
  if(musicToPlay == MAIN_THEME)
  {
    //Let's play the main theme
    while(getMusicName(sensorValue) != NO_MUSIC && i < 19) //While music to play is different of NO_MUSIC AND i is below the total number of notes
    {
      //Play a note
      tone(buzzer, StarWarsMainTheme[i][0], StarWarsMainTheme[i][1]);
      delay(StarWarsMainTheme[i][2]);
      
      //Increment counter
      i++;
      
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
    
    noTone(7);
  }
  else if(musicToPlay == CANTINA_THEME) //Else...
  {
    //Let's play the cantina theme
    while(getMusicName(sensorValue) != NO_MUSIC && i < 60) //While music to play it different of NO_MUSIC AND i is below the total number of notes
    {
      //Play a note
      tone(buzzer, StarWarsCantina[i][0], StarWarsCantina[i][1]);
      delay(StarWarsCantina[i][2]);
      
      //Increment counter
      i++;
      
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
    
    noTone(7);
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again for helping with this.  I can't wait to try this out, I just got home from the hospital (we just had our 4th baby, both mom and baby are doing great), so I haven't had a chance yet to plug this into the lily pad and test it out, but it looks great.  Thanks for commenting all of your code, it's really helping me understand it, and making me a better programmer.  As soon as I can, I will plug this into the lilypad, and I'll let you know how it works.  Thanks once again, you rock!
Logged

Offline Offline
Faraday Member
**
Karma: 68
Posts: 2806
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
we just had our 4th baby
If you are the dad, don't tell your wife how high the Arduino is in your priority  smiley-wink
Congratulations!
Logged

The way you have it in your schematic isn't the same as how you have it wired up! That goes for me too.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ha, I am the dad, and every time I get excited about figuring something new out on the arduino, she sort of zones out and stops listening to me smiley

I bought the lilypad protosnap for her so she could do some projects with it eventually (she really likes sewing and crafting, so I thought it would be fun to work on some projects with her), but it will probably be a while before she has any real time to play around with it (I also got the Sparkfun Inventors Kit and an Uno board for me, but I have been mostly playing around with the protosnap lilypad to learn how it works).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just stopping by to say congratulations!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put this together today with your code (I only have the code with the one song in it right now, I still have to get the timing fixed for that cantina song, but now that I have that, I can add as many songs as I want and get it to play in different light).  Thanks again for your help TomS240, I really appreciate it!

Here is a video of my son trying out his finished coat:

« Last Edit: March 29, 2014, 02:21:20 pm by melarky » Logged

Pages: [1]   Go Up
Jump to: