Guest User

Untitled

a guest
May 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. const int sensorPin1 = A4;
  2. const int sensorPin2 = A5;
  3. const int button = 2;
  4. int Sensor1 = 0;
  5. int Sensor2 = 0;
  6. int threshold = 700;
  7. int motorTurnA = 12;
  8. int motorSpeedA = 3;
  9. int motorTurnB = 13;
  10. int motorSpeedB = 11;
  11. int speedForward = 50;
  12. int speedTurn = 100;
  13. int buttonState;
  14. int trigPin = 10;
  15. int echoPin = A3;
  16. long duration, cm, inches;
  17. int e = 0;
  18. int d = 0;
  19. void setup() {
  20.  
  21. Serial.begin(9600);
  22.  
  23. //Opsætning af porte for afstandmåler
  24. pinMode(sensorPin1, INPUT);
  25. pinMode(sensorPin2, INPUT);
  26. pinMode(trigPin, OUTPUT); //Den digitale pin SAfstandTrigger sættes til output (lyd output)
  27. pinMode(echoPin, INPUT);
  28.  
  29. //Den digitale pin SAstandEkko sættes til input (lyd input)
  30.  
  31. //Opsætning af porte for motorer
  32. pinMode(motorTurnA, OUTPUT); // Erklærer at motorTurn er output
  33. pinMode(motorSpeedA, OUTPUT); // Erklærer at motorSpeed er output
  34. pinMode(motorTurnB, OUTPUT); // Erklærer at motorTurn er output
  35. pinMode(motorSpeedB, OUTPUT); // Erklærer at motorSpeed er output
  36.  
  37. //opsætning af porte for buttons
  38. pinMode(button, INPUT_PULLUP);
  39. }
  40.  
  41. void loop() {
  42.  
  43. // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  44. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  45. digitalWrite(trigPin, LOW);
  46. delayMicroseconds(5);
  47. digitalWrite(trigPin, HIGH);
  48. delayMicroseconds(10);
  49. digitalWrite(trigPin, LOW);
  50.  
  51. // Read the signal from the sensor: a HIGH pulse whose
  52. // duration is the time (in microseconds) from the sending
  53. // of the ping to the reception of its echo off of an object.
  54. pinMode(echoPin, INPUT);
  55. duration = pulseIn(echoPin, HIGH);
  56.  
  57. // convert the time into a distance
  58. cm = (duration/2) / 29.1;
  59. inches = (duration/2) / 74;
  60.  
  61.  
  62. //Aflæser input for lyssensor
  63. Sensor1 = analogRead(sensorPin1);
  64. Sensor2 = analogRead(sensorPin2);
  65.  
  66. //Printer lyssensor input
  67. // Serial.print(Sensor2); // venstre
  68. // Serial.print(", ");
  69. // Serial.print(Sensor1); // højre
  70. // Serial.print(", ");
  71.  
  72. if (Sensor1<threshold && Sensor2>threshold) { // Sensor1 ser hvid og Sensor2 (venstre) ser sort - drej til venstre
  73. digitalWrite(motorTurnA, HIGH);
  74. analogWrite(motorSpeedA, speedForward);
  75. digitalWrite(motorTurnB, LOW);
  76. analogWrite(motorSpeedB, speedTurn);
  77. }
  78. else if (Sensor1>threshold && Sensor2<threshold) { // Sensor2 ser hvid og Sensor1 (højre) ser sort - drej til højre
  79. digitalWrite(motorTurnA, LOW);
  80. analogWrite(motorSpeedA, speedTurn);
  81. digitalWrite(motorTurnB, HIGH);
  82. analogWrite(motorSpeedB, speedForward);
  83. }
  84.  
  85.  
  86. else{
  87. digitalWrite(motorTurnA, HIGH);
  88. // analogWrite(motorSpeedA, speedForward);
  89. digitalWrite(motorTurnB, HIGH);
  90. // analogWrite(motorSpeedB, speedForward);
  91. }
  92.  
  93.  
  94. //Serial.print(",");
  95. Serial.println(cm);
  96. Serial.print(" CM: ");
  97. //Serial.println(e);
  98. //Serial.print(buttonState);
  99.  
  100. if (cm<10) {
  101. e++;
  102. Serial.print(d);
  103. // Serial.print(" D: ");
  104. // Serial.print(" E: ");
  105. Serial.print(e);
  106. while (e>3 && d<300)
  107. {
  108. digitalWrite(motorTurnA, HIGH);
  109. analogWrite(motorSpeedA, speedForward);
  110. digitalWrite(motorTurnB, LOW);
  111. analogWrite(motorSpeedB, speedTurn);
  112. d++;
  113. Serial.println(d);
  114. }
  115. while (d>30)
  116. {
  117. buttonState = digitalRead(button);
  118. if (buttonState == HIGH){
  119. Serial.println("HIGH");
  120. Serial.println(buttonState);
  121. digitalWrite(motorTurnA, LOW);
  122. analogWrite(motorSpeedA, speedForward);
  123. digitalWrite(motorTurnB, HIGH);
  124. analogWrite(motorSpeedB, speedTurn);
  125. } else {
  126. Serial.println("LOW");
  127. Serial.println(buttonState);
  128. digitalWrite(motorTurnA, HIGH);
  129. analogWrite(motorSpeedA, speedForward);
  130. digitalWrite(motorTurnB, LOW);
  131. analogWrite(motorSpeedB, speedTurn);
  132. }
  133. }
  134. }
  135.  
  136. }
Add Comment
Please, Sign In to add comment