0

I've made a code to make a stepper work together with a HCSR04 sensor. The problem here is that the sensor has to use a delay in order to work. This delay messes with the smootness of the stepper.

If I remove the code for the sensor the stepper runs verry smooth, adding the sensor code causes the stepper to run 'shaky'.

The code:

int trigPin = 11; 
int echoPin = 10; 

void setup()
{  
pinMode(trigPin, OUTPUT); //Sensor
pinMode(echoPin, INPUT); //Sensor  
pinMode(6,OUTPUT); // Enable
pinMode(13,OUTPUT); // Step
pinMode(12,OUTPUT); // Dir
digitalWrite(6,LOW); // Set Enable low
digitalWrite(5,LOW); // Set Step low
Serial.begin(9600);
} 

void loop()
{  
//rotateleft();
//Serial.println("ik draai nu exact een rotatie linksom");
//delay(1000);
//rotateright();
//Serial.println("ik draai nu exact een rotatie rechtsom");
//delay(1000);
//velocity();

 //sensor
 long duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2); //**problem**
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); //**problem**
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 Serial.print ("cm:");
 Serial.print (distance);

//eigen creatie
if (distance > 20 && distance < 30)
{
  digitalWrite(13,HIGH); // Output high   
  delayMicroseconds(1000); // Wait   
  digitalWrite(13,LOW); // Output low   
  delayMicroseconds(1000); // Wait
}

if (distance > 10 && distance < 20)
{
  digitalWrite(13,HIGH); // Output high   
  delayMicroseconds(3000); // Wait   
  digitalWrite(13,LOW); // Output low   
  delayMicroseconds(3000); // Wait
}

if (distance > 1 && distance < 10)
{
  //digitalWrite(13,HIGH); // Output high   
  //delayMicroseconds(1000); // Wait   
  digitalWrite(13,LOW); // Output low   
}

else
{
 velocity(); 
}

}

void velocity()
{
 digitalWrite(13,HIGH); // Output high
 delayMicroseconds(500); // Wait
 digitalWrite(13,LOW); // Output low
 delayMicroseconds(500); // Wait
}
 }
4

Have you searched for other libraries or example code for this particular sensor?

I found this NewPing library that claims to have been developed to fix poor performance of such modules due to poor timing methods.

  • Yes, the bitbucket.org/teckel12/arduino-new-ping/wiki/… has a non-blocking timer-interrupt based example. – Dave X Nov 7 '16 at 16:14
  • I have been looking into the newPin library but I cannot find a function that resolves the problem. Still, I am going to look into the NewPin library. It seems to promising. But I quess it is not going to completly fix my delay problem. – Jordakoes Nov 7 '16 at 20:11
1

You only have a single thread of execution of an Arduino. So you have limited options:

  1. Live with it.
  2. Use two Arduinos.
  3. Use a microcontroller with 2 cores.
  4. Interrupts

2 Is possible, you could have a slave mcu to drive the motor and control it over I2C. You will still have contention when you are driving the I2C bus, but you can time that so it is not when you are scanning.

3 Is a very similar solution to 2, but is all in one chip.

  1. You MIGHT be able to use interrupts which will let processing continue whilst you are waiting for something to occur. I can't remember if the Arduino has timer interrupts, if it doesn't an external source could generate the interrupts and changes the state of the lines.,
  • Hi Matt, Interesting options. I do have a second Arduino Uno laying around for this project. Haven't heard of I2C but I will look into it. – Jordakoes Nov 7 '16 at 20:09
  • You could also use Serial to talk between the two devices, but you can't use the main serial port on an Uno, because that will make debugging difficult and people keep reporting issues with software serial. – Code Gorilla Nov 8 '16 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.