Getting stuck on a line of code on Grove Beginner Kit for Arduino

Hi everyone,

I am in year 6 and doing a project on a Grove beginner kit (Genuino/uno board also known as a Seeeduino Lotus V1.1 development board) with a Line Finder, a buzzer module and a cable.

I am trying to change the code so that it plays the buzzer tune when a black line is detected, instead of the ChainableLED turning off as is happening at the moment.

Could someone please give me suggestions on how to change the code? Thank you for your help.

This is where I am getting my code: http://wiki.seeedstudio.com/Grove_Beginner_Kit_for_Arduino/#session-5-grove-line-finder

LINE FINDER:

//add ChainableLED library to this project
#include <ChainableLED.h>

//set the number of leds linked to the chain
#define NUM_LEDS 1

/assign leds as the name of
the ChainableLED set the
pin of the ChainableLED to
pin7(clock pin) and pin8(data pin)
and number of the leds
/
ChainableLED leds(7, 8, NUM_LEDS);

//naming pin3 as lineFinder
#define lineFinder 3

void setup() {
// initialize the digital pin as an input:
pinMode(lineFinder, INPUT);
//initialise ChainableLED leds
leds.init();
}

void loop() {
/*read the line detector input

  • if detected black(HIGH) turn on LED
    */
    if (HIGH == digitalRead(lineFinder))
    {
    //turn off LED
    leds.setColorRGB(0, 10, 10, 10);
    }

/*read the line detector input

  • if reading Logic low turn off LED
    */
    if (LOW == digitalRead(lineFinder))
    {
    //turn off LED
    leds.setColorRGB(0, 0, 0, 0);
    }
    }

BUZZER:

// initalise the frequency of the notes
#define NOTE_A4 501
#define NOTE_AS4 466
#define NOTE_C4 200
#define NOTE_D4 300
#define NOTE_E4 400
#define NOTE_F4 500
#define NOTE_G4 392
#define NOTE_C5 523

//assign buzzer as pin 6
#define buzzer 6

// notes in the melody:
int melody = {
NOTE_C4, NOTE_D4, NOTE_C4, NOTE_E4, NOTE_D4, NOTE_E4,
NOTE_C4, NOTE_E4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_D4,
NOTE_C4, NOTE_E4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4,
NOTE_E4, NOTE_D4, NOTE_F4, NOTE_E4, NOTE_E4,
};

// note durations: 4 = quarter note, 8 = eighth note, 2 = half note, etc.:
int noteDurations = {
8, 8, 8, 4, 4, 8,
8, 8, 4, 4, 4, 4,
2, 8, 8, 4, 4, 2,
4, 8, 4, 4, 2,
};

void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0 ; thisNote < 25 ; thisNote++) {

// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 2000 / 4, eighth note = 1000/8, half note = 4000, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzer, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzer);
}
}

void loop() {
// no need to repeat the melody.
}

to get answers here you need to give as much as you can to make it quick for us to give a specific answer. most people here may not have a "grove" kit or know what is in it. we dont know how far along you are in your project or where you are stuck. are you asking how to attach a buzzer or how to code it? what buzzer are you using? if you post the code that is currently lighing your lights someone can probobly show you how to change it for a buzzer.

Thank you for your suggestions on improving my post :slight_smile:

I have edited it now with more information.

void loop() {
  /*read the line detector input
   * if detected black(HIGH) turn on LED
  */
  if (HIGH == digitalRead(lineFinder))
  {
    //turn off LED
    leds.setColorRGB(0, 10, 10, 10);
  }

  /*read the line detector input
   * if reading Logic low turn off LED
  */
  if (LOW == digitalRead(lineFinder))
  {
    //turn off LED
    leds.setColorRGB(0, 0, 0, 0);
  }
}

Look at your code there. Pay attention to the comments. You should be able to identify the line that turns the leds off in response to the line finder.

If you don't want to turn the leds off then remove that line. If you want to do something different, then replace it with that different thing.

Here's the deal though, unless the line follower robot is supposed to stop in it's tracks and play this melody (or possibly is supposed to just drive straight off into the wall while playing it) then you'll have to completely rewrite the melody part. It can't be a blocking function that relies on delay. It's going to have to be something that can let the line following code still run alongside.

just put all the variable declorations together at the top. the code in the setup function of the buzzer code is what would need to replace the code that turns on the led. if you are looking to learn arduino, I would reccomend some simple code tutorials that would teach the basics of variables and functions and brackets. if you are interested in learning you should make sure you understand whatever you are doing. and learn one step at a time. copy/paste doesnt teach you anything.

Cheers :slight_smile: but I am trying to find what to get rid of and what to change and when I run the code it says buzzer was no such file or directory.

//add buzzer library to this project
#include

//naming pin3 as lineFinder
#define lineFinder 3

void setup() {
// initialize the digital pin as an input:
pinMode(lineFinder, INPUT);
//initialise ChainableLED leds
buzzer.init();
}

Did you not bother reading the program you posted? The one where 'buzzer' is declared as a pin number not included? What makes you think it's a good idea to include it? And why would a pin number have a .init() function?

Steve

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile: