////////////////////////////////////////////////////////////////////////////////
// Name:       SmellyParty                                                    //
// Platform:   Arduino UNO R3                                                 //
// Created by: HARB rboek2@gmail.com october 2016 GPL copyrights              //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=8              //
// This program reads the TGS sensors and outputS the readings on Serial      //
//  monitor.                                                                  //
// To test run: http://robotigs.com/robotigs/mobile/sensors.php               //
////////////////////////////////////////////////////////////////////////////////

// FUSES (can always be altered f.e. 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             = TGS 01 analog input MQ 7             ADC //
// A1  = 96 = PF1 ADC1             = TGS 02 analog input MQ 135           ADC //
// A2  = 95 = PF2 ADC2             = TGS 03 analog input MQ 5             ADC //
// A3  = 94 = PF3 ADC3             =                                      ADC //
// A4  = 93 = PF4 ADC4/TMK         =                                      ADC //
// A14 = 83 = PK6 ADC14/PCINT22    =                                      ADC //
// A15 = 82 = PK7 ADC15/PCINT23    =                                      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        = Tgs01 Digital alarm signal input     INT //
// 3   =  7 = PE5 OC3C/INT5        = Tgs02 Digital alarm signal input     INT //
// 4   =  1 = PG5 OCOB             = Tgs03 Digital alarm signal input     PWM //
// 5   =  5 = PE5 OC3A/AIN1        = Tgs etc                              PWM //
// 12  = 25 = PB6 OC1B/PCINT6      =                                      PWM //
// 13  = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low   PWM //
// 18  = 46 = PD2 TXD1/INT3        =                                      TX1 //
// 19  = 45 = PD2 RXD1/INT2        =                                      RX1 //
// 20  = 44 = PD1 SDA/INT1         =                                      TWI //
// 21  = 43 = PD0 SCL/INT0         =                                      TWI //
// 44  = 40 = PL5 OC5C             =                                      PWM //
// 45  = 39 = PL4 OC5B             =                                      PWM //
// 46  = 38 = PL3 OC5A             =                                      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 //
////////////////////////////////////////////////////////////////////////////////

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

// Initialse conditional compiling, uncomment to include, comment to exclude ---
// #define RS232 1                 //Include RS232 sections to output debug info
// #ifdef RS232        //Only include this part if the variable has been defined

// Define precompiler variables ------------------------------------------------
#define LEDusr            PB7            //Arduino boards contain an onboard LED
#define DIGIN01             2  //Define the digital input pin connected to TGS01
#define ANAIN01            A0   //Define the analog input pin connected to TGS01
#define DIGIN02             3  //Define the digital input pin connected to TGS02
#define ANAIN02            A1   //Define the analog input pin connected to TGS02
#define DIGIN03             4  //Define the digital input pin connected to TGS03
#define ANAIN03            A2   //Define the analog input pin connected to TGS03

///Define the needed header files for the precompiler, no charge if not used ---
//#include <IRremote.h>     //Do never use the default by the IDE but replace it
//#include <AFMotor.h>  //Motors shield, Copyright Adafruit Industries LLC, 2009
//#include <TimerOne.h>    //Currently needed for reading wheel speed per second

//DEFINE CONSTANTS AND VARIABLES -----------------------------------------------
//const int RECV_PIN = 19;     //Connection of the IR remote TV control receiver
//3 COLOR LED breakout, common ground,      connect these pins preferably to PWM
/*-----( Declare Variables )-----*/
boolean      digVal01;               //Store digital alarm value in a single bit
unsigned int anaVal01;      //AnalogRead will fill variable with a 10 bit number
boolean      digVal02;               //Store digital alarm value in a single bit
unsigned int anaVal02;      //AnalogRead will fill variable with a 10 bit number
boolean      digVal03;               //Store digital alarm value in a single bit
unsigned int anaVal03;      //AnalogRead will fill variable with a 10 bit number
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void toggle_led(void) { //Toggles the default on-board LED on or off ***********
  if bit_is_clear(PORTB, LEDusr)                        //Test if the LED is off
    PORTB |= (1 << LEDusr);                        //If LED=off then turn LED on
  else                                                 //Else the LED must be on
    PORTB &= ~(1 << LEDusr);                             //Then turn the LED off
} //Exit toggle_led -- Or fe: state = !state; ----------------------------------


void setup() { //Setup runs once ***********************************************
  DDRB |= (1 << LEDusr);                         //Set onboard LED-pin as output
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode (DIGIN01, INPUT);             //Digital input signal, input is default
  pinMode (ANAIN01, INPUT);    //Analog, not actually required; input is default

  PORTB |= (1 << LEDusr);                            //Initally onboard LED = on
  delay(100); //Start pre run
  Serial.println("Smelly Gas Sensor brick test ");
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  anaVal01 = analogRead (ANAIN01);
  digVal01 = digitalRead(DIGIN01);
  anaVal02 = analogRead (ANAIN02);
  digVal02 = digitalRead(DIGIN02);
  anaVal03 = analogRead (ANAIN03);
  digVal03 = digitalRead(DIGIN03);
  
  Serial.print("01=");
  Serial.print(anaVal01), DEC; // display analog value
  Serial.print(",");
  Serial.print(digVal01), DEC; // display digital value

  Serial.print(" 02=");
  Serial.print(anaVal02), DEC; // display analog value
  Serial.print(",");
  Serial.print(digVal02), DEC; // display digital value

  Serial.print(" 03=");
  Serial.print(anaVal03), DEC; // display analog value
  Serial.print(",");
  Serial.print(digVal03), DEC; // display digital value

  Serial.println();
  delay(60000);                                            //Wait for 60 seconds
  toggle_led();                     //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898