Go Down

Topic: Ultrasonic Sensors Controlled Motors (Read 224 times) previous topic - next topic

sixuannn

Hi, I'm having some troubles with this. My intentions are for the motors are supposed to continuously until it detects something within the range of 100cm. I got a diagram with the code, which in theory should work (when I run the code it uploads without errors). Could someone tell me if it is supposed to work or not? Because then I could troubleshoot for loose wiring/faulty components etc. If any kind soul could help, it would be amazing! Thank you
image of my wiring & diagram: https://imgur.com/a/w5Pul
my code:

define echoPin 7 // Echo Pin

define trigPin 8 // Trigger Pin

define relayPin 13 // Relay Pin

int objectRange = 20; // Object range needed in cm
long duration; long distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
}
void loop() {
getDistance();
Serial.println("Distance = " + String(distance) + "cm"); //remove this serial print if not essential, it will speed up the response.
if (distance > objectRange) {
Serial.println("Relay On");
digitalWrite(relayPin, HIGH);
}
else { Serial.println("Relay Off");
digitalWrite(relayPin, LOW); }
delay(50);
}
void getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
}

slipstick

Without knowing what any of the components are it's impossible to say much. What motors, what relay, what are the things that look like transistors/MOSFETs and how connected, what diodes, what power source and why aren't the grounds connected?

Your text says 100cm, code says 20. Dividing an integer by 58.2 may not give the results you expect.

Steve

sixuannn

#2
Apr 11, 2018, 07:19 pm Last Edit: Apr 11, 2018, 07:23 pm by sixuannn
Thank you Steve for replying! Apologies for the text, I edited it last minute to test it out from a smaller distance and forgot to change it back! The mofset looking things are voltage regulators (LM7809 and LM7806, for different voltage of motors).
I'm not too sure by what you meant that the grounds aren't connected? I have not connected in the motors if that is what you mean! The relay is 12v, while I have 3 9v motors and 1 6 v motor. As for the diodes, I'll have to double check.

Go Up