#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"
using namespace std;

//set up the NXT
Connection *connection = new Bluetooth();
Sensor *sensor1 = new Adc_8591(IN_1, connection, 0x90); //address set to 0x90
Adc_ports adc;
int main()
{
  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(40);
    cout << "Connected" << endl;
    cout << "Print ADC readings - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
     cout << sensor1->print() << endl; //print sensor reading
     //get sensor reading
     cout << "Get ADC values" << endl;
     sensor1->read(adc);
     cout << "ADC 0: " << adc.port0 << endl;
     cout << "ADC 1: " << adc.port1 << endl;
     cout << "ADC 2: " << adc.port2 << endl;
     cout << "ADC 3: " << adc.port3 << endl;
     cout << "Wait" << endl;
     Sleep(3000);
    }
    connection->disconnect();
  }
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    
    cout << e.who() << endl;
    connection->disconnect();
  }
  return 0;
}