Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define trig 7
- #define echo 8
- #define enaPin 9
- #define motor1 10
- #define motor2 11
- #define redLed 5
- long distance, duration;
- void setup() {
- pinMode(trig, OUTPUT);
- pinMode(echo, INPUT);
- pinMode(enaPin, OUTPUT);
- pinMode(motor1, OUTPUT);
- pinMode(motor2, OUTPUT);
- pinMode(redLed, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- digitalWrite(trig, LOW);
- delayMicroseconds(2);
- digitalWrite(trig, HIGH);
- delayMicroseconds(10);
- digitalWrite(trig, LOW);
- duration = pulseIn(echo, HIGH);
- distance = duration / 29 / 2;
- if (distance > 50) {
- analogWrite(enaPin, 250);
- digitalWrite(motor1, HIGH);
- digitalWrite(motor2, LOW);
- digitalWrite(redLed, LOW);
- }
- else {
- digitalWrite(redLed, HIGH);
- for (int Speed = 250; Speed >= 0; Speed = Speed - 10) {
- analogWrite(enaPin, Speed);
- digitalWrite(motor1, HIGH);
- digitalWrite(motor2, LOW);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement