SHARE
TWEET

Untitled

a guest Oct 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Compilation of robotics programs for use on Instructables. Thanks for everyone that contributed!
  2.  
  3. #define trigPin 7 #define echoPin 6 //#define led 11 //#define led2 10
  4.  
  5. int pinI1=8;//define I1 interface int pinI2=11;//define I2 interface int speedpinA=9;//enable motor A int pinI3=12;//define I3 interface int pinI4=13;//define I4 interface int speedpinB=10;//enable motor B
  6.  
  7. void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // pinMode(led, OUTPUT);// // pinMode(led2, OUTPUT);//
  8.  
  9. pinMode(pinI1,OUTPUT); pinMode(pinI2,OUTPUT); pinMode(speedpinA,OUTPUT); pinMode(pinI3,OUTPUT); pinMode(pinI4,OUTPUT); pinMode(speedpinB,OUTPUT); }
  10.  
  11. void loop() { long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); - Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 40) { // This is where the LED On/Off happens analogWrite(speedpinA,0);//Slows speed of motors to zero analogWrite(speedpinB,0);//Slows speed of motors to zero digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise digitalWrite(pinI3,HIGH); digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise digitalWrite(pinI1,HIGH); // digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off // digitalWrite(led2,LOW); } else { analogWrite(speedpinA,240);//Drive Forward analogWrite(speedpinB,240);//Drive Forward digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise digitalWrite(pinI3,HIGH); digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise digitalWrite(pinI1,HIGH); // digitalWrite(led,LOW); // digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ analogWrite(speedpinA,240);//Drive Forward analogWrite(speedpinB,240);//Drive Forward digitalWrite(pinI4,LOW);//turn DC Motor B move clockwise digitalWrite(pinI3,HIGH); digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise digitalWrite(pinI1,HIGH); Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }
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