Check out the tutorial here:
const int trigPin = 13;
const int echoPin = 12;
int motorLpin1=4;
int motorLpin2=5;
int motorRpin1=6;
int motorRpin2=7;
int motorLpwm=3;
int motorRpwm=11;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorLpin1,OUTPUT);
pinMode(motorLpin2,OUTPUT);
pinMode(motorRpin1,OUTPUT);
pinMode(motorRpin2,OUTPUT);
pinMode(motorLpwm,OUTPUT);
pinMode(motorRpwm,OUTPUT);
}
long duration, distance;
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
if(distance<25)
{
digitalWrite(motorLpin1, LOW);
digitalWrite(motorLpin2, HIGH);
digitalWrite(motorRpin1, HIGH);
digitalWrite(motorRpin2, LOW);
analogWrite(motorLpwm,50);
analogWrite(motorRpwm,50);
delay(1000);
}
else
{
digitalWrite(motorLpin1, HIGH);
digitalWrite(motorLpin2, LOW);
digitalWrite(motorRpin1, HIGH);
digitalWrite(motorRpin2, LOW);
analogWrite(motorLpwm,50);
analogWrite(motorRpwm,50);
}
}
const int trigPin = 13;
const int echoPin = 12;
int motorLpin1=4;
int motorLpin2=5;
int motorRpin1=6;
int motorRpin2=7;
int motorLpwm=3;
int motorRpwm=11;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorLpin1,OUTPUT);
pinMode(motorLpin2,OUTPUT);
pinMode(motorRpin1,OUTPUT);
pinMode(motorRpin2,OUTPUT);
pinMode(motorLpwm,OUTPUT);
pinMode(motorRpwm,OUTPUT);
}
long duration, distance;
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
if(distance<25)
{
digitalWrite(motorLpin1, LOW);
digitalWrite(motorLpin2, HIGH);
digitalWrite(motorRpin1, HIGH);
digitalWrite(motorRpin2, LOW);
analogWrite(motorLpwm,50);
analogWrite(motorRpwm,50);
delay(1000);
}
else
{
digitalWrite(motorLpin1, HIGH);
digitalWrite(motorLpin2, LOW);
digitalWrite(motorRpin1, HIGH);
digitalWrite(motorRpin2, LOW);
analogWrite(motorLpwm,50);
analogWrite(motorRpwm,50);
}
}
You are great
ReplyDeletewhen i uploded the code the motor of the car kept making a buzzes sound
ReplyDelete