Coding step motor and Ultrasonic

Hello! I'm trying to do a program that:

  1. Move the arduino step motor 360 back and forth.
  2. calculate the ultrasonic in cm like every ms.
  3. serial print the step---> cm so I will have a reading every step
  4. And that the code serial.print the steps

So I can use this information to make a "radar"

I make a program using other codes but it have a problem. It is running the step motor and when it stop runs the ultrasonic for only one reading and then start moving the step motor again. What I can do to run the two codes at the same time?

Thanks!

/*
  BYJ48 Stepper motor code
  Connect :
  IN1 >> D8
  IN2 >> D9
  IN3 >> D10
  IN4 >> D11
  VCC ... 5V Prefer to use external 5V Source
  Gnd
 
 */
#define led 3
#define led2 4
#define trigPin 7
#define echoPin 6
#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
//ultrasound setup
unsigned long echo = 2;
int ultraSoundSignal = 3; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
//step motor
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;

void setup()
{
  Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(led, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(IN1, OUTPUT); 
 pinMode(IN2, OUTPUT); 
 pinMode(IN3, OUTPUT); 
 pinMode(IN4, OUTPUT); 


}
//ultrasonico
unsigned long ping()
{ 
 pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
 digitalWrite(ultraSoundSignal, LOW); // Send low pulse 
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(ultraSoundSignal, LOW); // Holdoff
 pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
 digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
 // please note that pulseIn has a 1sec timeout, which may
 // not be desirable. Depending on your sensor specs, you
 // can likely bound the time like this -- marcmerlin
 // echo = pulseIn(ultraSoundSignal, HIGH, 38000)
 echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
 ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
 return ultrasoundValue;
}

void loop()
{
//Stepper

 while(steps_left>0){
 currentMillis = micros();
 if(currentMillis-last_time>=1000){
 stepper(1); 
 time=time+micros()-last_time;
 last_time=micros();
 steps_left--;
 }
}
 // Serial.println(time);
 //Serial.println("Wait...!");
 delay(500);

 Direction=!Direction;
 steps_left=4095;
//ultrasonico
{
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 >= 300 || distance <= 0){
   Serial.println("Out of range");
 }
 else {
   Serial.print(distance);
   Serial.println(" cm");
 }
 delay(500);

}
}

void stepper(int xw){
 for (int x=0;x<xw;x++){
switch(Steps){
  case 0:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  break; 
  case 1:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);
  break; 
  case 2:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  break; 
  case 3:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
  break; 
  case 4:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  break; 
  case 5:
    digitalWrite(IN1, HIGH); 
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  break; 
    case 6:
    digitalWrite(IN1, HIGH); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  break; 
  case 7:
    digitalWrite(IN1, HIGH); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
  break; 
  default:
    digitalWrite(IN1, LOW); 
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
  break; 
}
SetDirection();
}
} 
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}

Please read this before posting a programming question and modify your post so that the code is in code tags.

What I can do to run the two codes at the same time?

Interleave them properly. Step, read, step, read,...

Call the function you wrote. Do NOT duplicate the code.

Thanks PaulS you are saying that I need to put this way:

Step1
-Ultrasonic

  • Step motor
    Step2
    -Ultrasonic
    -Stepmotor

instead of this:

Step 1
-Ultrasonic
Step2
-Stepmotor

I'm really new to coding and all the things I did in the past was just using logic and mixing chunks of other codes but this time not is working like I want.

  1. calculate the ultrasonic in cm like every ms.

You can't range more than about 20 or 30 times a second (50 to 33 ms)- this requirement is not sensible

Thanks! Ok maybe a little background before the code will help.

I need an exit information like the information displayed in serial.print this way:

Lecture in cm: Step:

__ cm 1
__ cm 2

So I can traduce the step to an angle in labview and use the "radar VI" in labview So I understand that maybe not is that often, maybe 2 times per second is enough.

I need to have the angle and the distance so it can be plotted in polar coordinate.

BTW I think the void loop and void stepper is the problem, the program is doing first one and then the other so the ultrasonic code in the void loop not going to run in the void stepper.

BTW I think the void loop and void stepper is the problem, the program is doing first one and then the other so the ultrasonic code in the void loop not going to run in the void stepper.

The analogRead() function returns an int. You don't refer to it as "the int analogRead" do you? The proper term is "loop function" or "stepper function".

Your code doesn't use the stepper function. It moves the stepper 4095 steps, and then does one ping, using code that is a duplicate of the code in the ping function, instead of calling the ping function.

Once the stepper has moved 4095 steps, it takes a permanent nap. Why?

The motor not stop, the motor makes the 4095 steps to the left, 1 second stop and then 4095 steps to the right. So it makes a full 360 turn and then wait 1 second an make Thats ok, I just wanted to run an ultrasonic at the same time reading at every angle or two times every angle of the 360 turn. The problem is the ultrasonic only work one reading at the one second when the motor stop, not is working when the step motor is doing the turn. I Think that it has something to do with the voids or the stepper matrix.

The problem is the ultrasonic only work one reading at the one second when the motor stop, not is working when the step motor is doing the turn.

No. The problem is that you do not step, read, step, read...

You need to understand the part of the code that makes the stepper move, and, after each move (or after some number of moves) read the ping sensor.

Reading 4096 values per revolution doesn't make sense, but reading after every 12 or 24 or 48 steps does.

Thats right, the 4096 values per revolution is the "steps" that the motor need to make to do a full 360 turn. Because the motor have a gear inside it traduce the 4096 into 64 steps with the gear.

I don't know how to make it read every 12-24 or 48 steps from the 4096. Other thing I can do is using two chips one for each thing. I just looking for options

I don't know how to make it read every 12-24 or 48 steps from the 4096.

Can you at least identify the part of the code that makes the stepper move?

Can you add code to count the number of steps taken?

Can you look at the modulo operator (%) and figure out how to determine if you have taken n steps?

Do you know how many steps you want to take between readings?