2

I want to make the vibration motor vibrate depending on the distance to an obstacle, for example if the distance is too short it should vibrate quickly and vice versa.

How can i make motor vibrate using arduino depending on the distance? Can you explain for me how can I use the delay in the vibration code? I found this code in the internet for vibration:

digitalWrite(y, HIGH);
delay(50);
digitalWrite(y, LOW);
delay(50); 

I know that I can change the delay, but how can we do it depending on our purpose? Please help me .. I don't know the use of the delay :(

3 Answers 3

1

I know that I can change the delay, but how can we do it depending on our purpose?

Are you asking how to use a variable? An example:

const int LED = 13;

void setup() 
  {
  pinMode (LED, OUTPUT);
  }

void loop() 
  {
  int amount = analogRead (A0);   // get a reading
  digitalWrite(LED, HIGH);
  delay(amount);
  digitalWrite(LED, LOW);
  delay(amount);
  }

That code reads some analog device, and adjusts the delay based on the figure it read.

1

You could just replace the "50" in the first delay with the ultrasonic distance variable, I'll explain a bit more below.

Let's assume your ultrasonic sensor outputs an analogue value between 0V and 5V. The output is connected to analog pin A0. Then you just set pinMode(A0, INPUT) and analogRead the value from the pin and store it in the variable X or whatever. Then you just use the variable X as a delay and it will do what you want. The higher the value gets the longer it will vibrate. Something like this:

[...]
val = analogRead(A0);
digitalWrite(5, HIGH);
delay(val);
digitalWrite(5, LOW);
[...]

Second option, if you only wish to turn it on if it is above or under a certain limit, would be using an if condition.

For example:

if(variable > 50){
  // if the variable named variable is over 50 this code is going to be executed
} else {
  // If the variable is below 50 this is executed
}

If you are unfamiliar with if conditions then I suggest reading this too.

0

I want to show you my thinking, and tell me please if it is right or wrong:

I build an relationship between the distance and the speed.

I know that (speed = distance/time) and if the obstacle is too close to the ultrasonic sensor the speed of the motor should be maximum, so I have decrease the time which is the delay, right?

I make the code according what i said above and it is shown below:

    if(distance <= 60 && distance >= 45)// the distance is too large
    {
    digitalWrite(motorPin, HIGH);

    delay(200);// I increase the delay to make the motor vibrate slowly

    digitalWrite(motorPin, LOW);

    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);// If the distance is too large the motor will be off
  }
3
  • The code is okay, but one of the comments are wrong. Using a longer delay makes the motor spin longer, delay after turning the motor off increases the time between that and the next reading so keep that in mind.
    – Avamander
    Aug 31, 2015 at 22:06
  • Thanks a lot for replying, but u didn't tell me if my thinking is right or not according to the equation that i wrote above.. Also, which comment is wrong ? Can u explain the idea for me more, cuz i got confuse little bit ... :( Also.. can u give me ur email that will make the communication easier ?? plz reply as soon as u can ...
    – zooz
    Sep 1, 2015 at 17:15
  • The first comment and the logic is wrong. You can not change the speed of a motor with a delay, you can change the duration of it being on or off. See my answer below for more details.
    – Avamander
    Sep 1, 2015 at 17:47

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.