FulligJimmy

VacuumCode_2:whileloop

Jan 7th, 2018
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.65 KB | None | 0 0
  1. //Code: VaccumCode
  2. //Version: 2.0.1
  3. //Author: Cesar Nieto refer to [email protected]
  4. //Last change: 19/05/2017
  5. //Changes: Documentation added
  6. #include <math.h>
  7. ////////////PINS////////////////
  8. //Distance Analog Sensors (Sharp)
  9. const int SD1 = 0; //left front sensor
  10. const int SD2 = 1; //right front sensor
  11. const int SD3 = 2; //left side sensor
  12. const int SD4 = 3; //right side sensor
  13.  
  14. //Battery Voltage input
  15. const int battery = 4;
  16.  
  17. //IndicatorLED
  18. const int led = 13;
  19.  
  20. //Fan output
  21. const int fanmotor =  12;      // the number of the LED pin
  22.  
  23. // Motor1 Right
  24. const int motor1Pin1 = 3;
  25. const int motor1Pin2 = 5;
  26.  
  27. // Motor2 Left
  28. const int motor2Pin1 = 6;
  29. const int motor2Pin2 = 9;
  30.  
  31. //Bumper
  32. const int bumper1 = 10;
  33. const int bumper2 = 11;
  34. const int bumper3 = 7;
  35. const int bumper4 = 8;
  36.  
  37. ///////////////Constants////////////////
  38. const float voltageBatCharged = 12.68; // Voltage measured when battery fully charged //Change this
  39. //PWM for the micro metal motors
  40. const int pwmMax = 160;// for 12V  pwmMAx = 170, for 10V output  pwmMax = 140
  41. const int pwmMin = 70;;// for 9V  pwmMin = 128
  42. //Mínimun distance of the sensor
  43. const int minSharp = 30;
  44.  
  45. // Variables will change:
  46. int bumperState = 0;  // variable for reading the pushbutton status
  47. boolean control = true;
  48. int counter = 0; //   Prevents from being stuck
  49. int bumperState2 = 0; // variable for reading the pushbutton status
  50. boolean control2 = true;
  51. int counter2 = 0; // Prevents from being stuck
  52. int bumperState3 = 0; // variable for reading the pushbutton status
  53. boolean control3 = true;
  54. int counter3 = 0; // Prevents from being stuck
  55. int bumperState4 = 0; // variable for reading the pushbutton status
  56. boolean control4 = true;
  57. int counter4 = 0; // Prevents from being stuck
  58.  
  59.  
  60. //////////////CODE/////////////
  61. void setup() {
  62.   //Initialize outputs and inputs
  63.   //Fan motor as output
  64.   pinMode(fanmotor, OUTPUT);
  65.   //Motor1
  66.   pinMode(motor1Pin1, OUTPUT);
  67.   pinMode(motor1Pin2, OUTPUT);
  68.   //Motor2
  69.   pinMode(motor2Pin1, OUTPUT);
  70.   pinMode(motor2Pin2, OUTPUT);
  71.   //LED
  72.   pinMode(led, OUTPUT);
  73.   //INPUTS
  74.   // initialize the pushbutton inputs
  75.   //Bumper
  76.   pinMode(bumper1, INPUT_PULLUP);
  77.   pinMode(bumper2, INPUT_PULLUP);
  78.   pinMode(bumper3, INPUT_PULLUP);
  79.   pinMode(bumper4, INPUT_PULLUP);
  80.   //Sensor
  81.   pinMode(SD1, INPUT);
  82.   pinMode(SD2, INPUT);
  83.   pinMode(SD3, INPUT);
  84.   pinMode(SD4, INPUT);
  85.   //Batt
  86.   pinMode(battery, INPUT);
  87.   // Initialize serial
  88.   Serial.begin(9600);    
  89.   ///////////////////////////////Wait////////////////////////////////////////
  90.   //Wait about 5 s and initialize fan if voltage ok
  91.   waitBlinking(5,1); //5 seconds at 1 Hz
  92.   //Crank (initialize the fan because the voltage drops when cranking)
  93.   if(readBattery(battery)>12.1){
  94.     digitalWrite(fanmotor, HIGH); //Turn the Fan ON
  95.     delay(1000); //For 1000ms
  96.   }
  97.   else {
  98.     //do nothing Convention
  99.     }
  100. }
  101. //////////Functions To Use //////////
  102. void waitBlinking(int n, int frequency){
  103.   //blink for n seconds at frequency hz
  104.   for (int i=1; i <= n; i++){
  105.     for(int j=1; j<=frequency; j++){
  106.       digitalWrite(led, HIGH);  
  107.       delay((1000/frequency)/2);   //Half time on            
  108.       digitalWrite(led, LOW);  
  109.       delay((1000/frequency)/2);   //Half time off
  110.     }
  111.    }
  112. }
  113. double sdSHARP(int Sensor){
  114.   //Returns the distance in cm
  115.   double dist = pow(analogRead(Sensor), -0.857); // x to power of y
  116.   return (dist * 1167.9);
  117. }
  118. void forwardMotors(int moveTime){  
  119.   //Manipulate direction according the desired movement of the motors
  120.    analogWrite(motor1Pin1, pwmMin);
  121.    analogWrite(motor1Pin2, 0); //PWM value wher 0 = 0% and 255 = 100%
  122.    analogWrite(motor2Pin1, pwmMin);
  123.    analogWrite(motor2Pin2, 0);
  124.    delay(moveTime);
  125. }
  126. void rightMotors(int moveTime){
  127.    analogWrite(motor1Pin1, 0);
  128.    analogWrite(motor1Pin2, pwmMin);
  129.    analogWrite(motor2Pin1, pwmMin);
  130.    analogWrite(motor2Pin2, 0);
  131.  
  132.    delay(moveTime);
  133. }
  134. void leftMotors(int moveTime){
  135.    analogWrite(motor1Pin1, pwmMin);
  136.    analogWrite(motor1Pin2, 0);
  137.    analogWrite(motor2Pin1, 0);
  138.    analogWrite(motor2Pin2, pwmMin+20);
  139.    delay(moveTime);
  140. }
  141. void backwardMotors(int moveTime){
  142.    analogWrite(motor1Pin1, 0);
  143.    analogWrite(motor1Pin2, pwmMin+20);
  144.    analogWrite(motor2Pin1, 0);
  145.    analogWrite(motor2Pin2, pwmMin+20);
  146.    delay(moveTime);
  147. }
  148. void stopMotors(){
  149.    analogWrite(motor1Pin1, 0);
  150.    analogWrite(motor1Pin2, 0);
  151.    analogWrite(motor2Pin1, 0);
  152.    analogWrite(motor2Pin2, 0);
  153. }
  154. float  readBattery(int input){
  155.   int readInput;
  156.   float voltage;
  157.   readInput = analogRead(input);
  158.   voltage = (((readInput*4.9)/1000)*voltageBatCharged ) / 5; // resolution of analog input = 4.9mV per Voltage
  159.   Serial.print(" Battery= ");
  160.   Serial.print(voltage);
  161.   return voltage;
  162.   }
  163. void batteryControl(int input){
  164.   //Turn everything off in case the battery is low
  165.   float v_battery;
  166.   v_battery = readBattery(input);
  167.   if(v_battery<=11.6){ //battery limit of discharge, Don't put this limit lower than  11.1V or you can kill the battery
  168.     control = false;
  169.     }
  170.   else {
  171.     //Do nothing Convention
  172.     }
  173. }
  174. /////////////////////////////////////////////////MAIN CODE//////////////////////////////
  175. void loop(){
  176.   /*  
  177.   Serial.print("SD1= ");
  178.   Serial.print(sdSHARP(SD1));
  179.   Serial.println();
  180.   Serial.print("  SD2= ");
  181.   Serial.print(sdSHARP(SD2));
  182.   Serial.println();
  183.   delay(200);*/
  184.   bumperState = digitalRead(bumper1);
  185.   bumperState2 = digitalRead(bumper2);
  186.   bumperState3 = digitalRead(bumper3);
  187.   bumperState4 = digitalRead(bumper4);
  188.   //Keep the control of the battery automatically turn the fan off
  189.   //If control = true the battery level is ok, otherwise the battery is low.
  190.   batteryControl(battery); //modifies the variable control of the battery is low
  191.  
  192.   if (control){
  193.     while (!sdSHARP(SD1)<=4.3 && !sdSHARP(SD2)<=4.3 && !bumperState==0 && !bumperState2==0 && !bumperState3==0 && !bumperState4==0);               {
  194.       forwardMotors(5100);
  195.       backwardMotors(300);
  196.       leftMotors(300);
  197.       forwardMotors (4800);
  198.       backwardMotors(400);
  199.       rightMotors(350);
  200.       forwardMotors(5000);
  201.       backwardMotors(280);
  202.       rightMotors(320);
  203.       forwardMotors (5200);
  204.       backwardMotors(410);
  205.       rightMotors(350);
  206.       forwardMotors(4500);
  207.       backwardMotors(310);
  208.       leftMotors(320);
  209.       forwardMotors (4800);
  210.       backwardMotors(400);
  211.       rightMotors(350);
  212.       forwardMotors (300);
  213.     }
  214.      
  215.      
  216.      
  217.    
  218.     digitalWrite(led, HIGH);
  219.    
  220.        if (sdSHARP(SD1)<=4.3 ){
  221.       //If the distance between an object and the left front sensor is less than 4.3 cm or the bumper hits, it will move to the left
  222.       if (counter ==2){ // prevent of being stuck on corners
  223.         counter = 0;
  224.         }
  225.       else {
  226.         //Do nothing Convention
  227.     }
  228.    
  229.       forwardMotors(100); // approach a bit
  230.       backwardMotors(500); // backward delay of 500ms
  231.       leftMotors(300);
  232.       counter = counter + 2;
  233.       Serial.print("  Turn Left ");
  234.       }
  235.     else if (sdSHARP(SD2)<=4.3){
  236.       //If the distance between an object and the right front sensor is less than 4.3 cm, it will move to the right
  237.       if (counter ==1){
  238.         counter = 0;
  239.         }
  240.       else{
  241.         //Do nothing Convention
  242.       }
  243.       forwardMotors(100);
  244.       backwardMotors(500);
  245.       rightMotors(300);
  246.       counter++;
  247.       Serial.print("  Turn Right");
  248.       }
  249.     else if (bumperState==0){//BUMPER1
  250.       counter = 0;
  251.       backwardMotors(1500); //backward delay of 500ms
  252.       leftMotors(500);
  253.       Serial.print("  Turn Left ");
  254.       }
  255.     else if (bumperState2==0){//BUMPER2
  256.       counter2 = 0;
  257.       backwardMotors(1500); //backward delay of 500ms
  258.       rightMotors(400);
  259.       Serial.print("  Turn Right ");
  260.       }
  261.      
  262.     else if (bumperState3==0){//BUMPER3
  263.       counter3 = 0;
  264.       backwardMotors(1500); //backward delay of 500ms
  265.       leftMotors(500);
  266.       Serial.print("  Turn Left ");
  267.       }
  268.     else if (bumperState4==0){//BUMPER4
  269.       counter = 4;
  270.       backwardMotors(1500); //backward delay of 500ms
  271.       rightMotors(400);
  272.       Serial.print("  Turn Right ");
  273.       }
  274.     else {
  275.       if(counter==3){ //Corner
  276.         leftMotors(1000);
  277.         counter = 0;
  278.         }
  279.       else {
  280.         forwardMotors(0);
  281.        
  282.       }
  283.       Serial.print("  Move Forward");
  284.       }
  285.          
  286.   }
  287.   else if (!control){
  288.     //If the battery is low, turn everything off
  289.     digitalWrite(fanmotor, LOW); //Turn the Fan OFF
  290.     stopMotors();
  291.     Serial.print(" Low Battery! ");
  292.     Serial.println();
  293.     waitBlinking(1,3);  //blink as warning 3hz in a loop
  294.     }
  295.   Serial.println();
  296.   }
Advertisement
Add Comment
Please, Sign In to add comment