HI, I am new to the forum, I hope I will find help.
I am doing a university project where the aim is to build an inner pipe climber robot.
I have attached a part of my code where I am stuck.
In this part I am using an Ultrasonic sensor and a stepper motor. The part 'void ultra' is the code to measure the distance with an ultrasonic sensor. However when I am measuring the distance as well as running the motor, the motor runs suddenly super slowly, however when I run the motors without the part of the code related to the ultrasonic sensor, it runs at normal speed.
I think that this might be due to the pulseIn function. PLEASE HELP me , I have no clue on how to solve this. This is the first time this ever happens to me.
Thank you !!
/////CODE/////
int Stoppage = 0;
// defines pins numbers
int trigPin = 2; // Trigger
int echoPin = 3; // Echo
long duration, cm;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
Ultra();
digitalWrite(9, HIGH);
delayMicroseconds(300);
digitalWrite(9, LOW);
delayMicroseconds(300);
}
void Ultra()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) /29.1;
Serial.print(distance);
Serial.print("cm");
Serial.println();
}
test_case_normal_avec_int.ino (813 Bytes)