Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Zumo32U4.h>
  3.  
  4. const uint16_t maxSpeed = 400;   //Max speed is 400.
  5.  
  6. Zumo32U4ButtonA buttonA;
  7. Zumo32U4LineSensors lineSensors;
  8. Zumo32U4Motors motors;
  9. Zumo32U4LCD LCD;
  10. #define NUM_SENSORS 5    //We will be using all five sensors, not three.
  11. unsigned int lineSensorValues[NUM_SENSORS];
  12.  
  13.  
  14. void calibrateSensors() {
  15.   delay(1000);    
  16.   for(uint16_t i = 0; i < 120; i++)   //the car rotates to the left and right in order to cross the
  17.   {         //black tape and calibrate the sensor values. The car stops
  18.     if (i > 30 && i <= 90)      //when the calibration is finished.
  19.     {
  20.       motors.setSpeeds(-200, 200);
  21.     }
  22.     else
  23.     {
  24.       motors.setSpeeds(200, -200);
  25.     }
  26.  
  27.     lineSensors.calibrate();
  28.   }
  29.   motors.setSpeeds(0, 0);
  30. }
  31.  
  32.  
  33. void setup() {
  34.   buttonA.waitForButton();
  35.   lineSensors.initFiveSensors();
  36.   calibrateSensors();
  37. }
  38.  
  39. void lcdWrite(String line1, String line2){
  40.   LCD.clear();
  41.   LCD.gotoXY(0, 0);
  42.   LCD.print(line1);
  43.   LCD.gotoXY(0, 1);
  44.   LCD.print(line2);
  45. }
  46.  
  47.  
  48. void LineFollow_noPID() {
  49.   uint16_t position = lineSensors.readLine(lineSensorValues,true);  //Determines where the tape is.
  50.   lcdWrite("",String(position));
  51.  
  52.   while(1950 < position && position < 2050){
  53.     motors.setSpeeds(100,100);
  54.     position = lineSensors.readLine(lineSensorValues);
  55.   }
  56.  
  57.   while(2050 <= position && position <= 4000){
  58.     motors.setSpeeds(100,50);
  59.     position = lineSensors.readLine(lineSensorValues);
  60.   }
  61.  
  62.   while(0 <= position && position <= 1950){
  63.     motors.setSpeeds(50,100);
  64.     position = lineSensors.readLine(lineSensorValues);
  65.   }
  66. }
  67.  
  68. void loop() {
  69.   LineFollow_noPID();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement