Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <QTRSensors.h>
  2. #include <ZumoReflectanceSensorArray.h>
  3. #include <ZumoMotors.h>
  4. #include <ZumoBuzzer.h>
  5. #include <Pushbutton.h>
  6.  
  7. ZumoBuzzer buzzer;
  8. ZumoReflectanceSensorArray reflectanceSensors;
  9. ZumoMotors motors;
  10. Pushbutton button(ZUMO_BUTTON);
  11. int lastError = 0;
  12.  
  13. const int MAX_SPEED = 380; //maximum motor speed
  14. const int buttonPin = 27; //Flinduino boot button
  15.  
  16. int buttonPushCounter = 0; // counter for button presses
  17. int buttonState = 0; // button state detector
  18. int lastButtonState = 0; // previous state of button
  19.  
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23. pinMode(buttonPin, INPUT); //int for bootbutton imput
  24. Serial.begin(9600);
  25.  
  26. buzzer.play(">g32>>c32");
  27. reflectanceSensors.init();
  28. button.waitForButton();
  29.  
  30. pinMode(13, OUTPUT);
  31. digitalWrite(13, HIGH);
  32. delay(1000);
  33. int i;
  34. for(i = 0; i < 80; i++)
  35. {
  36. if ((i > 10 && i <= 30) || (i > 50 && i <= 70))
  37. motors.setSpeeds(-200, 200);
  38. else
  39. motors.setSpeeds(200, -200);
  40. reflectanceSensors.calibrate();
  41.  
  42. // Since our counter runs to 80, the total delay will be
  43. // 80*20 = 1600 ms.
  44. delay(20);
  45. }
  46. motors.setSpeeds(0,0);
  47. digitalWrite(13, LOW);
  48. buzzer.play(">g32>>c32");
  49. delay(5500);
  50. buttonState = digitalRead(buttonPin);
  51.  
  52. if (buttonState != latButtonState) {
  53. if (buttonState == HIGH) {
  54. buttonPushCounter++;
  55. Serial.println("Button Pushed");
  56. Serial.println("number of button pushes: ");
  57. Serial.println(buttonPushCounter);
  58. } else {
  59. Serial.println("off");
  60. }
  61. delay(5)
  62. }
  63. lastButtonState = buttonState;
  64.  
  65. if (buttonPushCounter % 4 == 0) {
  66. motors.setLeftSpeed(200);
  67. } else {
  68. motors.setRightSpeed(200);}
  69. delay (250);
  70. motors.setSpeed(0,0);
  71. delay (1000);
  72.  
  73.  
  74. }
  75.  
  76. void loop() {
  77. unsigned int sensors[6];
  78.  
  79. int position = reflectanceSensors.readLine(sensors);
  80.  
  81. int error = position - 2500;
  82.  
  83. int speedDifference = error / 3 + 5 * (error - lastError);
  84.  
  85. lastError = error;
  86.  
  87. int m1Speed = MAX_SPEED + speedDifference;
  88. int m2Speed = MAX_SPEED - speedDifference;
  89.  
  90.  
  91. if (m1Speed < 0)
  92. m1Speed = 0;
  93. if (m2Speed < 0)
  94. m2Speed = 0;
  95. if (m1Speed > MAX_SPEED)
  96. m1Speed = MAX_SPEED;
  97. if (m2Speed > MAX_SPEED)
  98. m2Speed = MAX_SPEED;
  99.  
  100. motors.setSpeeds(m1Speed, m2Speed);
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement