Guest User

Untitled

a guest
Jan 12th, 2020
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This simple code allow you to send data from Arduino to Unity3D.
  2.  
  3. // uncomment "NATIVE_USB" if you're using ARM CPU (Arduino DUE, Arduino M0, ..)
  4. //#define NATIVE_USB
  5.  
  6. // uncomment "SERIAL_USB" if you're using non ARM CPU (Arduino Uno, Arduino Mega, ..)
  7. #define SERIAL_USB
  8.  
  9. //Include the NewPing library
  10. #include <NewPing.h>
  11.  
  12. //Make new sonar sensors
  13. NewPing sonar(9,8);
  14. NewPing sonar2(11,12);
  15.  
  16. void setup() {
  17. //Open Serial connection
  18. Serial.begin(9600); // You can choose any baudrate, just need to also change it in Unity.
  19. }
  20.  
  21. // Run forever
  22. void loop() {
  23.  
  24. //Make a variable and assign a value to it
  25. int distanceX = sonar.ping_cm();
  26. int distanceY = sonar2.ping_cm();
  27.  
  28. //Push values to Serial
  29. Serial.print("d_e"); //Tells the queue that this is an echo sensor...
  30. Serial.print(distanceX); Serial.print(",");
  31. Serial.println(distanceY);
  32.  
  33. delay(70); // Choose your delay having in mind your ReadTimeout in Unity3D
  34. }
  35.  
  36. void sendData(String data){
  37. #ifdef NATIVE_USB
  38. SerialUSB.println(data); // need a end-line because wrmlh.csharp use readLine method to receive data
  39. #endif
  40.  
  41. #ifdef SERIAL_USB
  42. Serial.println(data); // need a end-line because wrmlh.csharp use readLine method to receive data
  43. #endif
  44. }
RAW Paste Data