 
   // Open serial communications and wait for port to open:
   Serial.begin(9600);
-  mySerial.begin(9600);
   Serial.println("Startup");
 
+  setupRN2483();
+  setupSDP610();
+   
+  led_off();
+  delay(2000);
+  startReportingCycle();
+}
+
+// Just for testing the snippet I used to send a float
+// static uint8_t floatdata[] = { 0,0,0,0,0,0,0,0};
+// Example on how to send a float
+// dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
+// dtostrf(difPressure,5,2,(char*)floatdata);  
+// myLora.txUncnf(floatdata); // non blocking call
+// myLora.txCnf(difPressure);    // blocking call
+
+// the loop routine runs over and over again forever
+void loop() {
+  cycleEndCheck();
+  led_on();
+  detectBicycle();
+}
+
+void startReportingCycle() {
+  cycleStartTime = millis();
+}
+
+void cycleEndCheck() {
+  cycleEndTime = millis();
+  // Calculate the current duration
+  long duration = cycleEndTime - cycleStartTime;
+  //Serial.println("Current duration: " + String(duration) + " with a max duration of " + String(cycleDuration) );
+  //Serial.println("cycleSartTime: " + String(cycleStartTime) + " ; sycleEndTime: " + cycleEndTime);
+  
+  // compare with max duration
+  if ( duration > cycleDuration ) {
+   // We reached the end of the cycle
+   finishReportingCycle(); 
+  } 
+}
+
+void finishReportingCycle() {
+  //TODO implement
+  // Datastructure: Count;Temparature;Humidity;GPS
+  // TODO GPS, for now we leave it out
+  String gpspos = "Unknown";
+  // TODO get temp and humidity
+  long temparature = 0;
+  long humidity = 0;
+  // Send message
+  String msg = String(bikeCount) + ";" + String(temparature, DEC) + ";" + String(humidity, DEC) + ";" + gpspos;
+  Serial.println("Transmitting [" + msg + "]");
+  myLora.txUncnf(msg); // non blocking call
+  // Reset cylce
+  bikeCount = 0;
+  cycleEndTime = 0;
+  // Start a new cycle
+  cycleStartTime = millis();
+}
+
+void detectBicycle(){
+  readPressure();
+  debug("difPresure: " + String(difPressure));
+  // Check if it is above the upper and below the lower treshold
+  if(difPressure <= LOWER_TRESHOLD || UPPER_TRESHOLD <= difPressure){
+    // TODO we might want to measure time to later calculate speed
+    // Block and wait until it is between again
+    while(LOWER_TRESHOLD <= difPressure &&  difPressure <= UPPER_TRESHOLD) {
+      led_off(); // Make it blink quickly to show it is triggered
+      delay(5);
+      led_on();
+      readPressure();
+    }
+    // Count it as bike
+    bikeCount++;
+    debug("Counted a new bike");
+    led_off();
+    // Take a break before continuing counting
+    delay(COUNTING_PAUSE_TIME);
+  }
+}
+
+void setupSDP610(){
+  // Initialize I2C communication
+  Wire.begin();
+  // Set the starting value for the pressure difference to 0
+  difPressure = 0.0;
+}
+
+void setupRN2483(){
+  mySerial.begin(9600);
   // Reset rn2483
   pinMode(RST, OUTPUT);
   digitalWrite(RST, HIGH);

    join_result = myLora.init();
  }
  Serial.println("Successfully joined TTN");
  
  led_off();

  // Initialize I2C communication
  Wire.begin();
  // Set the starting value for the pressure difference to 0
  difPressure = 0.0;
  delay(2000);
  
}

// Just for testing sending a float
static uint8_t floatdata[] = { 0,0,0,0,0,0,0,0};

// the loop routine runs over and over again forever
void loop() {
  led_on();
  readPressure();
  Serial.print("TXing: ");
  Serial.println(difPressure);
  dtostrf(difPressure,5,2,(char*)floatdata);
  myLora.txUncnf(floatdata); // non blocking call
  //myLora.txCnf(difPressure);    // blocking call
  
  led_off();

  delay(20000);
}


void readPressure() {
  difPressure = SDP6x.GetPressureDiff();

{
  digitalWrite(13, 0);
}

void debug(String msg) {
  if(debug) {
    Serial.println("[" + String(millis()) + "]" + msg);
  }
}


