Advertisement
Guest User

ShaloHAris

a guest
Apr 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. const int motorA1 = 13;
  2. const int motorA2 = 12;
  3. const int motorB1 = 11;
  4. const int motorB2 = 10;
  5. const int buzzer = 5 ;
  6. int i=0;
  7. int j=0;
  8. int state;
  9. int vSpeed=255; // Default speed, from 0 to 255
  10.  
  11. void setup()
  12. {
  13. pinMode(motorA1, OUTPUT);
  14. pinMode(motorA2, OUTPUT);
  15. pinMode(motorB1, OUTPUT);
  16. pinMode(motorB2, OUTPUT);
  17.  
  18. Serial.begin(9600);
  19. }
  20.  
  21. void loop()
  22. {
  23.  
  24. if(Serial.available() > 0){
  25. state = Serial.read();
  26. }
  27.  
  28.  
  29. //Fram
  30.  
  31. if (state == 'F')
  32. {
  33. analogWrite(motorA1, vSpeed);
  34. analogWrite(motorA2, 0);
  35. analogWrite(motorB1, vSpeed);
  36. analogWrite(motorB2, 0);
  37. }
  38.  
  39. //Backa
  40.  
  41. else if (state == 'B')
  42. {
  43. analogWrite(motorA1, 0);
  44. analogWrite(motorA2, vSpeed);
  45. analogWrite(motorB1, 0);
  46. analogWrite(motorB2, vSpeed);
  47. }
  48.  
  49.  
  50. //Left
  51. else if (state == 'L')
  52. {
  53. analogWrite(motorA1, 0);
  54. analogWrite(motorA2, 255);
  55. analogWrite(motorB1, 0);
  56. analogWrite(motorB2, 0);
  57. }
  58.  
  59.  
  60. // Höger
  61. else if (state == 'R')
  62. {
  63. analogWrite(motorA1, 0);
  64. analogWrite(motorA2, 0);
  65. analogWrite(motorB1, 0);
  66. analogWrite(motorB2, 255);
  67. }
  68.  
  69. // Ljud
  70. else if (state == 'V')
  71. {
  72. if (j==0){
  73. tone(buzzer, 1000);//Speaker on
  74. j=1;
  75. }
  76. else if (j==1){
  77. noTone(buzzer); //Speaker off
  78. j=0;
  79. }
  80. state='n';
  81. }
  82.  
  83.  
  84. // Stoppa bilen
  85. else if (state == 'S'){
  86. analogWrite(motorA1, 0); analogWrite(motorA2, 0);
  87. analogWrite(motorB1, 0); analogWrite(motorB2, 0);
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement