Stepper mottor plus ultrasonic

Hello,

code bellow works great but can anyone help me add HC-SR04 sensor witch would turn stepper mottor in different direction when it detects motion in couple of centimeters ?

#define RPMS 104.0
#define STEP_PIN 9
#define DIRECTION_PIN 8
#define GO_PIN 3

#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 8
#define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))

uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;

void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIRECTION_PIN, LOW);
pinMode(GO_PIN,INPUT);
}

void loop() {
if (digitalRead(GO_PIN) == LOW)
{
CurrentTime = micros();
if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
{
LastStepTime = CurrentTime;
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
}
}
}

Read the forum guidelines. to see how to properly post code.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

It is not likely that anyone will write the code for you. If you post your best attempt we can help. Tell us what the code actually does and how that differs from what you want the code to do.

The NewPing library makes using the HCSR04 sensors easier. That library is available through the IDE library manager.

See my tutorial on Multi-tasking in Arduino which includes a detailed example using a temperature sensor to control a damper (as well as control by user input)

Have you coded and tested HC-SR04 sensor in a separate program so you can copy the code/logic to the other program? If not, start now.
Paul

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