Controlling the speed of a vibrating that is linked to an ultrasonic sensor

I'm using an Arduino UNO and connecting an ultrasonic sensor with a simple vibrating sensors as follows:

With the following code:

 /*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
#include "Ultrasonic.h"
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const uint8_t vibrate_pin = 11;

// defines variables
long duration;
int distance;
// Define global variables
ultrasonic_t ultrasonic;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
ultrasonic_initialize(&ultrasonic);
Serial.begin(9600); // Starts the serial communication for debugging purpose
pinMode(vibrate_pin, OUTPUT);
digitalWrite(vibrate_pin, LOW);
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

// Enable vibrate mottor
//delay(500);
if (distance <= 50) {
  digitalWrite(vibrate_pin, LOW);
}else{
  digitalWrite(vibrate_pin, HIGH);
}
}

I'm trying to make the vibrating sensor faster if the distance is less than 50cm. I'm getting the correct distance but the code isn't working correctly. The vibrating pin is vibrating at one speed only. The vibrating speed doesn't change no matter what I try. Can someone take a look at it and let me know what I'm doing wrong?

How fast will these lines of code take to execute?

if (distance <= 50) {
digitalWrite(vibrate_pin, LOW);
}else{
digitalWrite(vibrate_pin, HIGH);
}

Do you suppose you will even notice a vibration?

Paul

Does that motor have a kickback diode built in? If not you might ruin your output pin (or whole Arduino without it), what kind of vibrating frequency are you looking for? Have you tried the Tone function?

I honestly don't know. I've taken your advice but the problem still occurs. It seems like it only vibrates at the speed in the first condition no matter what. This is my current code:

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
#include "Ultrasonic.h"
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const uint8_t vibrate_pin = 11;

// defines variables
long duration;
int distance;
// Define global variables
ultrasonic_t ultrasonic;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
ultrasonic_initialize(&ultrasonic);
Serial.begin(9600); // Starts the serial communication for debugging purpose
pinMode(vibrate_pin, OUTPUT);
digitalWrite(vibrate_pin, LOW);
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

// Enable vibrate mottor
//delay(500);
if (distance > 50 && distance < 100) {
  tone(vibrate_pin, 50);
  //delayMicroseconds(1000);
}else if(distance >= 25 && distance <= 49){
  //digitalWrite(vibrate_pin, HIGH);
  tone(vibrate_pin, 500);
}else{
  tone(vibrate_pin, 1000);
}
}

The speed at the first condition is 50, so it vibrates at 50 no matter of the distance (which is changing and i'm sure about it because i can see it in the serial monitor). But if i was to change the first 50 to 500, it will also stay vibrating at 500 no matter what. Can it be that something is wrong with the code?

Paul_KD7HB:
How fast will these lines of code take to execute?

if (distance <= 50) {
digitalWrite(vibrate_pin, LOW);
}else{
digitalWrite(vibrate_pin, HIGH);
}

Do you suppose you will even notice a vibration?

Paul

Can you please elaborate? How would i measure the speed of the execution of the code?

I think the problem is

Abdulmalek97:
I honestly don't know. I've taken your advice but the problem still occurs. It seems like it only vibrates at the speed in the first condition no matter what. This is my current code:

/*

*/
#include "Ultrasonic.h"
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const uint8_t vibrate_pin = 11;

// defines variables
long duration;
int distance;
// Define global variables
ultrasonic_t ultrasonic;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
ultrasonic_initialize(&ultrasonic);
Serial.begin(9600); // Starts the serial communication for debugging purpose
pinMode(vibrate_pin, OUTPUT);
digitalWrite(vibrate_pin, LOW);
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

// Enable vibrate mottor
//delay(500);
if (distance > 50 && distance < 100) {
  tone(vibrate_pin, 50);
  //delayMicroseconds(1000);
}else if(distance >= 25 && distance <= 49){
  //digitalWrite(vibrate_pin, HIGH);
  tone(vibrate_pin, 500);
}else{
  tone(vibrate_pin, 1000);
}
}




The speed at the first condition is 50, so it vibrates at 50 no matter of the distance (which is changing and i'm sure about it because i can see it in the serial monitor). But if i was to change the first 50 to 500, it will also stay vibrating at 500 no matter what. Can it be that something is wrong with the code?

I think the problem is I'm not asking it to check the distance again every X seconds? How would i do that?