Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Save New Duplicate & Edit Just Text
- #include <Servo.h> //includes the servo library
- #include <NewPing.h> //includes the ultrasonic sensor library
- Servo releaseServo; //sets up the servo
- const int rightMotorSpeed = 5;
- const int rightDir = 4;
- const int leftMotorSpeed = 3;
- const int leftDir = 2;
- const int echoPin = 5;
- const int trigPin = 6;
- const int photosynthesizer = A1;
- int status = 0; //tracks whether the robot has sensed something or not
- int photoRead;
- NewPing batSensor(trigPin, echoPin); //sets up the ultrasonic sensor
- void setup() {
- releaseServo.attach(10); //attaches the servo to port 10
- pinMode(rightMotorSpeed, OUTPUT);
- pinMode(leftMotorSpeed, OUTPUT);
- pinMode(photosynthesizer, INPUT);
- pinMode(rightDir, OUTPUT);
- pinMode(leftDir, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(trigPin, INPUT);
- delay(3000);
- releaseServo.write(180); //tells the servo to turn to 180
- delay(400);
- }
- void loop() {
- releaseServo.write(180);
- photoRead = analogRead(photosynthesizer);//reads the input on the photoresistor and saves it
- int duration = batSensor.ping_median(); //reads input on the ultrasonic sensor
- duration = batSensor.convert_in(duration);//saves input on the ultrasonic sensor
- if(duration <= 30 && duration > 0) {//if ultrasonic finds something, run at it
- digitalWrite(leftDir, LOW);
- digitalWrite(rightDir, HIGH);
- analogWrite(leftMotorSpeed, 245);
- analogWrite(rightMotorSpeed, 245);
- status = 1;
- }
- else {
- status = 0;
- }
- if(photoRead <= 675) { //if photoresistor sees dark enough stuff, move backwards
- digitalWrite(leftDir, HIGH);
- digitalWrite(rightDir, LOW);
- analogWrite(leftMotorSpeed, 245);
- analogWrite(rightMotorSpeed, 245);
- status = 1;
- }
- if(status == 0) { //if nothing is seen, turn until something is seen
- digitalWrite(leftDir, LOW);
- digitalWrite(rightDir, LOW);
- analogWrite(leftMotorSpeed, 245);
- analogWrite(rightMotorSpeed, 245);
- }
- releaseServo.write(180);
- delay(400);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement