Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include <Servo.h>
  2. // Pinii motor 1
  3. #define mpin00 5
  4. #define mpin01 6
  5. // Pinii motor 2
  6. #define mpin10 3
  7. #define mpin11 11
  8.  
  9. const int trigPin = 2;
  10. const int echoPin = 12;
  11. // defines variables
  12. long duration;
  13. int distance1;
  14.  
  15.  
  16. Servo srv;
  17. void setup() {
  18. // configurarea pinilor motor ca iesire, initial valoare 0
  19. digitalWrite(mpin00, 0);
  20. digitalWrite(mpin01, 0);
  21. digitalWrite(mpin10, 0);
  22. digitalWrite(mpin11, 0);
  23. pinMode (mpin00, OUTPUT);
  24. pinMode (mpin01, OUTPUT);
  25. pinMode (mpin10, OUTPUT);
  26. pinMode (mpin11, OUTPUT);
  27. // pin LED
  28. pinMode(13, OUTPUT);
  29.  
  30. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  31. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  32. Serial.begin(9600); // Starts the serial communication
  33.  
  34.  
  35. }
  36. // Functie pentru controlul unui motor
  37. // Intrare: pinii m1 si m2, directia si viteza
  38. void StartMotor (int m1, int m2, int forward, int speed)
  39. {
  40.  
  41. if (speed==0) // oprire
  42. {
  43. digitalWrite(m1, 0);
  44. digitalWrite(m2, 0);
  45. }
  46. else
  47. {
  48. if (forward)
  49. {
  50. digitalWrite(m2, 0);
  51. analogWrite (m1, speed); // folosire PWM
  52. }
  53. else
  54. {
  55. digitalWrite(m1, 0);
  56. analogWrite(m2, speed);
  57. }
  58. }
  59. }
  60. // Functie de siguranta
  61. // Executa oprire motoare, urmata de delay
  62. void delayStopped(int ms)
  63. {
  64. StartMotor (mpin00, mpin01, 0, 0);
  65. StartMotor (mpin10, mpin11, 0, 0);
  66. delay(ms);
  67. }
  68. // Utilizare servo
  69. // Pozitionare in trei unghiuri
  70. // La final, ramane in mijloc (90 grade)
  71. void playWithServo(int pin)
  72. {
  73. srv.attach(pin);
  74. srv.write(0);
  75. delay(1000);
  76. srv.write(180);
  77. delay(1000);
  78. srv.write(90);
  79. delay(1000);
  80. srv.detach();
  81. }
  82. void loop() {
  83.  
  84. digitalWrite(trigPin, LOW);
  85. delayMicroseconds(2);
  86. // Sets the trigPin on HIGH state for 10 micro seconds
  87. digitalWrite(trigPin, HIGH);
  88. delayMicroseconds(10);
  89. digitalWrite(trigPin, LOW);
  90. // Reads the echoPin, returns the sound wave travel time in microseconds
  91. duration = pulseIn(echoPin, HIGH);
  92. // Calculating the distance
  93. distance1= duration/58.2;
  94. // Prints the distance on the Serial Monitor
  95. Serial.print("Distance: ");
  96. Serial.println(distance1);
  97.  
  98.  
  99. // Cod avertizare
  100. // Blink lent
  101. for (int i=0; i<10; i++)
  102. {
  103. digitalWrite(13, 1);
  104. delay(200);
  105. digitalWrite(13, 0);
  106. delay(200);
  107. }
  108. // Blink rapid. Scoateti cablul USB!!!!
  109. for (int i=0; i<10; i++)
  110. {
  111. digitalWrite(13, 1);
  112. delay(100);
  113. digitalWrite(13, 0);
  114. delay(100);
  115. }
  116. digitalWrite(13, 1);
  117. // Pornirea motorului Servo
  118. //playWithServo(8);
  119. // Acum se pornesc motoarele DC
  120.  
  121. if(distance1 <= 12){
  122. StartMotor (mpin00, mpin01, 1, 128);
  123. StartMotor (mpin10, mpin11, 1, 128);
  124. }
  125. else
  126. {
  127. StartMotor (mpin00, mpin01, 1, 0);
  128. StartMotor (mpin10, mpin11, 1, 0);
  129. }
  130.  
  131. //delay (500); // Cat timp e motorul pornit
  132. //delayStopped(500); // Cat timp e oprit
  133.  
  134. // StartMotor (mpin00, mpin01, 1, 128);
  135. //StartMotor (mpin10, mpin11, 1, 128);
  136.  
  137. // delay (500);
  138. // delayStopped(500);
  139. // StartMotor (mpin00, mpin01, 0, 128);
  140. // StartMotor (mpin10, mpin11, 1, 128);
  141. //
  142. // delay (500);
  143. // delayStopped(500);
  144. //
  145. // StartMotor (mpin00, mpin01, 1, 128);
  146. // StartMotor (mpin10, mpin11, 0, 128);
  147. //
  148. // delay (500);
  149. // delayStopped(500);
  150.  
  151.  
  152. // Clears the trigPin
  153.  
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement