Guest User

Untitled

a guest
Feb 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. //constants won't change. They're used here to set pin numbers:
  2. const int buttonPin = 4; // the number of the pushbutton pin
  3. const int ledPin = 13; // the number of the LED pin
  4.  
  5. // variables will change:
  6. boolean flag = true;
  7. int buttonState = 0; // variable for reading the pushbutton status
  8. int distance = 0;
  9.  
  10. void setup() {
  11.  
  12. pinMode(buttonPin, INPUT);
  13. pinMode(ledPin, OUTPUT);
  14. pinMode(8, OUTPUT); //YELLOW
  15. pinMode(9, OUTPUT); //OREN
  16.  
  17. digitalWrite(buttonPin, LOW);
  18. digitalWrite(8, HIGH);
  19. digitalWrite(9, HIGH);
  20. }
  21.  
  22. void loop() {
  23.  
  24. buttonState = digitalRead(buttonPin);// read the state of the pushbutton
  25.  
  26. if (digitalRead(buttonPin) == LOW) { // if pushbutton is pressed,
  27. buttonState is HIGH:
  28.  
  29. digitalWrite(ledPin, HIGH);
  30. digitalWrite(8, LOW); //LOW=FORWARD DIRECTION
  31. digitalWrite(9, HIGH); //9 = MOTOR ON/OFF
  32. delayMicroseconds(100);
  33. digitalWrite(9, LOW);
  34. delayMicroseconds(100);
  35. distance++;
  36.  
  37. if(distance > 21000){
  38. distance = 0;
  39. delay (3000);
  40. reverse();
  41. }
  42.  
  43. }
  44.  
  45. if (digitalRead(buttonPin) == HIGH){
  46. // turn LED off:
  47. digitalWrite(ledPin, LOW);
  48. digitalWrite(8, LOW); //LOW=FORWARD DIRECTION
  49. digitalWrite(9, LOW); //9 = MOTOR ON/OFF
  50. delayMicroseconds(100);
  51. digitalWrite(9, LOW);
  52. delayMicroseconds(100);
  53.  
  54.  
  55. }
  56. }
  57.  
  58. void reverse(){
  59.  
  60. digitalWrite(8,HIGH);
  61.  
  62. digitalWrite(9,HIGH);
  63. delayMicroseconds(100);
  64. digitalWrite(9,LOW);
  65. delayMicroseconds(100);
  66. distance++;
  67.  
  68. if(distance > 21000){
  69. distance = 0;
  70. delay (3000);
  71. return;
  72. }
  73.  
  74. reverse();
  75.  
  76. }
Add Comment
Please, Sign In to add comment