////////////////////////////////////////////////////////////////////////////////
// Name:       DoorbellUno-01                                                 //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB, April 2017, GPL copyrights                               //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=18             //
// This project used to be called VENSTERBANK.                                //
// An Arduino Uno is used to measure moisture, temperature and light. The data//
// are transmitted by wifi if requested so.                                   //
// Thanks: SR04 created by Rui Santos, http://randomnerdtutorials.com         //
////////////////////////////////////////////////////////////////////////////////


// SET PRECOMPILER OPTIONS *****************************************************
  // Initialse conditional compiling, uncomment to include, comment to exclude--
  // Do comment the next line for runtime versions -----------------------------
  #define RS232                   //Uncomment to include Serial Monitor sections
  //#ifdef RS232     //Only include these lines if the variable has been defined
  //Define the needed header files for the precompiler, no charge if not used --
  // Define precompiler variables, runs faster & doesn`t use RAM ---------------
  
  // Define PINS ---------------------------------------------------------------
  #define speakerPin   6     //To which PWM pin is the connected the LOUDSPEAKER
  #define buzzerPin   A5          //To which DIO pin is the connected the BUZZER
  #define switchPinIn  8               //To which DIO pin is connected deurbel A
  #define switchPinOut 9               //To which DIO pin is connected deurbel B
  
  //Define precompiler variables -----------------------------------------------
  bool   ledOnBoardVal = LOW;    //You choose HIGH-on or LOW-off for LED_BUILTIN
  int  frequency = 1000;            //Pitch of the sound produced by LOUDSPEAKER
//END OF PRECOMPILER OPTIONS --------------------------------------------------



void setup() { //Setup runs once ***********************************************
  disable_jtag();         //Disable jtag to free port C, enabled by default JTAG
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(speakerPin, OUTPUT);           //Set this pin as output to LOUDSPEAKER
  pinMode(buzzerPin, OUTPUT);                 //Set this pin as output to BUZZER
  pinMode(switchPinIn, INPUT_PULLUP);   //Set this pin as digital input DOORBELL
  pinMode(switchPinOut, OUTPUT);            //Set this pin as output to DOORBELL
  digitalWrite(switchPinOut, LOW);             //Set pin to pull LOW if DOORBELL

}//--(end setup )---------------------------------------------------------------



void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  while (!digitalRead(switchPinIn)) {
    digitalWrite(buzzerPin, HIGH);                 //Turn on the annoying buzzer
    tone(speakerPin, frequency, 50);
    frequency++;
    toggle_ledOnBoard();         //Toggles the LED_BUILTIN  ON or OFF onboardLED
    delay(2);                                    //Determins the fixed frequency
  }
  digitalWrite(buzzerPin, LOW);                    //Turn on the annoying buzzer
  frequency = 1000;                 //Pitch of the sound produced by LOUDSPEAKER
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER



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



void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------



////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  - CJMCU Moisture 2 Sensor  TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - CJMCU Moisture 2 Sensor  TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LED_BUILTIN Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - LED 3-color red          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - LED 3-color blue         PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - LED 3-color green        PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                          DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                          PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                          PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    -                          PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                          INT //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - DS1820 temperature       INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -      - VCC              - Output to breadboard     VCC //
// RES   -   1 - RES -      - PCINT14/RESET    -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       - Moisture 1 Left          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - Moisture 1 Right         ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898