SHARE
TWEET

Untitled

a guest Jun 14th, 2018 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //LUMIERE (LED)
  2. //int ledPin = 13;                // LED connected to digital pin 13
  3.  
  4. //MOTEUR
  5. const int AVANT = 10; // Constante pour la broche 10
  6. const int ARRIERE = 9; // Constante pour la broche 9
  7.  
  8. int maxSpeed = 0;
  9. int speedAmount = 5;
  10.  
  11. //SONAR
  12. #define CM 1      //Centimeter
  13. #define INC 0     //Inch
  14. #define TP 7      //Trig_pin
  15. #define EP 8      //Echo_pin
  16.  
  17. void stop() //Stop
  18. {
  19.   if (maxSpeed == 255) {
  20.     speedAmount = -speedAmount ;
  21.   }
  22. }
  23. void advance(char a) //Move forward
  24. {
  25.   analogWrite(AVANT, a);    
  26.   maxSpeed = maxSpeed + speedAmount;
  27. }
  28. void back_off (char a) //Move backward
  29. {
  30.   analogWrite(ARRIERE, a);    
  31.   maxSpeed = maxSpeed + speedAmount;
  32. }
  33.  
  34. void setup(){
  35.   //LUMIERE (LED)
  36.   //pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  37.  
  38.   //MOTEUR
  39.   pinMode(9, OUTPUT);
  40.   pinMode(10, OUTPUT);
  41.  
  42.   //SONAR
  43.   pinMode(TP,OUTPUT);       // set TP output for trigger  
  44.   pinMode(EP,INPUT);        // set EP input for echo
  45.  
  46.   /*Serial.begin(9600);      // init serial 9600
  47.   Serial.println("---------------------------------HC-SR04 Test---------------------------------");*/
  48. }
  49.  
  50. void loop(){  
  51.   //SONAR
  52.   long microseconds = TP_init();
  53.   long distance_cm = Distance(microseconds, CM);
  54.   /*Serial.print("Distance_CM = ");
  55.   Serial.println(distance_cm);*/
  56.   //delay(1000);
  57.  
  58.   if(distance_cm <= 10){
  59.   /*  //LUMIERE
  60.       digitalWrite(ledPin, HIGH);   // sets the LED on
  61.       delay(50);                  // waits for a second
  62.       digitalWrite(ledPin, LOW);    // sets the LED off
  63.       delay(50);                  // waits for a second*/
  64.    
  65.    //MOTEUR
  66.       /*stop();
  67.       delay(1000);*/
  68.       back_off (maxSpeed);
  69.       delay(2000);
  70.       /*stop();*/
  71.     }
  72.   else{  
  73.     //digitalWrite(ledPin, LOW);
  74.     advance(maxSpeed);
  75.     //delay(30);
  76.   }
  77. }
  78. //SONAR
  79. long Distance(long time, int flag)
  80. {
  81.   long distance;
  82.   if(flag)
  83.     distance = time /29 / 2  ;     // Distance_CM  = ((Duration of high level)*(Sonic :340m/s))/2
  84.                                    //              = ((Duration of high level)*(Sonic :0.034 cm/us))/2
  85.                                    //              = ((Duration of high level)/(Sonic :29.4 cm/us))/2
  86.   else
  87.     distance = time / 74 / 2;      // INC
  88.   return distance;
  89. }
  90.  
  91. //SONAR
  92. long TP_init()
  93. {                    
  94.   digitalWrite(TP, LOW);                    
  95.   delayMicroseconds(2);
  96.   digitalWrite(TP, HIGH);                 // pull the Trig pin to high level for more than 10us impulse
  97.   delayMicroseconds(10);
  98.   digitalWrite(TP, LOW);
  99.   long microseconds = pulseIn(EP,HIGH);   // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
  100.   return microseconds;                    // return microseconds
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top