Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. //project for designing electronic toys, written by Toma Ogawa
  2.  
  3. //Assigning pin numbers. n for negative and p for positive
  4. int motor_right_n = 2;
  5. int motor_right_p = 3;
  6. int motor_left_p = 4;
  7. int motor_left_n = 5;
  8. //EnA and EnB take the analog PWM input for motors 1 and 2 respectively.
  9. int EnA = 9;
  10. int EnB = 10;
  11. //receives the character sent by the laptop over the Tera Term terminal input.
  12. int incomingByte = 0;
  13.  
  14. void setup()
  15.  
  16. {
  17. //assigning pins
  18. pinMode(motor_right_n, OUTPUT);
  19. pinMode(motor_right_p, OUTPUT);
  20. pinMode(motor_left_p, OUTPUT);
  21. pinMode(motor_left_n, OUTPUT);
  22. pinMode(EnA, OUTPUT);
  23. pinMode(EnB, OUTPUT);
  24.  
  25. //making sure everything is off at the start.
  26. digitalWrite(motor_right_n, LOW);
  27. digitalWrite(motor_right_p, LOW);
  28. digitalWrite(motor_left_p, LOW);
  29. digitalWrite(motor_left_n, LOW);
  30. digitalWrite(EnA, LOW);
  31. digitalWrite(EnB, LOW);
  32.  
  33. //setting baud rate for connection over HC-06 bluetooth module.
  34. Serial.begin(9600);
  35. Serial.println("start");
  36.  
  37. }
  38.  
  39. void loop()
  40.  
  41. {
  42. if (Serial.available() > 0) {
  43. //reads character input from laptop
  44. incomingByte = Serial.read();
  45. }
  46.  
  47. //select case depending on character
  48. switch(incomingByte)
  49. {
  50. //see slide for combinations of digitalWrite values
  51.  
  52. case 's':
  53. digitalWrite(motor_right_n, LOW); // control for stop
  54. digitalWrite(motor_right_p, LOW);
  55. digitalWrite(motor_left_p, LOW);
  56. digitalWrite(motor_left_n, LOW);
  57. analogWrite(EnA, 0);
  58. analogWrite(EnB, 0);
  59. Serial.println("Stop\n");
  60. incomingByte='*';
  61. break;
  62.  
  63. case 'd':
  64. digitalWrite(motor_right_n, HIGH); // control for right
  65. digitalWrite(motor_right_p, LOW);
  66. digitalWrite(motor_left_p, HIGH);
  67. digitalWrite(motor_left_n, LOW);
  68. analogWrite(EnA, 200);
  69. Serial.println("right\n");
  70. incomingByte='*';
  71. break;
  72.  
  73. case 'a':
  74. digitalWrite(motor_right_n, LOW); // control for left
  75. digitalWrite(motor_right_p, HIGH);
  76. digitalWrite(motor_left_p, LOW);
  77. digitalWrite(motor_left_n, HIGH);
  78. analogWrite(EnB, 200);
  79. Serial.println("left\n");
  80. incomingByte='*';
  81. break;
  82.  
  83. case 'w':
  84. digitalWrite(motor_right_n, HIGH); // control for forward
  85. digitalWrite(motor_right_p, LOW);
  86. digitalWrite(motor_left_p, LOW);
  87. digitalWrite(motor_left_n, HIGH);
  88. analogWrite(EnA, 200);
  89. analogWrite(EnB, 200);
  90. Serial.println("forward\n");
  91. incomingByte='*';
  92. break;
  93.  
  94. case 'x':
  95. digitalWrite(motor_right_n, LOW); // control for backward
  96. digitalWrite(motor_right_p, HIGH);
  97. digitalWrite(motor_left_p, HIGH);
  98. digitalWrite(motor_left_n, LOW);
  99. analogWrite(EnA, 200);
  100. analogWrite(EnB, 200);
  101. Serial.println("backwards\n");
  102. incomingByte='*';
  103. break;
  104.  
  105. delay(5000);
  106.  
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement