The buzzer doesn't beep?

Hi,
The buzzer doesn't beep, why? all serial.print OK.
Thanks
Adam

/* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

   Sketch by max wolf / www.meso.net
   v. 0.1 - very basic functions - mw 20061220

*/

int val;
int encoder0PinA = 8;
int encoder0PinB = 9;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

const int buzzer = 6; //buzzer to arduino pin 9

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
    if (encoder0Pos == 0)
    {
      Serial.print ("encoder0Pos====");
      Serial.println (encoder0Pos);

    }


  }
  encoder0PinALast = n;

  if (encoder0Pos < 0)
  {



    digitalWrite(buzzer, HIGH);
    delay(1000);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(250);
    digitalWrite(buzzer, LOW);
    delay(250);

    Serial.println ("!!!!!!!!!!!!!!!!!!!!");

  }
}

Whatever pin the buzzer is connect to, the pinMode needs to be set to OUTPUT in setup().

1 Like

Great!
Thank you.

Some buzzers do not buzz, you have to create a waveform to drive them. In you system it will probably click each time you turn it on and off.

1 Like

A new question for here.
The buzzer does beep here.
But it doesn't beep when changed code as:

if (encoder0Pos < 0)
  {

void Buzzer();

    Serial.println ("!!!!!!!!!!!!!!!!!!!!");

  }
void Buzzer()
{
    digitalWrite(buzzer, HIGH);
    delay(1000);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(250);
    digitalWrite(buzzer, LOW);
    delay(250);
}

Please post complete code. Functionalizing the buzzer routine should not be a problem. There must be another issue in the code you are not posting.

1 Like

Thanks.
The code beep, with problem is can't stop, keep beep there:

int val;
int encoder0PinA = 8;
int encoder0PinB = 9;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

const int buzzer = 6; //buzzer to arduino pin 9

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);

   pinMode(buzzer, OUTPUT); // Set buzzer - pin 6 as an output
   
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
    if (encoder0Pos == 0)
    {
      Serial.print ("encoder0Pos====");
      Serial.println (encoder0Pos);

    }
  }
  encoder0PinALast = n;

  if (encoder0Pos < 0)
  {
  this code beep but cant stop even turn the Encoder. 
    digitalWrite(buzzer, HIGH);
    delay(1000);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(250);
    digitalWrite(buzzer, LOW);
    delay(250);

    Serial.println ("!!!!!!!!!!!!!!!!!!!!");
 // void beep();

  }
}

the code doesn't beep at all :

int val;
int encoder0PinA = 8;
int encoder0PinB = 9;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

const int buzzer = 6; //buzzer to arduino pin 9

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);

   pinMode(buzzer, OUTPUT); // Set buzzer - pin 6 as an output
   
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
    if (encoder0Pos == 0)
    {
      Serial.print ("encoder0Pos====");
      Serial.println (encoder0Pos);

    }
  }
  encoder0PinALast = n;

  if (encoder0Pos < 0)
  {
  /* this code beep but cant stop even turn the Encoder. 
    digitalWrite(buzzer, HIGH);
    delay(1000);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(250);
    digitalWrite(buzzer, LOW);
    delay(250);
*/
    Serial.println ("!!!!!!!!!!!!!!!!!!!!");
  void Beep();

  }
}

void Beep()
{
  digitalWrite(buzzer, HIGH);
    delay(1000);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(250);
    digitalWrite(buzzer, LOW);
    delay(250);

}

You are not calling the function properly

https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/

1 Like

Thank you.
Yes, it's beep now, but the code seems to be infinite loop there, and didn't go back whatever I turn the encoder.

but the code seems to be infinite loop there, and didn't go back whatever I turn the encoder.

this code beep but cant stop even turn the Encoder.

Are both codes behaving the same? Is this a change from the posting where you said the code was working as desired?

Your Beep() function is blocking with the delay(), and the encoder can not be read during the two and a half seconds of delay().

Are you saying that when you turn the encoder so it goes from 0 to -1 and the beeping starts, you can not make the reverse rotation to get back to position 0 and stop the beeping?

What does your serial print tell you about the encoder position.

Serial.print (encoder0Pos);

1 Like

The code didn't beep when my posting, the serial.print OK.
now the Serial.print still OK, keep print "!!!!!!!!!!!!!!!!!" when met the Encoder read '-1', and keep doing there, never go back whatever Encoder turns.

Serial.print (encoder0Pos); print: -1!!!!!!!! ......

0022

I can not confirm your results. Your code is very non responsive to the encoder but I see

13:47:49.217 -> 1/2/3/2/1/0/encoder0Pos====0
13:48:10.912 -> -1/!!!!!!!!!!!!!!!!!!!!
13:48:15.963 -> !!!!!!!!!!!!!!!!!!!!
13:48:18.479 -> !!!!!!!!!!!!!!!!!!!!
13:48:21.009 -> !!!!!!!!!!!!!!!!!!!!
13:48:23.493 -> !!!!!!!!!!!!!!!!!!!!
13:48:26.024 -> !!!!!!!!!!!!!!!!!!!!
13:48:26.024 -> 0/encoder0Pos====0
13:48:33.018 -> 1/0/encoder0Pos====0
13:48:39.298 -> -1/!!!!!!!!!!!!!!!!!!!!
13:48:44.349 -> !!!!!!!!!!!!!!!!!!!!
13:48:46.830 -> !!!!!!!!!!!!!!!!!!!!
13:48:49.361 -> !!!!!!!!!!!!!!!!!!!!
13:48:49.361 -> 0/encoder0Pos====0

You are not running the posted code as the -1!!!!!!! is not possible output.

The issue may be related to the encoder being used, and the detent pattern. My encoder has one change with every detent, and I have to turn it slowly so that every change is read by the non responsive program. On a different encoder with all 4 changes between detents, you may not be picking up the A pin transition.

What encoder do you have? Are there detents? Do you know the AB pattern at the detents?
What do you see with this code if you go between detents?

void setup() {
 Serial.begin (115200);
 pinMode(encoderPinA, INPUT_PULLUP); 
 pinMode(encoderPinB, INPUT_PULLUP);
}
void loop(){  
  Serial.print(digitalRead(encoderPinA));
  Serial.print('\t');
  Serial.println(digitalRead(encoderPinB));
  delay(2000);  
}

This is the code I used except one line added because of you asked in # 10, Sorry I didn't meation it, that is Serial.print (encoder0Pos); :

As I said, I can not confirm your results with my encoder, and I think the issue may be with the detent positions and the ability of your non responsive code to pick up the transitions.

What model of encoder do you have?
What do you see when your run the detent detection code I posted in my last post?

What are you trying to do with your encoder? Have you written a simple program which just counts up and down without the beeping at -1?

Post a schematic, not a frizzy thing and a link to the buzzer that you have.

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