Pages: [1]   Go Down
Author Topic: Small Robot With HC-SR04  (Read 154 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, guys,

Well, maybe it's just a silly thing...but, here we go...
I'm programming a small robot...just like a car or something. Actually, I've got 4 servo motors and I need to control them.
I also have 2 ultrasonic sensors. One of them is straight ahead the car and the other behind it. So, I just want my little car runs to one direction and if it finds a wall at least 10.50 cm, it goes to the opposite. The same happens if the other sensor "sees" a wall (or any object) as near as 10.50cm. The code is attached.

Is there someone to help me with that?

* sketch_jun04_CORRETO.ino (1.46 KB - downloaded 11 times.)

Offline Offline
God Member
Karma: 17
Posts: 673
Hey bien!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok let's look at that code:

#include <Ultrasonic.h>
#define PINO_TRIGGER  12
#define PINO_ECHO     13
#define PINO_TRIGGER2 6
#define PINO_ECHO2    7

Ultrasonic ultrasonic(PINO_TRIGGER, PINO_ECHO);
Ultrasonic ultrasonic2(PINO_TRIGGER2, PINO_ECHO2);
int ENA = 3;
int ENB = 5;
int IN1 = 8;
int IN2 = 9;
int IN3 = 11;
int IN4 = 10;
int LED = 2;

void setup()
  pinMode(LED, OUTPUT);

void loop()
  float cmMsec, cmNsec;
  long microsec = ultrasonic.timing();
  long microsec2 = ultrasonic2.timing();
  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  cmNsec = ultrasonic2.convert(microsec2, Ultrasonic::CM);
  Serial.print("Cent: ");
  Serial.print("Cent: ");
 if(cmMsec >= 10.50){
  analogWrite(ENA, 100);// motor speed 
  digitalWrite(IN1,HIGH);// rotate forward
  analogWrite(ENB, 100);// motor speed 
  digitalWrite(IN3,HIGH);// rotate forward
   } else if(cmNsec >= 10.50){
 analogWrite(ENA, 100);
 digitalWrite(IN1,LOW);// rotate reverse
 analogWrite(ENB, 100);
 digitalWrite(IN3,LOW);// rotate forward
if(cmMsec <= 10.50 && cmNsec <= 10.50) {
 analogWrite(ENA, 0);
 digitalWrite(IN1,LOW);// rotate reverse
 analogWrite(ENB, 0);
 digitalWrite(IN3,LOW);// rotate forward
 digitalWrite(IN4, LOW);}

Can you describe the problems?

As we fight our way northward into the great unknown, only that one thing remains certain...

Pages: [1]   Go Up
Jump to: