Servo Motor and US Proximity Sensor

Please help. I am desperately trying to save my grade and my mental health is severly declining. I am just trying to make it so when the proximity sensor is triggered, the motors turn 90 degrees, and when it isnt triggered it turns back 90 degrees.

Here is my absolute horrible attempt at coding. Btw my teavcher is no help. ANY HELP IS APPRECIATED PLEASE HELP

#include <Stepper.h>

const int TRIG_PIN = 11; // Pins

const int ECHO_PIN = 3;
const unsigned int MAX_DIST = 23200; // Anything over 400 cm (23200 us pulse) is "out of range"

int STEPS = 200; // change this to the number of steps on your motor

Stepper stepper(STEPS, 4, 5, 6, 7); //create a stepper class and set pins

Stepper stepper2(STEPS, 8, 9, 11, 12);

void setup()
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
Serial.begin(9600);
{

stepper.setSpeed(20); // set the speed of the motor to 30 RPMs

stepper2.setSpeed(20);

}

void loop()
unsigned long t1, t2, pulse_width;
float cmValue, inchesValue;

digitalWrite(TRIG_PIN, HIGH); // Hold the trigger pin high for at least 10 us
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

while ( digitalRead(ECHO_PIN) == 0 ); // Wait for pulse on echo pin
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1); //end of echo
t2 = micros();
pulse_width = t2 - t1; //calculate time between send pulse and returned pulse
cmValue = pulse_width / 58.0; //convert time into cm
inchesValue = pulse_width / 148.0; //convert time into inches

if (pulse_width > MAX_DIST ) {

for (int i =0; i<50; i++)
{
stepper.step(1);
stepper2.step(1);

}

else

for (int i =0; i<50; i++)
{
stepper.step(-1);
stepper2.step(-1);

}

delay(1000);

}

}

Your code doesn't even compile.
Why not tell us?

You don't really need these.

Have you worked through any of the code examples in the IDE?

Please remember to use code tags when posting code.

Have you got the ultrasonic ranger to detect distances reliably?

Have you got the motor to turn 90 ° in both directions reliably?

@cmoll69, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

This is what your functions should look like

void setup()
{
  do stuff here
}

void loop()
{
  do stuff here
}

Do your setup() and loop() look like that?

I’ve just completed a project for someone that does everything plus more than you want…
The first thing you need to figure out, is how many steps are needed for 360 degrees, then divide that by 360 to get the number of ‘steps per degree’.

Then, when you want to move a particular path, you’ll know how many times you need to step the motor to reach a particular degree angle.
(This could just as easily be radians, feet, millimetres or yards.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.