// analog read (DAC)

int stearing = A1;

int acceleration = A2;


const int button1 = 2;

int button1Read;

const int button2 = 4;

int button2Read;

const int button3 = 6;

int button3Read;

const int button4 = 7;

int button4Read;


const int pinDebounced1 = 8;

int pinDebounced1Read;

const int pinDebounced2 = 9;

int pinDebounced2Read;


unsigned char confirmByte;


void setup() {

  Serial.begin(38400);

  while (!Serial) {

    ;   // wait for serial port to connect

  }


  pinMode(button1, INPUT);

  pinMode(button2, INPUT);

  pinMode(button3, INPUT);

  pinMode(button4, INPUT);


  // turn on pullup resistors

  digitalWrite(button1, HIGH);      

  digitalWrite(button2, HIGH);

  digitalWrite(button3, HIGH);

  digitalWrite(button4, HIGH);


  // when hardware debouncing, then no pull up needed

  pinMode(pinDebounced1, INPUT);

  pinMode(pinDebounced2, INPUT);

}


void loop() {

  // put your main code here, to run repeatedly:

  float h = map(analogRead(stearing), 0, 1023, -100, 100);

  float v = map(analogRead(acceleration), 0, 1023, -100, 100);


  pinDebounced1Read = digitalRead(pinDebounced1);

  pinDebounced2Read = digitalRead(pinDebounced2);


  button1Read = digitalRead(button1);

  button2Read = digitalRead(button2);

  button3Read = digitalRead(button3);

  button4Read = digitalRead(button4);


  Serial.println(String(pinDebounced1Read)  + " " +

                 String(pinDebounced2Read) + " " +

                 String(button1Read) + " " +

                 String(button2Read) + " " +

                 String(button3Read) + " " +

                 String(button4Read) + " " +

                 String(h / 100) + " " +

                 String(v / 100));


  Serial.flush();


  // require confirmation from Unity

  while (Serial.available() == 0) {

    ;

  }


  // the confirmation byte can be used as a control sum

  confirmByte = Serial.read();

  Serial.flush();

  delay(5);

}