-3

This buzzer needs to generate an audible sound for one second. The Buzzer is inside a function that is called by a loop that continues to run. It is possible to stop the loop for this one second. PWM to a piezo disk is what I had in mind. The coding has been simplified to: Stating the PWM // #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) // set timer_0 divisor to 64 for PWM frequency of 976.56 Hz (The DEFAULT) // The prescaler is a 3-bit value stored in the three least significant bits of the Timer/Counter register: CS02, CS01, and CS00. TCCR0B = TCCR0B & B11111000 | B00000011; // 976.56 Hz for PIN 5 // TCCR0B = _BV(CS00) | _BV(CS01) //Register first 5 bits B11111000 logical 'OR' Divisor = B00000011 and stop is with: TCCR0B = 0; Is that all that I need? #include #include #include #include #include #include #include class TimingISR { public: TimingISR();

  char lead_char, unit_sec, tens_sec, num_min;
  int int_unit_sec, int_tens_sec, int_num_min;
  int Count_Seconds, Remainder, brightness = 4, PowerLevel;   //BRIGHT_TYPICAL = 4, BRIGHT_DARKEST = 0, BRIGHTEST = 7
  bool Colon, PointFlag;  
  char data_char;  
};

// these are all Clobal Variables
  int pin_Clk = 2; //  or 3 for IC2
  int pin_DIO = 3; //  or 2 for IC2
  int PIN_RELAY = 4;  // controls power on/off to 'cook'   
  int PIN_Beeper = 5; // PWM beeps when finished
// Main() not included in this transmission, limit is 2 programs
// in case you recognize the above, I now have functions instead of GOTO's in the MAIN()
//*********************************************************************************
// http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/
// Arduino timer CTC interrupt example
// www.engblaze.com

// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>

//#define LEDPIN 2  will flash on & off once a second if a LED is used
Timer() 
void setup()
{
    //pinMode(LEDPIN, OUTPUT);

    // initialize Timer1
    cli();          // disable global interrupts
    TCCR1A = 0;     // set entire TCCR1A register to 0
    TCCR1B = 0;     // same for TCCR1B

    // set compare match register to desired timer count:
    OCR1A = 15624;
    // turn on CTC mode:
    TCCR1B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1024 prescaler:
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
    // enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);
    // enable global interrupts:
    sei();
}

//*********************************************************************************
ISR(TIMER1_COMPA_vect)     // once a second code    Counting down,
   void){  // can only get here if lead_ char is a 'c' or a '1' i.e. 'cook' or 'def'

       If ( Colon = On ) Colon = OFF; else Colon = ON;  //blink     
       // on TM1736 4 digit 7 second LED display
    {  if  ( Count_Seconds = 0 )  // Done cooking,  Beep On for one second
// https://arduino-info.wikispaces.com/Arduino-PWM-Frequency   
       // set timer 0 divisor to    64 for PWM frequency of   976.56 Hz (The DEFAULT)            
        TCCR0B = TCCR0B & B11111000 | B00000011;  // 976.56 Hz  for PIN 5 
// https://electronics.stackexchange.com/questions/164834/stop-timer-set-new-value-and-start-again-avr-interrupt
        TCCR0B &= ~(0 << CS00);   // set the CS02, CS01, and CS00 bits in TCCR0B 
        TCCR0B &= ~(0 << CS02);
        TCCR0B &= ~(0 << CS03);   
        //Write 'DonE' to the TM1736
        lead_char = 'D';  num_min = 'o'; tens_sec =  'n'; unit_sec =  'E';
        Colon = OFF; brightness = 7;//  Full Brightness // for a few seconds
DisplayAll()  // Main() has a character to number converter  // DisplayAll() has an extended Segment table
    }      
     {
      if  ( Count_Seconds = -1 ) //  Beep OFF  
           TCCR0B &= ~(1 << CS00);   // // clear the CS02, CS01, and CS00 bits in TCCR0B 
           TCCR0B &= ~(1 << CS02);
           TCCR0B &= ~(1 << CS03);
     }      
       if (  Count_Seconds < -11 )  cli();          // disable global interrupts //stop the interrupt timer
return(0);
       else
          Count_Seconds = Count_Seconds - 1  // don't restart the timer
 { ( if   Count_Seconds < -10 || Count_Seconds > 0 )  
          brightness = 4; counterValue = 0; resetCounter; Count_Seconds = Count_Seconds -1
 }
 }   End of Timer
// ----------------------------------------------------
  • 3
    Well, does it generate one or doesn't it? It's unlikely to be the "best" way, but why ask the question without stating if it meets you expectation or not? – Chris Stratton Jun 29 '17 at 5:11
  • 1
    Please explain, in your question, why you think that this is the way to generate a beep, and why you are asking if it is (what are your doubts)? What does it actually do, and why have you done it this way? It certainly seems a pretty low level way of achieving your goal. If you provide some explanation in your question, you will probably get some upvotes, but as it stands it is just an impressive block of code, but with no description (apart from the comments. Did you write it yourself, or did you find it on the web? – Greenonline Jun 29 '17 at 6:34
  • 1
    What's wrong with digitalWrite(x,HIGH); delay(1000); digitalWrite(x,LOW); ? This seems like using a sledgehammer to swat a fly. – SDsolar Jun 29 '17 at 13:52
  • -1 - I see that you have attempted to edit your question, yet managed to make it even more unintelligible. Please take the time to format it correctly, so that it is readable. Sorry, I am not being mean, but please learn how to correctly present your question, using the formatting tools available in markdown, instead of hoping someone else will format it for you. – Greenonline Jul 1 '17 at 4:48
2

You have pasted this code together from a couple of samples, I even recognise some of it, but I think you really don't understand what its doing.

If I want to learn how to fly a plane I am not just going to jump in the pilots chair and push the throttles wide open. Its the same with learning to code, and if you try and run this code it will do nothing.

To answer you question, no.

Try doing something really simple and annoying and wire a buzzer up to one of the pins and load this code:

static const int BuzzerPin = 5;
void setup()
{
  pinMode(BuzzerPin, OUTPUT);
}
void loop ()
{
  digitalWrite (BuzzerPin, HIGH);
  delay (500);
  digitalWrite (BuzzerPin, LOW);
  delay(500);
}

Then once you work out how that works, move on to PWM to work out how that works and then finally try interrupts (if that's what you really want).

(Oh and yes the code is virtually blink.ino which you get with the IDE.)

|improve this answer|||||
-3

A few ways.

1 set up a pwm output with your desired frequency. Set the duty cycle to 0.

2 when you need the output, turn in the duty cycle to something other than 0 or 1 briefly, and then 0 briefly.

3 repeat 3 as desired

here is a quick experiment, following the idea above.

void loop() {
    bzr_on(); delay(10); bzr_off(); delay(10);  //buzzer on/off - 1 round
    bzr_on(); delay(10); bzr_off(); delay(10);  //buzzer on/off - 1 round
    bzr_on(); delay(10); bzr_off(); delay(10);  //buzzer on/off - 1 round
    bzr_on(); delay(10); bzr_off(); delay(10);  //buzzer on/off - 1 round
    delay(100);
}

four short bursts of sound, following by a longer period of silence. over and over again.

enter image description here

it seems to work as expected.

a simpler approach is to keep the pwm on at all times, and use another gpio pin to short the output when needed, through a resistor. Less mcu load but takes one more pin.

|improve this answer|||||
  • This buzzer needs to generate an audible sound for one second. The Buzzer is inside a function that is called by a loop that continues to run. It is possible to stop the loop for this one second. PWM to a piezo disk is what I had in mind. The coding has been simplifiedto: – Winnie-the-Pough Jul 1 '17 at 1:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.