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);
}
}