I can not get out of it

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 :slight_smile:

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

}

Arduinos can only run one program at a time!.

Mark

you want ultrasonic to control your stepper ?

  1. first define variables (for both stepper and untrasonic sensor)
    2.void setup() .. to prepare i/o
  2. void loop() { what should be repeated}

that are all quick answers, it is intended that the stepper motor is running, one direction, and that the sensor ensures that he stops when he approached a Wall

Does your stepper perform OK just now?
then:
..and when the obstacle is detected.. then what (just stop, end of game?)
or: turn steering left -> (back up until free sight >xx / back up 50 steps) -> set steering straight. -> repeat from start..
*Do you have one or two motors for driving (so motors can be user for stering)
*Can the vehicle steer?

#define STP  2    // Constant   
#define DIR  3    // Direction Pin ??? forward / r
#define BUT  4    // Direction Button HIGH=forward.  LOW=STOP!  
#define grn_led 10
#define red_led 11
#define echoPin 12
#define trigPin 13
 
void setup()
{   
  pinMode(DIR, OUTPUT);         // Direction is an output   
  pinMode(STP, OUTPUT);         // Step is an output   
  pinMode(BUT, INPUT_PULLUP);  
  pinMode(red_led, OUTPUT);      
  pinMode(grn_led, OUTPUT);      
  pinMode(echoPin, INPUT);       
  pinMode(trigPin, OUTPUT);     
  Serial.begin (9600);
 
}   
 
boolean noObstacles(byte cm) // within cm centimeter
{ 
  long dist,dura; // local vars
  digitalWrite(trigPin, LOW);   delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);  delayMicroseconds(7); 
  digitalWrite(trigPin, LOW);
  dura = pulseIn(echoPin, HIGH, 5000); // timeout 5ms
  dist = (dura/58.2);
  if (dist==0) return true ;  // i believ this is value for timeout
  return (dist>cm);
}
 
void steps(int nr) // step a given number
{   
  for (int i=0; i<nr; i++)
  {
    digitalWrite(STP, HIGH);  delay(3);   
    digitalWrite(STP, LOW);   delay(3);   
  }
}

void loop()
{   
    digitalWrite(DIR, HIGH);  // set direction (forward)
    while ((digitalRead(BUT)==HIGH) && noObstacles(20)) // "go" + clear sight 20cm
    {
      digitalWrite(grn_led,HIGH);
      steps(10); // run a short distance forward
    }
    // determine WHY while-loop ended . Button released or object on front
    if (digitalRead(BUT)==HIGH)  // button still pressed. must be an obstacle - back up
    {
      // turn steeringwheels
      digitalWrite(grn_led,LOW);
      digitalWrite(DIR, LOW);  // reverse
      steps(5); // back up a little
    }
    delay(999); // hold a sec
}

hello knut_ny,

with that sketch the stepper perform good,
no just stop before the wall, and turn everything off, i have a arduino relay thats connect to the LED1 as signal.

^^^
maybe you can refine my code... | | | (up there.)

Hmmm, I do not know what to say knut_ny, , but it works you code, where I have been for weeks and weeks looking for an answer, you are the man, thanks allot. :slight_smile:

Then use it a a starting point - I'm happy you'r on track!

I've been studying your code, but it is not easy to understand.
have a lot to learn :slight_smile:

Film your 'robot' - upluad youtube - send link.
It will be easier to contribute with ideas.