Beginner help - buzzer

Hey im trying to execute this program which is supposed to take values from a bluetooth module via a smartphone and use those values to turn on and off a buzzer.

I used the following code:


char data= Serial.read();
void setup() {
Serial.begin(9600);
}

if(data= 'a')
{
void loop() {
tone(8,1550, 300);
delay(600);
}
}
else if (data= 'b')
{
void loop() {
tone(8,0,300);
delay(600);
}
}
else
{
}


But it keeps giving me the error 'expected unqualified id before 'if' '.

Please help.

I want the buzzer to sound once you press a on the smartphones app, and continue sounding until b is pressed.

Okay so. A bit improvement.


int dd= 1;
void setup()
{
Serial.begin(9600);
}

void loop()
{

if(Serial.available()>0)
{
char data= Serial.read();
switch(data)
{
case 'a': dd=2;; break;
case 'b': dd=1; break;

}
do
{
tone(8,1550, 300);
delay(600);
} while (dd=2);
}
}


Now if I press 2, the buzzer doesnt stop sounding. I want it to to stop making that sound when I press 1.
Its not happening.

Helppp.

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.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

In your first code;

char data= Serial.read();
void setup() {
  Serial.begin(9600);
}

if(data= 'a')
{
void loop() {
  tone(8,1550, 300);
  delay(600);

What is

if(data= 'a')
{

Doing between the void setup() and void loop() ?

Tom.. :slight_smile:

Be sure to understand the difference between = and ==

To elaborate on ChrisTenone's post:
"=" assigns a new value to a variable
as in: make my variable called x store the number 6:

x = 6;

"==" means is equal to
as in: if the number stored in the variable x is equal to 6 then do something

if (x == 6) {
//Do something
}

Have a look at the reference pages on:

  1. Equal to (=)
  2. the Assignment operator (==)