0

This code we are using only one motor and one hc-sr04 ultrasonic sensor, the code is shown below:

#define trigPin 7
#define echoPin 6
#define motorPin 5

void setup()
{ 
  Serial.begin(9600);// to use the serial monitor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motorPin, OUTPUT);
}

void loop()
{
  int duration,distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2)/29.1;
  Serial.print(distance);//to print the distance on the serial monitor
  Serial.println("cm");

  // to make the motor vibrate depending on the distance
  // if the ultrasonic sensor is too far from the obstacle it will vibrate slowly
  // if the ultrasonic sensor is very close to the obstacle it will vibrate very fast

    if(distance <= 60 && distance >= 45)
    {
      // to make the motor vibrate
    digitalWrite(motorPin, HIGH); // making the motor on

    delay(200);
    digitalWrite(motorPin, LOW);// making the motor off
    delay(200);
    } 

    else if(distance < 45 && distance >= 30)
    {
    digitalWrite(motorPin, HIGH);
    delay(100);
    digitalWrite(motorPin, LOW);
    delay(100);
   }
    else if(distance < 30){

    digitalWrite(motorPin, HIGH);
    delay(50);
    digitalWrite(motorPin, LOW);
    delay(50); 
  } 
  else 
  {
    digitalWrite(motorPin, LOW);
  }
}

We want you to tell us your comment about the code.

And we want to add two more motors and two ultrasonic sensors, one of them in the left and the other in the right.

We want you to help us in the code, because we don't know how we can change it for two other motors.

2
  • Welcome to the Arduino StackExchange! We hope your stay will be fruitful. To help us view your code you should put it into "code blocks". I have edited your post and done it for you this time. Please read Editing Help to assist you in doing this. What is your question exactly? We want you to help us in the code isn't really a question.
    – Nick Gammon
    Aug 30, 2015 at 7:41
  • do you have a sketch, maybe a Fritzing sketch, for your arduino setup?
    – user20972
    May 11, 2016 at 15:10

1 Answer 1

1

There's a few things you need to consider when making multiple things happen at the same time.

Firstly is timing. You can't make two things happen concurrently with different timings if you have delay() in there, since nothing else can happen while delay() is executing. So you have to get rid of all delay() calls and replace them with examining the difference between successive calls to millis().

For instance, imagine you're in the kitchen and you want to boil and egg and make a cup of tea at the same time. You fill the kettle and turn it on. You put a pan of water on the stove with an egg in it to boil.

Now you could sit and stare at the egg until it's boiled enough for you to eat, but what happens to the kettle while you're doing that? It's boiled, turned itself off, and started to go cold.

So instead you put the egg in the pan and look at the clock. Then you think "in 5 minutes I will take the egg out of the boiling water". You might set an alarm for 5 minutes time if you have one, or you may keep glancing at the clock periodically to see if the 5 minutes have past yet. Meanwhile the kettle has boiled and you've made your cup of tea.

Then the 5 minutes have passed. You know that because you remembered what time you put the egg on to boil and you know what the time is now, so you can subtract one from the other to find that 5 minutes have passed.

Now let's make it a little more complex. Imagine you want two eggs - one soft boiled and one hard boiled. The hard boiled one may want 8 minutes, the soft boiled one 5 minutes.

So you put one egg on to boil and remember the time. Roughly three minutes later you put the other egg in and also remember that time.

Once enough time has passed that the first egg has been in 8 minutes you take it out.

Once enough time has passed that the second egg has been in 5 minutes you take it out.

You have had to remember two start times which you then compare separately to decide what to do with each egg when.

To translate that to your program - you have to remember a start time for each action for your motors, along with the state of the motors - and when the right time has elapsed you change the state of the motor (motor on -> motor off - egg in pan -> egg on plate) and remember the time at which you changed the state so you can know when to change the state again.

That's exactly what the BlinkWithoutDelay example does to make an LED change state (on -> off -> on) at certain times by remembering when it last changed state so you know when to change the state next.

4
  • Thanks a lot for replying .. the idea of our project is making a hat for blind person, inside the hat we have 3 hc.sr04 ultrasonic sensor and 3 motor located in : front , right and left , the motor should vibrate slowly when the obstacle is far and vise versa ... can you help us in code itself ? even if we can communicate to you by your email.. Aug 30, 2015 at 18:55
  • I have rolled the two basic functions (ping and blink) into simple libraries that should get you going. It should be a very simple matter to put them together. I haven't tested the Pinger library yet since I can't find my ultrasound board off hand, but it's very simple and should work. github.com/MajenkoLibraries/Blink github.com/MajenkoLibraries/Pinger
    – Majenko
    Aug 30, 2015 at 20:01
  • Thanks a lot .. plz give me ur email as soon as possible .. I want to ask u about the code :( Aug 31, 2015 at 9:48
  • 1
    If you have a problem with one of the libraries please raise it as an issue on Github. If you have a problem with understanding certain aspects of Arduino programming please ask a fresh question on this site detailing what you have tried and what you are having problems with. I don't hand my email address out willy-nilly.
    – Majenko
    Aug 31, 2015 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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