////////////////////////////////////////////////////////////////////////////////
// Name:       KY-006                                                         //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB rboek2@gmail.com June 2016 GPL copyrights                 //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=215          //
// This program will make sounds triggered by Serial Monitor                  //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************

// Define precompiler settings PINS --------------------------------------------
#define buzPin      6   //To which DIO pin is the passiv buzzer KY-006 connected

// Define variables ------------------------------------------------------------
bool ledBinVal = LOW;         //You can chose HIGH-on or LOW-off for LED_BUILTIN
int  frequency = 100;                    //Pitch of the sound produced by buzzer
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void setup() { //Setup runs once ***********************************************
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(buzPin, OUTPUT);                                   //KY-006 buzzer pin
  toggle_ledBin();                                           //Show we are awake
  peep();                          //Give a short beep with KY-006 active buzzer
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  tone(buzPin, frequency, 50);
  frequency++;
  delay(200);          //Needed for IR receiver Wait milliseconds, TIME TO SPARE
  toggle_ledBin();                  //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


void peep() { //Give a short beep with KY-006 active buzzer ********************
  tone(buzPin, 500, 5);
} //Exit peep ------------------------------------------------------------------


void beep(uint8_t ms) { //Create a beep with KY-006 active buzzer **************
  while (ms > 0){                            //Timer of the duration of the beep
    digitalWrite(buzPin, HIGH);                 //Push the speaker conus outward
    delay(2);                                    //Determins the fixed frequency
    digitalWrite(buzPin, LOW);                   //Pull the speaker conus inward
    delay(2);                                    //Determins the fixed frequency
    ms--;                                     //Countdown untill we reached zero
  }            //Timer of the duration of the beep has been counted down to zero
} //Exit beep ------------------------------------------------------------------



void toggle_ledBin(void) { //Toggles the LED_BUILTIN on-board LED on or off ****
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------


////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS                                                            //
// Prog=List=Chip                                                             //
// A0  = 97 = PF0 ADC0             =                                      ADC //
// A1  = 96 = PF1 ADC1             =                                      ADC //
// A2  = 95 = PF2 ADC2             =                                      ADC //
// A3  = 94 = PF3 ADC3             =                                      ADC //
// A4  = 93 = PF4 ADC4/TMK         =                                      ADC //
// A14 = 83 = PK6 ADC14/PCINT22    = Battery monitor H=1/2 6Vdc pack      ADC //
// A15 = 82 = PK7 ADC15/PCINT23    = Battery monitor H=1/3 9Vdc battery   ADC //
// 0   =  2 = PE0 RXD0/PCINT8      = Serial monitor, also on-board LED    RX0 //
// 1   =  3 = PE1 TXD0             = Serial monitor, also on-board LED    TX0 //
// 2   =  6 = PE4 OC3B/INT4        = IR TV remote control receiver        INT //
// 3   =  7 = PE5 OC3C/INT5        = Motorshield                          INT //
// 4   =  1 = PG5 OCOB             = Motorshield                          PWM //
// 5   =  5 = PE3 OC3A/AIN1        = Motorshield M3 => FREE PWM pin       PWM //
// 6   = 15 = PH3 OC4A             = Motorshield M4 => Buzzer             PWM //
// 7   = 16 = PH4 OC4B             = Motorshield                          PWM //
// 8   = 17 = PH5 OC4C             = Motorshield                          PWM //
// 9   = 18 = PH6 OC2B             = Motorshield                          PWM //
// 10  = 23 = PB4 OC2A/PCINT4      = Motorshield                          PWM //
// 11  = 24 = PB5 OC1A/PCINT5      = Motorshield                          PWM //
// 12  = 25 = PB6 OC1B/PCINT6      = Motorshield                          PWM //
// 13  = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low   PWM //
// 18  = 46 = PD2 TXD1/INT3        = Speed encoder Right                  TX1 //
// 19  = 45 = PD2 RXD1/INT2        = Speed encoder Left                   RX1 //
// 20  = 44 = PD1 SDA/INT1         = SDA Yellow SRF10                     TWI //
// 21  = 43 = PD0 SCL/INT0         = SCL White  SRF10                     TWI //
// 44  = 40 = PL5 OC5C             = 3 Color led Blue                     PWM //
// 45  = 39 = PL4 OC5B             = 3 Color led Red                      PWM //
// 46  = 38 = PL3 OC5A             = 3 Color led Green                    PWM //
// 50  = 22 = PB3 MISO/PCINT3      =                                      SPI //
// 51  = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// 52  = 20 = PB1 SCK/PCINT1       =                                      SPI //
// 53  = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898