Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1.  
  2. Save  New  Duplicate & Edit  Just Text
  3. #include <Servo.h>                   //includes the servo library
  4. #include <NewPing.h>                 //includes the ultrasonic sensor library
  5.  
  6. Servo releaseServo;                  //sets up the servo
  7. const int rightMotorSpeed = 5;
  8. const int rightDir = 4;
  9. const int leftMotorSpeed = 3;
  10. const int leftDir = 2;
  11. const int echoPin = 5;
  12. const int trigPin = 6;
  13. const int photosynthesizer = A1;
  14. int status = 0;                      //tracks whether the robot has sensed something or not
  15. int photoRead;
  16. NewPing batSensor(trigPin, echoPin); //sets up the ultrasonic sensor
  17.  
  18. void setup() {
  19.   releaseServo.attach(10);           //attaches the servo to port 10
  20.   pinMode(rightMotorSpeed, OUTPUT);
  21.   pinMode(leftMotorSpeed, OUTPUT);
  22.   pinMode(photosynthesizer, INPUT);
  23.   pinMode(rightDir, OUTPUT);
  24.   pinMode(leftDir, OUTPUT);
  25.   pinMode(echoPin, INPUT);
  26.   pinMode(trigPin, INPUT);
  27.   delay(3000);
  28.   releaseServo.write(180);           //tells the servo to turn to 180
  29.   delay(400);
  30. }
  31.  
  32. void loop() {
  33.   releaseServo.write(180);
  34.   photoRead = analogRead(photosynthesizer);//reads the input on the photoresistor and saves it
  35.   int duration = batSensor.ping_median();  //reads input on the ultrasonic sensor
  36.   duration = batSensor.convert_in(duration);//saves input on the ultrasonic sensor
  37.   if(duration <= 30 && duration > 0) {//if ultrasonic finds something, run at it
  38.     digitalWrite(leftDir, LOW);
  39.     digitalWrite(rightDir, HIGH);
  40.     analogWrite(leftMotorSpeed, 245);
  41.     analogWrite(rightMotorSpeed, 245);
  42.     status = 1;
  43.   }
  44.   else {
  45.     status = 0;
  46.   }
  47.   if(photoRead <= 675) {             //if photoresistor sees dark enough stuff, move backwards
  48.     digitalWrite(leftDir, HIGH);
  49.     digitalWrite(rightDir, LOW);
  50.     analogWrite(leftMotorSpeed, 245);
  51.     analogWrite(rightMotorSpeed, 245);
  52.     status = 1;
  53.   }
  54.   if(status == 0) {                  //if nothing is seen, turn until something is seen
  55.     digitalWrite(leftDir, LOW);
  56.     digitalWrite(rightDir, LOW);
  57.     analogWrite(leftMotorSpeed, 245);
  58.     analogWrite(rightMotorSpeed, 245);
  59.   }
  60.   releaseServo.write(180);        
  61.   delay(400);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement