sketch_moveCarBT.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <Servo.h>
  2. #include <Metro.h>
  3. Metro measureDistance = Metro(50);
  4. Metro sweepServo = Metro(20);
  5. //Standard PWM DC control
  6. int E1 = 5; //M1 Speed Control
  7. int E2 = 6; //M2 Speed Control
  8. int M1 = 4; //M1 Direction Control
  9. int M2 = 7; //M1 Direction Control
  10. int speedPin_M1 = 5; //M1 Speed Control
  11. int speedPin_M2 = 6; //M2 Speed Control
  12. int directionPin_M1 = 4; //M1 Direction Control
  13. int directionPin_M2 = 7; //M1 Direction Control
  14. ///For previous Romeo, please use these pins.
  15. //int E1 = 6; //M1 Speed Control
  16. //int E2 = 9; //M2 Speed Control
  17. //int M1 = 7; //M1 Direction Control
  18. //int M2 = 8; //M1 Direction Control
  19. Servo myservo; // create servo object to control a servo
  20. int pos = 60;
  21. int sweepFlag = 1;
  22. int myMaxSpeed = 255;
  23. int URPWM = 3; // PWM Output 0-25000US,Every 50US represent 1cm
  24. int URTRIG= 10; // PWM trigger pin
  25. uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01}; // distance measure command
  26. void SensorSetup(){
  27. pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRIG
  28. digitalWrite(URTRIG,HIGH); // Set to HIGH
  29. pinMode(URPWM, INPUT); // Sending Enable PWM mode command
  30. for(int i=0;i<4;i++){
  31. Serial.write(EnPwmCmd[i]);
  32. }
  33. }
  34. void carStop(){ // Motor Stop
  35. digitalWrite(speedPin_M2,0);
  36. digitalWrite(directionPin_M1,LOW);
  37. digitalWrite(speedPin_M1,0);
  38. digitalWrite(directionPin_M2,LOW);
  39. }
  40. void carTurnLeft(int leftSpeed,int rightSpeed){ //Turn Left
  41. analogWrite (speedPin_M2,leftSpeed); //PWM Speed Control
  42. digitalWrite(directionPin_M1,HIGH);
  43. analogWrite (speedPin_M1,rightSpeed);
  44. digitalWrite(directionPin_M2,HIGH);
  45. }
  46. void carTurnRight(int leftSpeed,int rightSpeed){ //Turn Right
  47. analogWrite (speedPin_M2,leftSpeed);
  48. digitalWrite(directionPin_M1,LOW);
  49. analogWrite (speedPin_M1,rightSpeed);
  50. digitalWrite(directionPin_M2,LOW);
  51. }
  52. void carBack(int leftSpeed,int rightSpeed){ //Move backward
  53. analogWrite (speedPin_M2,leftSpeed);
  54. digitalWrite(directionPin_M1,LOW);
  55. analogWrite (speedPin_M1,rightSpeed);
  56. digitalWrite(directionPin_M2,HIGH);
  57. }
  58. void carAdvance(int leftSpeed,int rightSpeed){ //Move forward
  59. analogWrite (speedPin_M2,leftSpeed);
  60. digitalWrite(directionPin_M1,HIGH);
  61. analogWrite (speedPin_M1,rightSpeed);
  62. digitalWrite(directionPin_M2,LOW);
  63. }
  64. void stop(void) //Stop
  65. {
  66. digitalWrite(E1,LOW);
  67. digitalWrite(E2,LOW);
  68. }
  69. void advance(char a,char b) //Move forward
  70. {
  71. /*analogWrite (E1,a); //PWM Speed Control
  72. digitalWrite(M1,HIGH);
  73. analogWrite (E2,b);
  74. digitalWrite(M2,HIGH);*/
  75. carAdvance(a, b);
  76. }
  77. void back_off (char a,char b) //Move backward
  78. {
  79. /*analogWrite (E1,a);
  80. digitalWrite(M1,LOW);
  81. analogWrite (E2,b);
  82. digitalWrite(M2,LOW);*/
  83. carBack(a, b);
  84. }
  85. void turn_L (char a,char b) //Turn Left
  86. {
  87. /*analogWrite (E1,a);
  88. digitalWrite(M1,LOW);
  89. analogWrite (E2,b);
  90. digitalWrite(M2,HIGH);*/
  91. carTurnLeft(a, b);
  92. }
  93. void turn_R (char a,char b) //Turn Right
  94. {
  95. /*analogWrite (E1,a);
  96. digitalWrite(M1,HIGH);
  97. analogWrite (E2,b);
  98. digitalWrite(M2,LOW);*/
  99. carTurnRight(a, b);
  100. }
  101. void carTurnSoftLeft() {
  102. carAdvance(0,myMaxSpeed);
  103. }
  104. void carTurnSoftRight() {
  105. carAdvance(myMaxSpeed, 0);
  106. }
  107. void setup(void)
  108. {
  109. int i;
  110. /*for(i=4;i<=7;i++)
  111. pinMode(i, OUTPUT); */
  112. myservo.attach(9);
  113. Serial.begin(9600); // Sets the baud rate to 9600
  114. SensorSetup();
  115. Serial.println("Run keyboard control");
  116. }
  117. void loop(void)
  118. {
  119. if(Serial.available()){
  120. char val = Serial.read();
  121. Serial.write(val);
  122. if(val != -1)
  123. {
  124. switch(val)
  125. {
  126. case 'z'://Move Forward
  127. advance (255,255); //move forward in max speed
  128. break;
  129. case 'x'://Move Backward
  130. back_off (255,255); //move back in max speed
  131. break;
  132. case 'q'://Turn Left
  133. turn_L (150,150);
  134. break;
  135. case 'd'://Turn Right
  136. turn_R (150,150);
  137. break;
  138. case 'w':
  139. Serial.println("Hello");
  140. break;
  141. case 'a':
  142. carTurnSoftLeft();
  143. break;
  144. case 'e':
  145. carTurnSoftRight();
  146. break;
  147. case 's':
  148. stop();
  149. break;
  150. }
  151. }
  152. else stop();
  153. }
  154. }