Question about if and else if

Hey guys. I was just wondering where can I write if and else if.
Should I write in a setup or a loop?
The plot of my project is a buzzer beeping and stopping when I press the switch. Frequency of the buzzer will be different every time, and my project will only run once.

what is correct?

#define buzzer 3

#define switch 13

void setup() {
// put your setup code here, to run once:void setup() {
// put your setup code here, to run once:
Serial.begin(11520);
pinMode(switch, INPUT);
pinMode(buzzer, OUTPUT);

}

if (switch,HIGH);

tone(buzzer,100);

else if(switch,LOW);
noTone(buzzer); // here?

void loop() {
// put your main code here, to run repeatedly:

if (switch,HIGH);

tone(buzzer,100);

else if(switch,LOW);
noTone(buzzer);
// or here?

or is my code is completly wrong?

You need to read the reference for if statements and then change yours so they look like the ones everyone else uses.

Then think about this statement "Frequency of the buzzer will be different every time, and my project will only run once." How can the frequency change "every time" if it only runs once? That should tell you if the code goes in setup() which only runs once or loop() which keeps repeating so there are many different times.

Steve

or is my code is completly wrong?

Yes.

#define buzzer 3

#define switch 13

void setup()
{
  Serial.begin(11520);
  pinMode(switch, INPUT);
  pinMode(buzzer, OUTPUT);
}

// all of the code from here to loop() is outside of a 
// function and will cause a compile error  So, no, not here.
if (switch, HIGH);
tone(buzzer, 100);
else if (switch, LOW);
noTone(buzzer); // here?

void loop() 
{
  if (switch, HIGH); // semi-colon at end of if statement ends the if
  // every thing after the if will execute unconditionally.
  tone(buzzer, 100);
  else  if (switch, LOW);  // same semi-colon problem and else without previous if error
                           // cause no if block.  Should be just else, not else if  
  noTone(buzzer);
  // missing closing bracket to end loop() block

And at some point you will need to read the switch to determine its state.

K, how about now?
if (switch,HIGH)
{
tone(buzzer,100);

}
else (switch,LOW)
{
noTone(buzzer);
}

  else (switch,LOW)

Nope, there is no condition in an else statement. The switch state can only be HIGH or else it is LOW.

 else 
 {
  noTone(buzzer);
 }

You REALLY need to use better names for your variables (and NOT use #define).

Is switch a pin number or the state of the pin? If you can't IMMEDIATELY answer, then the name is meaningless.

If the variable was properly named switchPin, you'd KNOW that it makes no sense to compare the pin NUMBER to HIGH.

You'd also KNOW that there needed to be some way to get the state of the pin and that you needed to call that function to value switchState.

Is

if (switch, HIGH)

really valid syntax? What does it mean? I've never seen , (comma) used as a comparison operator before.

Steve

Is

if (switch, HIGH)

really valid syntax?

Yes.

The comma operator is being abused here. What switch, HIGH does is return the last item in the comma separated list. The comma operator is probably the least often correctly used operator in C (and the least often needed).

That statement is equivalent to

if(HIGH)

which the OP might have been.

Ah, so valid but quite wrong in this context. Thanks.

So I looked up the comma operator to see what it might be useful for. Now I think I'm going to forget all about it again. It's way too confusing for my simple mind.

Steve

A common use of the comma operator is updating multiple variables in a for loop:

for (...; ...; i++, j--) { ... }

Your 'if' statement needs to have something in it that can calculate to true or false, if you have a variable named 'something' that has a value of 1 assigned to it then (something == 1) would be true and the 'if' statement would run. (something == 2) would be false and the if statement would be skipped.