hello everyone ,
I can not figure out what I'm doing wrong , try it for weeks and weeks .
it should be simple for an ultrasound sensor and stepper motor .
it is a project for my child. and the intent is to comply to one code. Here is the code of the stepper
already thanks ![]()
// Constants ------------------------------------
#define DIR 3 // Direction Pin
#define STEP 2 // Constant
#define BUT 4 // Direction Button
// Variables ------------------------------------
// Setup ----------------------------------------
void setup(){
pinMode(DIR, OUTPUT); // Direction is an output
pinMode(STEP, OUTPUT); // Step is an output
}
// Loop -----------------------------------------
void loop(){
digitalWrite(DIR, HIGH); // set direction
stepOnce(); // Step
}
// Function Definition ---------------------------
// the specifications says the low to high transition
// must be at least 1 microsecond
void stepOnce(){
digitalWrite(STEP, HIGH);
delay(1);
digitalWrite(STEP, LOW);
delay(1);
}
and the code for the sensor
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}