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)
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()
{
Serial.begin(9600);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
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(cmMsec);
Serial.print("Cent: ");
Serial.println(cmNsec);
if(cmMsec >= 10.50){
analogWrite(ENA, 100);// motor speed
digitalWrite(IN1,HIGH);// rotate forward
digitalWrite(IN2,LOW);
analogWrite(ENB, 100);// motor speed
digitalWrite(IN3,HIGH);// rotate forward
digitalWrite(IN4,LOW);
} else if(cmNsec >= 10.50){
analogWrite(ENA, 100);
digitalWrite(IN1,LOW);// rotate reverse
digitalWrite(IN2,HIGH);
analogWrite(ENB, 100);
digitalWrite(IN3,LOW);// rotate forward
digitalWrite(IN4,HIGH);
}
if(cmMsec <= 10.50 && cmNsec <= 10.50) {
analogWrite(ENA, 0);
digitalWrite(IN1,LOW);// rotate reverse
digitalWrite(IN2,LOW);
analogWrite(ENB, 0);
digitalWrite(IN3,LOW);// rotate forward
digitalWrite(IN4, LOW);}
}
Can you describe the problems?