Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.67 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <EEPROM.h>
  3. #define SLAVE_ADDRESS 0x08
  4.  
  5.  
  6. // pin number declaration
  7.  
  8. // pins limitswitches
  9. const int limitswitchPin[] = {27,28,29,30,31,32,33,34};
  10. // pins infrared sensor
  11. const int infraredsensorPin[] = {A0,A1};
  12. // pins motor
  13. const int motorDrivingOutPin = 35;
  14. const int motorDrivingPWMPin = 7;
  15.  
  16. const int motorRotationOutPin = 36;
  17. const int motorRotationPWMPin = 8;
  18.  
  19. const int motorElevatorOutPin = 37;
  20. const int motorElevatorPWMPin = 9;
  21.  
  22. const int motorXaxisOutPin = 38;
  23. const int motorXaxisPWMPin = 10;
  24.  
  25. const int servoSteeringPin = 39;
  26.  
  27. const int relaiGripperInPin = 40;
  28. const int relaiGripperOutPin = 41;
  29.  
  30. const int elevatorEncoderDTPin = 3;
  31. const int elevatorEncoderCLKPin = 4;
  32.  
  33. const int xaxisEncoderDTPin = 5;
  34. const int xaxisEncoderCLKPin = 6;
  35.  
  36. // define global variables
  37. int distance[5];
  38. bool infrared[2] = {false,false};
  39. int leeway = 20;
  40. bool angled;
  41. String front;
  42. String back;
  43. int currentcase = 0;
  44. const int casesorder[] = {0,10,1,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,6,7,1,8,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,8,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,8,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,8,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,8,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,9,7,1,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,4,2,3,2,5,2,3,2,0};
  45. const int elevatorheight[] = {60,50,60,40,80,70,80,40,100,90,100,40,120,110,120,40,140,130,140,40,60,50,60,40,50,40,60,50,60,50,60,40,50,40,60,50,60,50,60,40,50,40,60,50,60,50,60,40,50,40,60,50,60,50,60,40,50,40,60,50,60,40,60,50,60,40,80,70,80,40,100,90,100,40,120,110,120,40,140,130,140};
  46. const int posxaxis[] = {0,1,0,2,0,3,0,4,0,5,0,6,5,0,5,4,0,4,3,0,3,2,0,2,1,0,6,0,5,0,4,0,3,0,2,0,1};//0 is blikjes pakken buiten robot 1-6 zijn opslagplekken 1t/m 6
  47. int nElevatorcase = 0;
  48. int nXaxiscase = 0;
  49. int casescounter = 0;
  50. int elevatorcounter = 0;
  51. int xaxiscounter = 0;
  52. bool stopdriving;
  53. int encoderval;
  54. bool stopprogram = false;
  55. int forwardDriveTime = 3000;//drive time in ms for case 6 (possibly make variable for different time the case is used)
  56. int backwardsDriveTime = 1750;//drive time in ms for case 9 (possibly make variable for different time the case is used)
  57. int infraredThresh1 = 0.4; //als onder deze waarde is valt is de afstand zeker meer dan 30 cm * of minder dan 0.1)
  58.  
  59. float elevatorlowestpoint = 40;// the lowest point in the elevator without pressing the limitswitch in cm
  60. float elevatordistance = 100; //cm die de lift tussen zijn limitswitches heeft zitten
  61. int targetsXaxis[] = {800,350,300,250,200,150,100}; // encoder values required to aim for when going towards position
  62.  
  63. int xaxisTotalSteps = 0; // encoder step available in the xaxis
  64. int elevatorTotalSteps = 0; // encoder steps available in the elevetor
  65. // bool rotationstate = false; // mogelijk gebruiken om aan te geven welke stand bastion gedraaid is
  66.  
  67.  
  68.  
  69. void setup()
  70. {
  71.   pinMode(motorDrivingOutPin, OUTPUT);
  72.   pinMode(motorDrivingPWMPin, OUTPUT);
  73.   pinMode(motorRotationOutPin, OUTPUT);
  74.   pinMode(motorRotationPWMPin, OUTPUT);
  75.   pinMode(motorElevatorOutPin, OUTPUT);
  76.   pinMode(motorElevatorPWMPin, OUTPUT);
  77.   pinMode(motorXaxisOutPin, OUTPUT);
  78.   pinMode(motorXaxisPWMPin, OUTPUT);
  79.   pinMode(servoSteeringPin, OUTPUT);
  80.   pinMode(relaiGripperInPin, OUTPUT);
  81.   pinMode(relaiGripperOutPin, OUTPUT);
  82.  
  83.   digitalWrite(motorDrivingPWMPin,LOW);
  84.   digitalWrite(motorRotationPWMPin,LOW);
  85.   digitalWrite(motorElevatorPWMPin,LOW);
  86.   digitalWrite(motorXaxisPWMPin,LOW);
  87.  
  88.   pinMode(elevatorEncoderDTPin, INPUT);
  89.   pinMode(elevatorEncoderCLKPin, INPUT);
  90.   pinMode(xaxisEncoderDTPin, INPUT);
  91.   pinMode(xaxisEncoderCLKPin, INPUT);
  92.  
  93.   //digitalWrite(relaiGripperOutPin,LOW);
  94.   //digitalWrite(relaiGripperInPin,LOW;
  95.   for(int i = 0; i<8;i++)
  96.   {
  97.     pinMode(limitswitchPin[i],INPUT);
  98.   }
  99.   Wire.begin();
  100.   Serial.begin(9600); // Starts the serial communication or bluetooth
  101.  
  102.   xaxisTotalSteps += EEPROM.read(0) + (EEPROM.read(1) << 8); //read bytes from eeprom and set xaxistotal to value
  103.   elevatorTotalSteps += EEPROM.read(2) + (EEPROM.read(3) << 8);
  104. }
  105.  
  106.  
  107.  
  108. void updateInfrareds()//update infrared values of the robot
  109. {
  110.   for(int i;i<2;i++)
  111.   {
  112.     float irvalue = analogRead(infraredsensorPin[i])*0.0048828125; // voltage wat sensor terug geeft(5/1024)
  113.     if(irvalue < infraredThresh1)
  114.     {
  115.         infrared[i] = true;
  116.     }
  117.     else
  118.     {
  119.       infrared[i] = false;
  120.     }
  121.   }
  122. }
  123.  
  124.  
  125.  
  126. void printvalues()
  127. {
  128.   String distances = "distances: " + (String)distance[0] + " " + (String)distance[1] + " " + (String)distance[2] + " " + (String)distance[3] + " " + (String)distance[4] + "/n";
  129.   String infrareds = "infrareds: " + (String)infrared[0] + " " + (String)infrared[1] + "/n";
  130.   String positioning = "angled: " + (String)angled + " " + front + " " + back + "/n";
  131.   String casesstring = "case: " + (String)currentcase + "/n";
  132.   Serial.println(distances + infrareds + positioning + casesstring);
  133. }
  134.  
  135. void updateUltrasonics()// updates ultrasonic sensor values from the arduino one chip
  136. {
  137.   Wire.requestFrom(SLAVE_ADDRESS, 10);
  138.   if(Wire.available() >= 10)
  139.   {
  140.     for(int i = 0; i < 5; i++)
  141.     {
  142.       int iRXVal;
  143.       for (int j = 0; j < 2; j++)             // Receive and rebuild the 'int'.
  144.       {
  145.           iRXVal += Wire.read() << (j * 8);   //    "     "     "     "    "
  146.       }
  147.       //  Serial.println(iRXVal);                 // Print the result.
  148.       distance[i] = iRXVal;
  149.     }
  150.   }
  151. }
  152.  
  153. void steeringforward() //decides how to steer during forward driving
  154. {
  155.   // check if distance is the same for sensors on the same side
  156.   if(distance[0] >= distance[2] - leeway && distance[0] <= distance[2] + leeway && distance[1] >= distance[3] - leeway && distance[1] <= distance[3] + leeway)
  157.   {
  158.     Serial.println("Robot is angled correctly");
  159.     angled = true;
  160.   }
  161.   else
  162.   {
  163.     Serial.println("Robot is angled incorrectly");
  164.     angled = false;
  165.   }
  166.  
  167.   // check if the front is centerd
  168.   if(distance[0] >= distance[1] - leeway && distance[0] <= distance[1] + leeway)
  169.   {
  170.     front = "centerd";
  171.   }
  172.   else if(distance[0] >= distance[1])
  173.   {
  174.     front = "right side";
  175.   }
  176.   else
  177.   {
  178.     front = "left side";
  179.   }
  180.   //check if the back is sented
  181.       if(distance[2] >= distance[3] - leeway && distance[2] <= distance[3] + leeway)
  182.   {
  183.     back = "centerd";
  184.   }
  185.   else if(distance[2] >= distance[3])
  186.   {
  187.     back = "right side";
  188.   }
  189.   else
  190.   {
  191.     back = "left side";
  192.   }
  193.   //servo aansturen op basis van gegevens
  194. }
  195.  
  196.  
  197. void steeringbackwards() //decides how to steer during forward driving
  198. {
  199.   if(distance[0] >= distance[2] - leeway && distance[0] <= distance[2] + leeway && distance[1] >= distance[3] - leeway && distance[1] <= distance[3] + leeway)
  200.   {
  201.     Serial.println("Robot is angled correctly");
  202.     angled = true;
  203.   }
  204.   else
  205.   {
  206.     Serial.println("Robot is angled incorrectly");
  207.     angled = false;
  208.   }
  209.  
  210.   // check if the front is centerd
  211.   if(distance[0] >= distance[1] - leeway && distance[0] <= distance[1] + leeway)
  212.   {
  213.     front = "centerd";
  214.   }
  215.   else if(distance[0] >= distance[1])
  216.   {
  217.     front = "right side";
  218.   }
  219.   else
  220.   {
  221.     front = "left side";
  222.   }
  223.   //check if the back is sented
  224.       if(distance[2] >= distance[3] - leeway && distance[2] <= distance[3] + leeway)
  225.   {
  226.     back = "centerd";
  227.   }
  228.   else if(distance[2] >= distance[3])
  229.   {
  230.     back = "right side";
  231.   }
  232.   else
  233.   {
  234.     back = "left side";
  235.   }
  236.   //servo aansturen op basis van gegevens
  237. }
  238.  
  239.  
  240.  
  241. void stop(int directionpin,int speedpin)                    //Stop
  242. {
  243.   digitalWrite(speedpin,0);
  244.   digitalWrite(directionpin,LOW);        
  245. }  
  246. void advance(int directionpin,int speedpin,int speedmotor)          //Move forward
  247. {
  248.   analogWrite (speedpin,speedmotor);      //PWM Speed Control
  249.   digitalWrite(directionpin,HIGH);  
  250. }  
  251. void back_off(int directionpin,int speedpin,int speedmotor)          //Move backward
  252. {
  253.   analogWrite (speedpin,speedmotor);      //PWM Speed Control
  254.   digitalWrite(directionpin,LOW);
  255. }
  256. void movemotor(int directionpin,int speedpin,int direct,int speedmotor)          //Move forward
  257. {
  258.   analogWrite (speedpin,speedmotor);      //PWM Speed Control
  259.   digitalWrite(directionpin,direct);  
  260. }  
  261.  
  262. /*
  263. bool limitswitchCheck(int switchpin)
  264. {
  265.   if(digitalRead(switchpin) == 1)
  266.   {
  267.     //Serial.print("limitswitch met pin ");Serial.print(switchpin);Serial.println(" is ingedrukt");
  268.     return true;
  269.   }
  270.   return false;
  271. }*/
  272.  
  273.  
  274. int upcurrentcase() //set robot to next case on the case order
  275. {
  276.   if(casesorder[casescounter] == 2)
  277.   {
  278.     nElevatorcase++;
  279.   }
  280.   else if(casesorder[casescounter] == 3)
  281.   {
  282.     nXaxiscase++;
  283.   }
  284.   casescounter++;
  285.   return casesorder[casescounter-1];
  286. }
  287.  
  288. void setcasezero() //set robot to case 0 and stops program
  289. {
  290.   casescounter = 0;
  291.   currentcase = 0;
  292.   nElevatorcase = 0;
  293.   nXaxiscase = 0;
  294. }
  295.  
  296.  
  297.  
  298.  
  299. void case0()//code for running case 0 (wait for button)
  300. {
  301.   delay(1);
  302.   //wait for button press
  303.   //check if robot is in the correct state (elevator low, turned the right way and xaxis in robot)
  304. }
  305.  
  306.  
  307.  
  308.  
  309.  
  310. void case1()//code for running case 1: drive backward to the wall
  311. {
  312.   unsigned long startMillis = millis();
  313.   unsigned long threshold1 = startMillis + 500;
  314.   unsigned long threshold2 = startMillis + 1000;
  315.   unsigned long currentMillis;  
  316.   int currentspeed;
  317.   int setspeed = 100;
  318.   while(1)
  319.   {
  320.     updateUltrasonics();
  321.     Serial.println(String(distance[0]) + " " + String(distance[1]) + " " + String(distance[2]) + " " + String(distance[3]));
  322.     steeringforward();
  323.     //naar voren rijden door motor aan te zetten (slower == true)
  324.     //bijsturen op basis van ultrasoon sensoren
  325.  
  326.     currentMillis = millis();
  327.  
  328.     if(currentMillis>= startMillis + forwardDriveTime)
  329.     {
  330.       break;
  331.     }
  332.     if(currentMillis >= threshold1)
  333.     {
  334.       if(currentMillis >= threshold2)
  335.       {
  336.         setspeed = 255;
  337.       }
  338.       else
  339.       {
  340.         setspeed = 175;
  341.       }
  342.     }
  343.     else
  344.     {
  345.       setspeed = 100;
  346.     }
  347.    
  348.     if(setspeed != currentspeed)
  349.     {
  350.       back_off(motorDrivingOutPin,motorDrivingPWMPin,setspeed);
  351.       currentspeed = setspeed;
  352.     }
  353.     // encoder uitlezen
  354.     // als encoder bepaalde waarde berijkt aangeven of trager moet of uit moet
  355.     // if(stopdriving == true) {break;}
  356.   }
  357.   stop(motorDrivingOutPin,motorDrivingPWMPin);
  358. }
  359.  
  360.  
  361.  
  362.  
  363.  
  364. void case2()//code for running case 2: change elevator position
  365. {
  366.   unsigned long checkMillis = millis();
  367.   unsigned long startMillis = millis();
  368.   unsigned long currentMillis;  
  369.  
  370.   int target = elevatorheight[nElevatorcase];
  371.   int targetcounter = (target-elevatorlowestpoint)/elevatordistance*elevatorTotalSteps;
  372.   int motordirection;
  373.   int encoderval;
  374.   bool startingUp = true;
  375.   int encoderpinlastval = digitalRead(elevatorEncoderCLKPin);
  376.   if (targetcounter > elevatorcounter)
  377.   {
  378.     motordirection = 1;
  379.   }
  380.   else
  381.   {
  382.     motordirection = 0;
  383.   }
  384.   int setspeed = 100;
  385.   int currentspeed = 0;
  386.   while(1)
  387.   {
  388.     currentMillis = millis();
  389.     if(currentMillis >= checkMillis + 100) // elke 100 milisec snelheid update
  390.     {
  391.       if(targetcounter - 5 < elevatorcounter && targetcounter + 5 > elevatorcounter)
  392.       {
  393.         break;
  394.       }
  395.       else if (targetcounter - 50 < elevatorcounter && targetcounter + 50 > elevatorcounter)
  396.       {
  397.         setspeed = 100;
  398.         startingUp = false
  399.       }
  400.       else if (targetcounter - 100 < elevatorcounter && targetcounter + 100 > elevatorcounter)
  401.       {
  402.         if (startingUp = false)
  403.         {
  404.           setspeed = 175;
  405.         }
  406.       }
  407.       else
  408.       {
  409.         /*
  410.         if (startingUp = false)
  411.         {
  412.           setspeed = 255;
  413.         }
  414.         else*/
  415.        
  416.        if(currentMillis >= startMillis + 1000)
  417.        {
  418.           setspeed = 255;
  419.          
  420.        }
  421.        else if (currentMillis >= startMillis + 500)
  422.        {
  423.          setspeed = 175;
  424.          startingUp = false;
  425.        }
  426.        else
  427.        {
  428.          setspeed = 100;
  429.        }
  430.       }
  431.       if(setspeed != currentspeed)
  432.       {
  433.         movemotor(motorElevatorOutPin ,motorElevatorPWMPin ,motordirection ,setspeed);
  434.         currentspeed = setspeed;
  435.       }
  436.     }
  437.  
  438.     encoderval = digitalRead(elevatorEncoderCLKPin);
  439.     if (encoderval != encoderpinlastval)
  440.     {
  441.       if (digitalRead(elevatorEncoderDTPin) != encoderval)
  442.       {
  443.         // Means pin A Changed first - We're Rotating Clockwise
  444.         elevatorcounter ++;
  445.         encoderpinlastval = encoderval;
  446.       }
  447.       else
  448.       {
  449.         // Otherwise B changed first and we're moving counter clock wise
  450.         elevatorcounter--;
  451.         encoderpinlastval = encoderval;
  452.       }
  453.     }
  454.   }
  455.   stop(motorElevatorOutPin ,motorElevatorPWMPin);
  456. }
  457.  
  458.  
  459.  
  460.  
  461.  
  462. void case3()//code for running case 3: move Xaxis to predetermined position
  463. {
  464.   delay(10);
  465.   posxaxis[nXaxiscase];
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472. void case4()//code for running case 4: close gripper
  473. {
  474.   while(1)
  475.   {
  476.     digitalWrite(relaiGripperInPin,HIGH);
  477.  
  478.     if(digitalRead(limitswitchPin[6]) == 1)
  479.     {
  480.       digitalWrite(relaiGripperInPin,LOW);
  481.       break;
  482.     }
  483.   }
  484. }
  485.  
  486.  
  487.  
  488.  
  489.  
  490. void case5()//code for running case 5: open gripper
  491. {
  492.   while(1)
  493.   {
  494.     digitalWrite(relaiGripperOutPin,HIGH);
  495.  
  496.     if(digitalRead(limitswitchPin[7]) == 1)
  497.     {
  498.       digitalWrite(relaiGripperOutPin,LOW);
  499.       break;
  500.     }
  501.   }
  502. }
  503.  
  504. void case6()//code for running case 6: move elevator to predetermined position
  505. {
  506.   unsigned long startMillis = millis();
  507.   unsigned long threshold1 = startMillis + 500;
  508.   unsigned long threshold2 = startMillis + 1000;
  509.   unsigned long currentMillis;  
  510.   int currentspeed;
  511.   int setspeed = 100;
  512.   while(1)
  513.   {
  514.     updateUltrasonics();
  515.     Serial.println(String(distance[0]) + " " + String(distance[1]) + " " + String(distance[2]) + " " + String(distance[3]));
  516.     steeringforward();
  517.     //naar voren rijden door motor aan te zetten (slower == true)
  518.     //bijsturen op basis van ultrasoon sensoren
  519.  
  520.     currentMillis = millis();
  521.  
  522.     if(currentMillis>= startMillis + forwardDriveTime)
  523.     {
  524.       break;
  525.     }
  526.     if(currentMillis >= threshold1)
  527.     {
  528.       if(currentMillis >= threshold2)
  529.       {
  530.         setspeed = 255;
  531.       }
  532.       else
  533.       {
  534.         setspeed = 175;
  535.       }
  536.     }
  537.     else
  538.     {
  539.       setspeed = 100;
  540.     }
  541.    
  542.     if(setspeed != currentspeed)
  543.     {
  544.       advance(motorDrivingOutPin,motorDrivingPWMPin,setspeed);
  545.       currentspeed = setspeed;
  546.     }
  547.     // encoder uitlezen
  548.     // als encoder bepaalde waarde berijkt aangeven of trager moet of uit moet
  549.     // if(stopdriving == true) {break;}
  550.   }
  551.   stop(motorDrivingOutPin,motorDrivingPWMPin);
  552. }
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559. void case7()//code for running case 5: turn Bastion 90 degrees
  560. {
  561.   unsigned long startMillis = millis();
  562.   unsigned long threshold1 = startMillis + 500;
  563.   unsigned long threshold2 = startMillis + 1000;
  564.   unsigned long currentMillis;  
  565.   int currentspeed = 0;
  566.   int setspeed = 100;
  567.  
  568.   if(digitalRead(limitswitchPin[0]) == 1)
  569.   {
  570.     while(1)
  571.     {
  572.       currentMillis = millis();
  573.       if(digitalRead(limitswitchPin[1]) == 1)
  574.       {
  575.         break;
  576.       }
  577.       if(currentMillis >= threshold1)
  578.       {
  579.         if(currentMillis >= threshold2)
  580.         {
  581.           setspeed = 255;
  582.         }
  583.         else
  584.         {
  585.           setspeed = 175;
  586.         }
  587.       }
  588.       else
  589.       {
  590.         setspeed = 100;
  591.       }
  592.      
  593.       if(setspeed != currentspeed)
  594.       {
  595.         advance(motorRotationOutPin,motorRotationPWMPin,setspeed);
  596.         currentspeed = setspeed;
  597.       }
  598.     }
  599.   }
  600.   else//if(digitalRead(switchpin[1]) == 1)
  601.   {
  602.     while(1)
  603.     {
  604.       currentMillis = millis();
  605.       if(digitalRead(limitswitchPin[0]) == 1)
  606.       {
  607.         break;
  608.       }
  609.       if(currentMillis >= threshold1)
  610.       {
  611.         if(currentMillis >= threshold2)
  612.         {
  613.           setspeed = 255;
  614.         }
  615.         else
  616.         {
  617.           setspeed = 175;
  618.         }
  619.       }
  620.       else
  621.       {
  622.         setspeed = 100;
  623.       }
  624.      
  625.       if(setspeed != currentspeed)
  626.       {
  627.         back_off(motorRotationOutPin,motorRotationPWMPin,setspeed);
  628.         currentspeed = setspeed;
  629.       }
  630.     }
  631.   }
  632.   stop(motorRotationOutPin,motorRotationPWMPin);
  633. }
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643. void case8()//code for running case 8: drive forward to next robot
  644. {
  645.  
  646.   unsigned long startMillis = millis();
  647.   unsigned long threshold1 = startMillis + 500;
  648.   unsigned long threshold2 = startMillis + 1000;
  649.   unsigned long currentMillis;
  650.   int currentspeed;
  651.   int setspeed = 100;
  652.   bool driveslow = false;
  653.   while(1)
  654.   {
  655.     updateUltrasonics();
  656.     updateInfrareds();
  657.     Serial.println(String(distance[0]) + " " + String(distance[1]) + " " + String(distance[2]) + " " + String(distance[3]));
  658.     steeringforward();
  659.     //naar voren rijden door motor aan te zetten (slower == true)
  660.     //bijsturen op basis van ultrasoon sensoren
  661.     // infrarood sensoren uitlezen
  662.     // if(stopdriving == true) {break;} (possibly add stop if encoder is far overdue)
  663.     currentMillis = millis();
  664.    
  665.     if(driveslow)
  666.     {
  667.       setspeed = 100;
  668.       if(infrared[1])
  669.       {
  670.         break;
  671.       }
  672.     }
  673.     else
  674.     {
  675.       if(currentMillis >= threshold1)
  676.       {
  677.         if(currentMillis >= threshold2)
  678.         {
  679.           setspeed = 255;
  680.           // pas kijken om zachter te rijden nadat hij volle snelheid rijdt zodat hij niet zijn beginplek gebruikt
  681.           if(infrared[0])
  682.           {
  683.             driveslow = true;
  684.           }
  685.         }
  686.         else
  687.         {
  688.           setspeed = 175;
  689.         }
  690.       }
  691.       else
  692.       {
  693.         setspeed = 100;
  694.       }
  695.     }
  696.     if(setspeed != currentspeed)
  697.     {
  698.       advance(motorDrivingOutPin,motorDrivingPWMPin,setspeed);
  699.       currentspeed = setspeed;
  700.     }
  701.     // encoder uitlezen
  702.     // als encoder bepaalde waarde berijkt aangeven of trager moet of uit moet
  703.     // if(stopdriving == true) {break;}
  704.   }
  705.   stop(motorDrivingOutPin,motorDrivingPWMPin);
  706. }
  707.  
  708. void case9()//code for running case 9: drive backwards to given location
  709. {
  710.   delay(10);
  711. }
  712.  
  713. void loop() {
  714.   switch (currentcase)
  715.   {
  716.     case 0://wait for the user input
  717.       case0();
  718.       break;
  719.     case 1://drive forward to next robot
  720.       case1();
  721.       break;
  722.     case 2://drive forward to predetermined position
  723.       case2();
  724.       break;
  725.     case 3:
  726.       case3();
  727.       break;
  728.     case 4:
  729.       case4();
  730.       break;
  731.     case 5:
  732.       case5();
  733.       break;
  734.     case 6:
  735.       case6();
  736.       break;
  737.     case 7:
  738.       case7();
  739.       break;
  740.     case 8:
  741.       case8();
  742.       break;
  743.     case 9:
  744.       case9();
  745.       break;
  746.   }
  747.  
  748.   printvalues();
  749.  
  750.   if(stopprogram)
  751.   {
  752.     setcasezero();
  753.   }
  754.   else
  755.   {
  756.     currentcase = upcurrentcase();
  757.   }
  758. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement