very simple question - 'if < 50 then turn motor on'

hello everyone,

I plan to build a sensor for blind people. The sketch below works well. But I need to add a vibrating motor took from an old cellphone. It needs to shake when something is less than 50cm from the person. How can I do it? I know this is simples but my attempts didn’t work.

#include “Ultrasonic.h”
#define echoPin 13 //Pino 13 recebe o pulso do echo
#define trigPin 12 //Pino 12 envia o pulso para gerar o echo
//iniciando a função e passando os pinos
Ultrasonic ultrasonic(12,13);

void setup()
Serial.begin(115200); //inicia a porta serial
pinMode(echoPin, INPUT); // define o pino 13 como entrada (recebe)
pinMode(trigPin, OUTPUT); // define o pino 12 como saida (envia)

void loop()
//seta o pino 12 com um pulso baixo “LOW” ou desligado ou ainda 0
digitalWrite(trigPin, LOW);
// delay de 2 microssegundos
//seta o pino 12 com pulso alto “HIGH” ou ligado ou ainda 1
digitalWrite(trigPin, HIGH);
//delay de 10 microssegundos
//seta o pino 12 com pulso baixo novamente
digitalWrite(trigPin, LOW);
// função Ranging, faz a conversão do tempo de
//resposta do echo em centimetros, e armazena
//na variavel distancia
int distancia = (ultrasonic.Ranging(CM));

Serial.print("Distancia em CM: ");
delay(100); //espera 1 segundo para fazer a leitura novamente


sketch_may08a_-_sensor_ultrassonico_fis_exp_II.ino (1.1 KB)

I know this is simples but my attempts didn't work.

Will you show us didn't work.


I put this… :confused:

if (distancia < 50)
digitalWrite(motorPin, HIGH);

When you: Serial.println(distancia); what gets printed?

I put this…

if (distancia < 50)
digitalWrite(motorPin, HIGH);

Are you connecting the motor directly to “motorPin”? X-X
(A motor requires some support circuitry/components.)

Did you remember:
byte motorPin = x;