Me and my friend wanted to make a sumo robot, but turns out that we don't have enough digital ports on our aduino UNO. So we set the ultrasonic sensors (hc-sr04) on the analogic ports and our L298n dual-H bridge on the digitals. we didn't know how to power the sensors cause they need 5V to work and we had to use 4 of them and the arduino has only space for one. We made a voltage reductor with a voltage regulator 7805 and a 100mfd condenser to power them with the 9v batteries. So we checked with a tester and the sensors are getting 6v and the one contected to the arduino only gets 4.5v. And we used the following code:
#define echo4 A1
#define trig4 A0
#define echo2 A3
#define trig2 A2
#define echo3 12
#define trig3 11
#define echo1 4
#define trig1 3
long duracion, duracion1, duracion2, duracion3 ;
long distancia = 0,distancia1=0, distancia2=0,distancia3=0;
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup(){
pinMode (trig1, OUTPUT);
pinMode (echo1, INPUT);//Ultrasonido
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop () {
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(10);
digitalWrite(trig1, LOW);
duracion = pulseIn(echo1, HIGH);
distancia = (duracion / 2) / 29;
digitalWrite(trig2, LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
duracion1 = pulseIn(echo2, HIGH);
distancia1 = (duracion / 2) / 29;
digitalWrite(trig3, LOW);
delayMicroseconds(2);
digitalWrite(trig3, HIGH);
delayMicroseconds(10);
digitalWrite(trig3, LOW);
duracion2 = pulseIn(echo3, HIGH);
distancia2 = (duracion / 2) / 29;
digitalWrite(trig4, LOW);
delayMicroseconds(2);
digitalWrite(trig4, HIGH);
delayMicroseconds(10);
digitalWrite(trig4, LOW);
duracion3 = pulseIn(echo4, HIGH);
distancia3 = (duracion / 2) / 29;
if (distancia <= 100|| distancia1<=100 || distancia2<=100 || distancia3<=100 || distancia >= 0 ||distancia1>=0 ||distancia2>=0||distancia3>=0){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(enA,150);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
analogWrite(enB,150);
}
if (distancia <= 10 && distancia >= 1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(enA,255);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
analogWrite(enB,255);
}
if (distancia1 <= 10 && distancia1 >= 1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(enA,180);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
analogWrite(enB,-180);
}
if (distancia2 <= 10 && distancia2 >= 1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(enA,-255);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
analogWrite(enB,-255);
}
if (distancia3 <= 10 && distancia3 >= 1){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(enA,-180);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
analogWrite(enB,180);
}
}
I don't know if it's a problem on our code or if we're making the conections wrong. we conected both our echo and our trig on the analogics and the gnd and the vgg to the condeser and voltage regulator.