Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. //Feel free to use this code.
  2. //Please be respectful by acknowledging the author in the code if you use or modify it.
  3. //Original Author: Bruce Allen
  4. //Modified by: Jason Edwards
  5. //Date: 1/12/10
  6.  
  7.  
  8. const int DigiIR = 2; //This is where the IR pin is located (const)
  9. // Following lines of code set the speed(pwm) and the direction(dir)
  10. const int pwm_a=3;
  11. const int pwm_b = 11;
  12. const int dir_a = 12;  
  13. const int dir_b = 13;
  14. const long SamplePoint = 489/10000;//this constant is variable that adjusts the range accordingly
  15. //variables needed to store values
  16. long pulse, inches, cm;
  17. int object = 0; // variable to store digital value
  18. int analogRange = 0;
  19. long range, IRinches, IRcm;
  20.  
  21. void setup() {
  22.  
  23.   pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  24.   pinMode(pwm_b, OUTPUT);
  25.   pinMode(dir_a, OUTPUT);
  26.   pinMode(dir_b, OUTPUT);
  27.  
  28.   //This opens up a serial connection to shoot the results back to the PC console
  29.   Serial.begin(9600);
  30.  
  31.  
  32. }
  33.  
  34. void loop() {
  35.  
  36.  
  37.   pinMode(DigiIR, INPUT);
  38.  
  39.     //Used to read in the pulse that is being sent by the MaxSonar device.
  40.   //Pulse Width representation with a scale factor of 147 uS per Inch.
  41.  
  42.   range = analogRead(analogRange);
  43.   object = digitalRead(DigiIR);
  44.   pulse = pulseIn(pwPin, HIGH);
  45.   analogWrite(pwm_a, 200);    
  46.   analogWrite(pwm_b, 200);  
  47.  
  48.   if(object == HIGH){
  49.     Serial.println("Path Clear");
  50.     digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
  51.     digitalWrite(dir_b, HIGH);
  52.   }
  53.   else{
  54.      Serial.println("Object Detected");
  55.      digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
  56.      digitalWrite(dir_b, LOW);      
  57.   }
  58.   delay(500);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement