Simple if else with a sensor and a buzzer

Hello there, beginner programmer here.

Expected result with the code:
The buzzer go through the sound pattern only when the condition > 250 on A0 is met. Otherwise, no sound.

Unwanted result:
The current code will produce the sound pattern when the condition is met, else it will produce a long beep.

Here is the code:

/* Testing MQ-2 GAS sensor with serial monitor
   Suitable for detecting of LPG, i-butane, propane, methane ,alcohol, Hydrogen or smoke
   More info: http://www.ardumotive.com/how-to-use-mq2-gas-sensor-en.html
   Dev: Michalis Vasilakis // Date: 11/6/2015 // www.ardumotive.com                     */

const int gasPin = A0; //GAS sensor output pin to Arduino analog A0 pin


//Example code KY012 active buzzer
int speakerPin = 7;


void setup () {
  pinMode (speakerPin, OUTPUT);
  Serial.begin(9600); //Initialize serial port - 9600 bps
}



void loop () {



  if (gasPin > 250){
     analogWrite (speakerPin, 255);
     delay (500);
     analogWrite (speakerPin, 0);
     delay (10);
   } else { analogWrite (speakerPin, LOW);
      
  }

  Serial.println(analogRead(gasPin));
  delay(500); // Print value every 1 sec.
}

Where is something like
gasPinValue = analogRead(gasPin);
to get a number for the comparison?

I don't have any clue really. This is my first sensor program.
Any clues will help me or examples.

Thanks

In your code the variable named gasPin refers to a pin on the Arduino. To get a reading from your gas sensor that is hooked up to gasPin the code needs to read the value of the pin, this is how the gas sensor communicates with the Arduino.

As CrossRoads pointed out, the function analogRead() is used to read an analog pin. You did use analogRead() but only in a print statement at the bottom of loop. Your comparison to 250 was with the pin number. The code needs to be changed so that the comparison is done with the value of the sensor read from the pin.

void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (gasSensorValue > 250) {
    analogWrite (speakerPin, 255);
    delay (500);
    analogWrite (speakerPin, 0);
    delay (10);
  } else {
    analogWrite (speakerPin, LOW);

  }

  Serial.println(gasSensorValue);
  delay(500); 
}

OK I understand.

I am a little confused with the syntaxe of the variables, could you help?

Here is my last try:

void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (analogRead(gasPin) > 250) {
    analogWrite (speakerPin, 255);
    delay (500);
    analogWrite (speakerPin, 0);
    delay (10);
  } else {
    analogWrite (speakerPin, LOW);

  }

  Serial.println(gasSensorValue);
  delay(500); 
}

Once you have read the gasPin, there is no need to read it again in an if; you want to use that momentary value.

void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (gasSensorValue > 250) {
    analogWrite (speakerPin, 255);
    delay (500);
    analogWrite (speakerPin, 0);
    delay (10);
  } else {
    analogWrite (speakerPin, LOW);

  }

  Serial.println(gasSensorValue);
  delay(500);
}

I don't know if it does what you want it to do.

OK Great, this has worked! Thanks

No I need to solve the sound problem with my buzzer.

In the folowing "else" I want to shut the active buzzer but I get a "long beep".

Any clues?

const int gasPin = A0; //GAS sensor output pin to Arduino analog A0 pin


//Example code KY012 active buzzer
int speakerPin =7;


void setup () {
  pinMode (speakerPin, OUTPUT);
  Serial.begin(9600); //Initialize serial port - 9600 bps
}



void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (gasSensorValue > 250) {
    analogWrite (speakerPin, 255);
    delay (500);
    analogWrite (speakerPin, 0);
    delay (10);
  } else {
    analogWrite (speakerPin, 0);

  }

  Serial.println(gasSensorValue);
  delay(500);
}

OK Finally this has worked, can anynody explain be why the command is counterintuitive like that 255 for mute and "0" for sound activation, am I missing something?

void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (gasSensorValue > 250) {
    analogWrite (speakerPin, 255); // do a off-on pattern starting with off
    delay (500);
    analogWrite (speakerPin, 0);
    delay (10);
  } else {                                      // shut the buzzer
    analogWrite (speakerPin, 255);

  }

  Serial.println(gasSensorValue);
  delay(500);
}

However, I was testing the tone() notone() function and it doesn't seems to work. The compiler gives me the following:

Arduino: 1.8.1 (Windows 8.1), Board: "Arduino/Genuino Uno"

GasSensor_Buzzer_v1:31: error: 'notone' was not declared in this scope

     notone(speakerPin);

                      ^

exit status 1
'notone' was not declared in this scope
void loop () {

  int gasSensorValue = analogRead(gasPin); // read sensor

  if (gasSensorValue > 250) {
    notone(speakerPin);
    delay (500);
    tone(speakerPin, 0);
    delay (10);
  } else {
    notone(speakerPin);

  }

  Serial.println(gasSensorValue);
  delay(500);
}

It depends on how your buzzer is wired. Vcc to buzzer to pin or pin to buzzer to ground.

And for tone, you probably need to include tone.h (can't check at the moment, look for some examples).

C++ is case sensitive, there is no notone() function.

You could try noTone().

https://www.arduino.cc/en/Reference/NoTone

Thanks!