Go Down

Topic: help with control dc motors with ultrasonic sensor. (Read 3105 times) previous topic - next topic


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: [Select]

#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() {   
  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);


void loop() {
  digitalWrite(triggerPin, LOW);
  digitalWrite(triggerPin, HIGH);
  digitalWrite(triggerPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  float cm = microsecondsToCentimeters(duration);

  digitalWrite( pinA1, HIGH);     
  digitalWrite( pinB1, LOW);     
  digitalWrite( pinA2, LOW);     
  digitalWrite( pinB2, LOW);
  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);
    digitalWrite(whatPin, LOW);

appreciate the help

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131