Ultra sonic sensor code issue

Hey guys,

I’ve tried to code an ultra sonic sensor for the first time.

However when it gets to the section of the code where i want the sensor to do its thing, it just totally bypasses that section.

I am running continuous rotation servos. I want the sensor to stop 2 of these motors when a distance from a wall of 250mm is reached.

Below is the part of the code i am having issues with. Any help is greatly appreciated

#include <Servo.h>

Servo servoLW; //left wheel servo
Servo servoRW; // right wheel servo
Servo servoSL; // scissor lift servo
Servo servoA1; // Left arm
Servo servoA2; //Right arm

// Ultra Sonic Setup
unsigned int duration, mm;
#define ping 2
#define echo 3

void setup() {
servoLW.attach(9); // Attaches servoLW to pin 9
servoRW.attach(10); // Attach servoRW to pin 10
servoSL.attach(11); // Attach servoSL to pin 11
servoA1.attach(12); // Attach servo A1 to pin 12
servoA2.attach(13); // Attach servo A2 to pin 13

servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(0);servoA2.write(0);
do{ mm = ultraRead(); } while (mm == 250);

void loop() {

// Function that reads the distance
int ultraRead(){
pinMode ( ping, OUTPUT );
digitalWrite( ping, LOW );
delayMicroseconds(2);
digitalWrite( ping, HIGH );
delayMicroseconds(5);
digitalWrite( ping, LOW);
pinMode (echo, INPUT);
duration = pulseIn(echo, HIGH);
mm = duration / 580;
}

Thanks for your help!!

You appear to have defined a function inside another function.
The compiler won't like that, and will have told you so.

You could have told us that, instead of just saying "issues", which is about as much use as telling us it isn't working.

And please remember code tags

The compiler is happy with it.

Do you have ant suggestions for how else i could code this for the outcome i am after?

What compiler is happy with this?

void loop()  {

// Function that reads the distance
int ultraRead(){
do{ mm = ultraRead(); } while (mm == 250);

Can you explain what your intention here is?

Does that intention appear a very likely outcome?

I'm obviously a novice at this. I have been given this code from a friend who seems to have as much idea as i do.

i'm not sure how to approach my problem.

How should i call the function when i need it?

The code you posted is incorrect and can never compile.
Maybe it’s not all your code and you thought you’d edit it to spare us the gory details, but that means we can’t replicate your code.

So post your code, in code tags.
And post your observations - don’t just hand-wave and say it has issues.

Don’t know what you mean by code tag, here’s the whole thing.

#include <Servo.h>

Servo servoLW; //left wheel servo
Servo servoRW; // right wheel servo
Servo servoSL; // scissor lift servo
Servo servoA1; // Left arm
Servo servoA2; //Right arm

// Ultra Sonic Setup
unsigned int duration, mm;
#define ping 2
#define echo 3

void setup() {
servoLW.attach(9); // Attaches servoLW to pin 9
servoRW.attach(10); // Attach servoRW to pin 10
servoSL.attach(11); // Attach servoSL to pin 11
servoA1.attach(12); // Attach servo A1 to pin 12
servoA2.attach(13); // Attach servo A2 to pin 13

servoLW.write(90);servoRW.write(90);servoSL.write(90); servoA1.write(90); servoA2.write(90);// Initial delay for set up
delay(2000);

servoLW.write(180);servoRW.write(0);servoSL.write(90); servoA1.write(90);servoA2.write(90); //Move 1: Move into position for making left turn
delay(1900);

servoLW.write(90);servoRW.write(90);servoSL.write(90); servoA1.write(90);servoA2.write(90); // Pause to ensure traction for the turn
delay(300);

servoLW.write(90);servoRW.write(0);servoSL.write(90);servoA1.write(90);servoA2.write(90);// Make left turn 90 degrees
delay(1690);

servoLW.write(90);servoRW.write(90);servoSL.write(90); servoA1.write(90);servoA2.write(90);//Pause for moment so both wheels at same speed for take off
delay(300);

servoLW.write(180);servoRW.write(0);servoSL.write(0);servoA1.write(90);servoA2.write(90);//Move into position over pole/have assumed that scissor
delay(4000); //lift is high enough for arms to clear the pole, this may
//not turn out to be correct. Start scissor lift
servoLW.write(90);servoRW.write(90);servoSL.write(0); servoA1.write(90);servoA2.write(90);//Lift up to get on pole
delay(28000);

servoLW.write(180);servoRW.write(0);servoSL.write(90);servoA1.write(90);servoA2.write(90);//Move into position over pole
delay(1550);

servoLW.write(90);servoRW.write(90);servoSL.write(180); servoA1.write(90);servoA2.write(90);//Retract onto pole
delay(18000);

servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);//Delay for scissor lift to turn in other direction
delay(300);

// Transverse
servoLW.write(90);servoRW.write(90);servoSL.write(0);servoA1.write(0);servoA2.write(0);//Start arm 1/2 for crossing/start extending scissor lift in prep
do{ mm = ultraRead(); } while (mm == 250); //for getting off pole at other side

servoLW.write(90);servoRW.write(90);servoSL.write(0); servoA1.write(90);servoA2.write(90);//Arm 1 stops/ scissor lift extends to ground and lifts high
delay(29000); //enough to ready the device for reversing

servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);//Delay for scissor lift to turn in other direction
delay(300);

servoLW.write(0);servoRW.write(180);servoSL.write(180);servoA1.write(90);servoA2.write(90);//Back device up/ retract scissor lift to get under pole
delay(500);

servoLW.write(90);servoRW.write(90);servoSL.write(180); servoA1.write(90);servoA2.write(90);//Keep retracting scissor lift until clearance achieved
delay(42000);

servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);//
delay(300);

servoLW.write(180);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);//Make 30 degree turn
delay(300);

servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);//
delay(300);

servoLW.write(180);servoRW.write(0);servoSL.write(90);servoA1.write(90);servoA2.write(90);//Drive forward positioning device adjacent to finish zone
delay(7200);

servoLW.write(90);servoRW.write(90);servoSL.write(90); servoA1.write(90);servoA2.write(90);//Pause to ensure traction for the turn
delay(300);

servoLW.write(90);servoRW.write(90);servoSL.write(180); servoA1.write(90);servoA2.write(90);//Retract onto pole
delay(50000);
}

void loop() {
servoLW.write(90);servoRW.write(90);servoSL.write(90);servoA1.write(90);servoA2.write(90);
delay(50);
}

// Function that reads the distance
int ultraRead(){
pinMode ( ping, OUTPUT );
digitalWrite( ping, LOW );
delayMicroseconds(2);
digitalWrite( ping, HIGH );
delayMicroseconds(5);
digitalWrite( ping, LOW);
pinMode (echo, INPUT);
duration = pulseIn(echo, HIGH);
mm = duration / 580;
}

Now the observations, please.

You can edit in the code tags too, when you're ready.