Go Down

Topic: looking for help with a music playing, light seeking robot :) (Read 902 times) previous topic - next topic

etan32

Hi guys!

I'm making a light seeking robot that uses two LDR's to detect the brightest light source, and when it finds the light source it will play a melody just once. The coding I have at the moment is halfway there (I hope!) and I am stumped as to why the melody keeps looping, and thought maybe someone would like to jump on board and help me out? Also, want to know if the LDR system is working properly.

Here is the code:

Code: [Select]
#include <Servo.h>
#define Lowest 0
#define Highest 600
#include "pitches.h"

#define melodyPin 3
//Mario main theme melody
int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0,NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,

  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,

  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0,NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo

int tempo[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};


const int leftLDR = A0;
const int rightLDR = A1;
int leftValue;
int rightValue;
int difference;
Servo leftServo;
Servo rightServo;

void setup()
{
  pinMode(3, OUTPUT); //buzzer
  pinMode(13, OUTPUT); //LEDS
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  leftServo.attach(9);
  rightServo.attach(8);
  leftServo.write(90);
  rightServo.write(90);
}

void loop()
{
  sing(2);
  Serial.println(leftLDR);
  rightValue = analogRead(rightLDR);
  leftValue = analogRead(leftLDR);
  difference = abs(rightValue-leftValue);
  if(difference < 50)
  {
    moveForward();
  }
  else
  {
    if(rightValue > leftValue)
    {
     //moveRight(); //light avoid
     moveLeft();  //light follow
    }
    else
    {
     //moveLeft();   //light avoid
     moveRight();  //light follow
    }
  }
}

void moveLeft()
{
  leftServo.write(180);
  rightServo.write(180);
}

void moveRight()
{
  leftServo.write(0);
  rightServo.write(0);
}

void moveForward()
{
   leftServo.write(0);
   rightServo.write(180);
}

int song = 0;

void sing(int s){
  //iterate over the notes of the melody
  song = s;
  if(song==2);{
    Serial.println(" 'Mario Theme' ");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {
      // to calculate the note duration, take one second
       // divided by the note type.
       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
       int noteDuration = 1000/tempo[thisNote];
       buzz(melodyPin, melody[thisNote], noteDuration);
       // to distinguish the notes, set a minimum time between them.
       // the note's duration + 30% seems to work well:
       int pauseBetweenNotes = noteDuration * 1.30;
       delay(pauseBetweenNotes);
       // stop the tone playing:
       buzz(melodyPin, 0, noteDuration);
    }
  }
}

void buzz(int targetPin, long frequency, long length) {
  digitalWrite(13, HIGH);
  long delayValue = 1000000/frequency/2;
  long numCycles = frequency * length/1000;
   for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
  digitalWrite(13,LOW);

}



If you need the pitches.h file it's located here http://arduino.cc/en/Tutorial/tone#

PaulS

Quote
The coding I have at the moment is halfway there (I hope!) and I am stumped as to why the melody keeps looping,

Because you tell it to:
Code: [Select]
void loop()
{
  sing(2);



Go Up