So I got one problem here. I'm using the Arduino Uno and connected some things for a working self-driving car.
We got 2x servos (SG90, tower pro 9g Micro servo)
1x HC-SR04 supersonic sensor
1x HW307 mechanical relay
and at last 1x 5v DC engine
When connected (we're using the relay to shut the engine from it's power) it keeps spinning while distance is under the minimum distance (in cm). I tested the supersonic sensor, but it's working fine and producing right values (tested in the serial monitor).
Maybe someone of you can find a idea, how to fix that!
Thanks for looking into this
#include <Servo.h>
Servo servolenkung, servosonar; //Servos
int iRelais = 2; // Relay
int iEcho = 3; // Echo-Pin
int iTrigger = 4; // Trigger-Pin
int iMinDis = 8; //minimum distance in cm
int iDistance = 0;
int sonar() {
int iTime;
digitalWrite(iTrigger, LOW);
delay(5);
digitalWrite(iTrigger, HIGH);
delay(10);
digitalWrite(iTrigger, LOW);
iTime = pulseIn(iEcho, HIGH);
iDistance = (iTime/2) * 0.03432; //calculating distance in cm
return iDistance;
}
void setup() {
servolenkung.attach(5);
servosonar.attach(6);
pinMode(iRelais, OUTPUT);
pinMode(iTrigger, OUTPUT);
pinMode(iEcho, INPUT);
}
void loop() {
//Setting this to high shuts the engine from it's power, meaning it does not drive now
digitalWrite(iRelais, HIGH);
sonar();
if (iDistance < iMinDis) {
digitalWrite(iRelais, HIGH);
servosonar.write(165);
servolenkung.write(1);
sonar(); //calling the sonar method
if (iDistance < iMinDis){
servosonar.write(30); //servo under the supersonic sensor to the right side
servolenkung.write(165); //spinning the wheels
sonar();
//distance-check
if (iDistance < iMinDis){
//lol here we are implementing a error-led
}
}
}
//setting the voltage of the relais to low, making the engine spin
digitalWrite(iRelais, LOW);
delay(1500);
//servos back to 90
servolenkung.write(90);
servosonar.write(90);
}