 
 void thunder_init()
 {
-  pinMode(AS3935_CS, OUTPUT);
-  digitalWrite(AS3935_CS, HIGH);
-  pinMode(AS3935_INT, INPUT);
-
-//  if( !lightning.beginSPI(AS3935_CS, 2000000) ){ 
-//    Serial.println ("debug: Lightning Detector did not start up, freezing!"); 
-//    while(1); 
-//  }
-//  else
-//    Serial.println("debug: Lightning Detector Ready!");
-
-  //lightning.setIndoorOutdoor(AS3935_OUTDOOR); 
-}
-
-double TMP116_ReadTempSensor(void){
-  uint8_t data[2]; 
-  int16_t datac;
-
-  Wire.beginTransmission(TMP116_address); 
-  Wire.write(TMP116_temp_reg); 
-  Wire.endTransmission(); 
-
-  delay(10); 
-
-  Wire.requestFrom(TMP116_address,2); 
-
-  if(Wire.available() <= 2){  
-    data[0] = Wire.read(); 
-    data[1] = Wire.read(); 
-
-    datac = ((data[0] << 8) | data[1]); 
-
-    return datac*0.0078125; 
-  }
-}
-
-void tmp116_init(){
-  // Configuration Register: Default is 0x02, 0x20
-  // 11:10 MOD[1:0]
-  // 00: Continuous conversion (CC) - default
-  // 01: Shutdown (SD)
-  // 10: Continuous conversion (CC), same as 00 (reads back = 00)
-  // 11: One-shot conversion (OS)
-  // 9:7 CONV[2:0] http://www.ti.com/lit/ds/symlink/tmp116.pdf
-  // 100: - default
-  // 6:5 AVG[1:0]
-  // 01: - default 
-  // Default = 8 samples, 1 second
-  i2cwrite(TMP116_address, TMP116_config_reg, 0x02, 0x20);  
-}
-
-double i2cwrite(int device_addr, int register_addr, int high_bytes, int low_bytes){
-  Wire.beginTransmission(device_addr); 
-  Wire.write(register_addr);
-  Wire.write(high_bytes);
-  Wire.write(low_bytes);
-  Wire.endTransmission();
-  delay(10);
-}
+	pinMode(AS3935_CS, OUTPUT);
+	digitalWrite(AS3935_CS, HIGH);
+	pinMode(AS3935_INT, INPUT);
+
+	if( !lightning.beginSPI(AS3935_CS, 2000000) ){ 
+		Serial.println ("MIKROESAM3X_DEBUG:Lightning Detector did not start up, freezing:#");; 
+		has_thunder=false;
+	}
+	else
+		Serial.println("MIKROESAM3X_DEBUG:Lightning Detector Ready:#");
+
+	if (has_thunder) {
+		lightning.setIndoorOutdoor(AS3935_OUTDOOR); 
+	}
+}

void setup()
{
	Serial.begin(57600);
	Serial.println("MIKROE SAM3X WeatherDemo:#");

	bme_init();
	sgp_init();

//  SPI.begin();
//  SPI.setDataMode( SPI_MODE1 );

//  thunder_init();

//  Wire.begin();

//  tmp116_init();
}

int counter_sgp = 0;
void loop()
{
//  double temp = TMP116_ReadTempSensor();
//  Serial.println(temp);
  
//  if(digitalRead(AS3935_INT) == HIGH){
//    AS3935_intVal = lightning.readInterruptReg();
//    if(AS3935_intVal == AS3935_NOISE_INT){
//      Serial.println("debug: Noise."); 
//    }
//    else if(AS3935_intVal == AS3935_DISTURBER_INT){
//      Serial.println("debug: Disturber."); 
//    }
//    else if(AS3935_intVal == AS3935_LIGHTNING_INT){
//      Serial.println("debug: Lightning Strike Detected!"); 
//      // Lightning! Now how far away is it? Distance estimation takes into
//      // account any previously seen events in the last 15 seconds. 
//      byte distance = lightning.distanceToStorm(); 
//      Serial.print("Approximately: "); 
//      Serial.print(distance); 
//      Serial.println("km away!"); 
//    }
//  }

	if (has_bme) {
		bme_read();


void sgp_init() {
	if (! sgp.begin()) {
		Serial.println("MIKROESAM3X_DEBUG:SGP30: Sensor not found!:#");
        has_sgp=false;
    }
	Serial.print("MIKROESAM3X_DEBUG:Found SGP30 serial:");
	Serial.print(sgp.serialnumber[0], HEX);
	Serial.print(sgp.serialnumber[1], HEX);
	Serial.print(sgp.serialnumber[2], HEX);
