fork download
  1. #include <SoftwareSerial.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define rxPin 10
  5. #define txPin 11
  6. #define rxPinJ 9
  7. #define txPinJ 8
  8.  
  9.  
  10. SoftwareSerial softSerial = SoftwareSerial(rxPin, txPin);
  11. SoftwareSerial Jetson = SoftwareSerial(rxPinJ, txPinJ);
  12.  
  13. int speed[] = {300, 400};
  14. int x, y, z;
  15. String face = "";
  16.  
  17.  
  18. void setup() {
  19. Jetson.begin(57600);
  20. softSerial.begin(57600);
  21. delay(2000);
  22. start(); // start create in full mode
  23. delay(500);
  24. setWelcome(); // write song_welcome
  25. delay(500);
  26. setWarning(); // write song_warning
  27. delay(500);
  28. }
  29.  
  30. const char TERMINATOR = '|';
  31.  
  32. void loop() {
  33. patrol();
  34. }
  35.  
  36. void start(){
  37. softSerial.write(128);
  38. softSerial.write(132);
  39. }
  40.  
  41. void setWelcome(){
  42. softSerial.write(140);
  43. softSerial.write(1);
  44. softSerial.write(3);
  45. softSerial.write(60);
  46. softSerial.write(12);
  47. softSerial.write(62);
  48. softSerial.write(12);
  49. softSerial.write(64);
  50. softSerial.write(12);
  51. }
  52.  
  53. void setWarning(){
  54. softSerial.write(140);
  55. softSerial.write(2);
  56. softSerial.write(2);
  57. softSerial.write(60);
  58. softSerial.write(12);
  59. softSerial.write(64);
  60. softSerial.write(12);
  61. }
  62.  
  63. void drive(int speed_right, int speed_left, int sec) {
  64. softSerial.write(145);
  65. softSerial.write((speed_right>>8)&0xFF);
  66. softSerial.write(speed_right&0xFF);
  67. softSerial.write((speed_left>>8)&0xFF);
  68. softSerial.write(speed_left&0xFF);
  69. delay(sec);
  70. }
  71.  
  72. void bump(){ // detect bump
  73. z = 0;
  74. softSerial.write(142);
  75. softSerial.write(7);
  76. delay(64);
  77. z = softSerial.read();
  78. if((z&0x03) == 0x03){
  79. stop_d();
  80. delay(100);
  81. drive(-300, -300, 500);
  82. drive(-300, 300, 1250);
  83. } // if hit in the front, turn back
  84. else if((z&0x01) == 0x01){
  85. stop_d();
  86. delay(100);
  87. drive(-300, -300, 500);
  88. drive(400, 0, 500);
  89. } // if hit in the right, turn left
  90. else if((z&0x02) == 0x02){
  91. stop_d();
  92. delay(100);
  93. drive(-300, -300, 500);
  94. drive(0, 400, 500);
  95. } // if hit in the left, turn right
  96. }
  97.  
  98. void stop_d(){
  99. softSerial.write(145);
  100. softSerial.write((byte)(0));
  101. softSerial.write((byte)(0));
  102. softSerial.write((byte)(0));
  103. softSerial.write((byte)(0));
  104. }
  105.  
  106.  
  107. void scan(){
  108. while(Jetson.available()>0){
  109. face = Jetson.readStringUntil(TERMINATOR);
  110. stop_d();
  111. delay(2000);
  112. if(face.toInt() == 100 || face.toInt() == 200){
  113. Jetson.write("ok");
  114. stop_d();
  115. delay(1000);
  116. if(face.toInt() == 100){
  117. play_song(1);
  118. delay(2000);
  119. // drive(-300, 300, 1250);
  120. face = "";
  121. while(1);
  122. }
  123. else if(face.toInt() == 200){
  124. while(1){
  125. play_song(2);
  126. }
  127. face = "";
  128. }
  129. }
  130. }
  131. }
  132.  
  133. void patrol(){
  134. x = rand() % 2;
  135. y = rand() % 2; // random speed
  136. for(int c = 0; c < 30; c++){
  137. drive(speed[x], speed[y], 100);
  138. bump();
  139. scan();
  140. }
  141. }
  142.  
  143. void play_song(int num){
  144. softSerial.write(141);
  145. softSerial.write(num);
  146. }
  147.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:10: fatal error: SoftwareSerial.h: No such file or directory
 #include <SoftwareSerial.h>
          ^~~~~~~~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty