Pages: [1]   Go Down
Author Topic: help with control dc motors with ultrasonic sensor.  (Read 1693 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to help me this code to work is for two dc motor controlled by ultrasonic sensor, if the robot detects an object ahead you turn right wing to stop motor 2 and motor 1 forward and then advancing normal motor motor 1 forward 2 forward, with the ultrasonic distance of 7 cm, should not object the robot detect normal forward progress.

the program
Code:
#define triggerPin 11
#define echoPin 10
int ledPin =  13;    // LED conectado al pin digital 13
int pinA1 = 7; // entrada Motor A 1
int pinB1 = 8; // entrada Motor B 1
int pinEN1 = 9; // entrada EN 1
int pinA2= 3 ;// entrada a motor A2
int pinB2= 4; // entrada a motor B2
int pinEN2=6;

void setup() {   
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT); 
  pinMode( pinA1, OUTPUT);     
  pinMode( pinB1, OUTPUT);     
  pinMode(pinEN1, OUTPUT);     
  digitalWrite(pinEN1, HIGH); 
  pinMode(pinA2, OUTPUT);
  pinMode(pinB2, OUTPUT);
  pinMode(pinEN2, HIGH);
  Serial.begin(9600); 

}

void loop() {
 
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  float cm = microsecondsToCentimeters(duration);
 
 if(cm=7)
 {
  digitalWrite( pinA1, HIGH);     
  digitalWrite( pinB1, LOW);     
  digitalWrite( pinA2, LOW);     
  digitalWrite( pinB2, LOW);
  delayMicroseconds(300);
 }
 else
 {
  digitalWrite( pinA1,HIGH);
  digitalWrite( pinB1,LOW);
  digitalWrite( pinA2,HIGH);
  digitalWrite( pinA2, HIGH);
 }
 

}
float microsecondsToCentimeters(long microseconds)
{
 
  float seconds = (float) microseconds / 1000000.0;
 
  float distance = seconds * 340;
 
  distance = distance / 2;
 
  distance = distance * 100;
 
  return distance;
}


void blink(int whatPin, int cuantoTiempo, int milliSecs)
{
  for (int i = 0; i < cuantoTiempo; i++)
  {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}

appreciate the help
Logged

Pages: [1]   Go Up
Jump to: