High and low logic

Preformatted text help me what's wrong here I can't turn on the buzzer properly sorry I'm a beginner thank you

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String sTopic = topic;

  if (sTopic == mqtt_keywords2)
  {
    Serial.println("Sense Control Command");
    if ((char)payload[1] == '1')
    {
      digitalWrite(BUZZER, HIGH);
    }

    else
    {
      digitalWrite(BUZZER, LOW);
    }
  }
}

Why not include the actual command when you print?

sorry i don't understand what you mean can you give me an example

what kind of buzzer do you have?

if you run this code, does it start buzzing for 5 seconds ?

const byte BUZZER = xxx; // here put the right pin number

void setup() {
  pinMode(BUZZER, OUTPUT);
  digitalWrite(BUZZER, HIGH);
  delay(5000);
  digitalWrite(BUZZER, LOW);
}

void loop() {}

some buzzers need to receive a succession of HIGH and LOW to produce a sound. You would use the tone() function for that. other would make some noise when set HIGH as you do.

how did you wire the buzzer? did you set the buzzer pin as an OUTPUT?

(read How to get the best out of this forum to know what to provide when you ask questions).

Next after the print, you test the first byte of the payload for a value. Why not print that value, as well, then you will actually see if value is what you expect. Right now you are just guessing it is correct.

what's wrong with this when data o buzzer is not low and when 1 is buzzer is not high

For one thing, you wrote that it is not working the way you want. So, perhaps those values are not where you think they are, or are not what you think they are. That is why we print things so we can track problems.

start by answering question in #4

yes buzzer is output

and the code? what does it do?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.