Problem with counter when my buzzer is ringing

Good morning,

Im a begginer with Arduino; I have a little problem with this code.

When my beep function starts (buzzer rings), my sensor stop reading and I can´t use my counter (I can use again when beep finishes). I know the problem are DELAYs and I tried different solutions, but I couldn´t to fix it.
I was reading about millis(), Time library and multiprocess topics, but I don´t know how to use in my case.

Thank you in advance.

Regards

ARDUINO UNO

#include <LiquidCrystal.h>

LiquidCrystal lcd(7,6,5,4,3,2);

int contador = 0;
int pinsensor = 8;
int pinbuzzer = 9;
int lectura;

void setup()
{
  pinMode(pinsensor, INPUT);
  pinMode(pinbuzzer, OUTPUT);
 
  lcd.begin(16,2);
  lcd.setCursor(4,0);
  lcd.print("SACHETS");
  lcd.setCursor(0,1);
  lcd.print("Contador : 0");

}

void loop()
{
  lectura = digitalRead(pinsensor);

  if (lectura != 1)
  {
    contador = contador + 1;
    lcd.setCursor(11,1);
    lcd.print("     ");
    lcd.setCursor(11,1);
    lcd.print(contador);

    if (contador == 10)
    {
     beep(500);
    }
   
    while(digitalRead(pinsensor) != 1)
    {}
  }

 

}

void beep(unsigned char delayms){
 
 
  for (int i=0; i<20; i++){
  analogWrite(9, 254);     
  delay(delayms);         
  analogWrite(9, LOW);       
  delay(delayms);
  }
  analogWrite(9, 254);           

}

Your beep() function blocks, with the for loop and delay(). Nothing else happens while that loop is running.

500 will NOT fit in an unsigned char. Do NOT use char for passing numbers to functions.

:slight_smile: Thank you PaulS,

I have changed it. Can you help me about the delays? I want to change it because my loop is blocked when my beep function is running.

thank you in advance

What type of buzzer are you using?

The buzzers I got, only need power.

Its a piezoelectric buzzer. I want my buzzer sounds and stops 20 times and then it sounds all the time (In this process, my sensor/counter should be working).

Thanks

joseasis:
Its a piezoelectric buzzer.

Is that buzzer active or passiv?

There is an easy way to test the type your buzzer.

Change the '254' in the analogWrites to '255'
(this is equivalent to a digitalWrite(pin,HIGH),
analogWrite(pin,LOW) is digitalWrite(pin,LOW) anyway).

If your program still produces sound after that change,
you can treat your buzzer like you would treat a LED,
(switching it on and of via digitalWrite)
which reduces your problem to a 'special blink without delay'.

Its a piezoelectric buzzer.

Does it buzz when connected to a voltage source or do you need to toggle it on/off to make a beep ?

Whandall:
Is that buzzer active or passiv?

There is an easy way to test the type your buzzer.

Change the '254' in the analogWrites to '255'
(this is equivalent to a digitalWrite(pin,HIGH),
analogWrite(pin,LOW) is digitalWrite(pin,LOW) anyway).

If your program still produces sound after that change,
you can treat your buzzer like you would treat a LED,
(switching it on and of via digitalWrite)
which reduces your problem to a 'special blink without delay'.

Yes, I can change for digitalWrite. I will try now "special blink without delay".
Thank you

UKHeliBob:
Does it buzz when connected to a voltage source or do you need to toggle it on/off to make a beep ?

I need to toggle it on/off to make a beep, and then I need it only in ON

thanks