VIBRATIONS IN AN IF LOOP

Hey guys in my project i need arduino to calc the distance with ultrasonic and according to distance the buzzer will product sound but when it comes to close i want a vibrator motor to make vibrations..the buzzer is working but when it comes to close the motor vibrator is not working :frowning:

/*
* 
* 
* Complete Guide for Ultrasonic Sensor HC-SR04
*
   Ultrasonic sensor Pins:
       VCC: +5VDC
       Trig : Trigger (INPUT) - Pin11
       Echo: Echo (OUTPUT) - Pin 12
       GND: GND
*/

int trigPin = 7;    // Trigger
int echoPin = 12;    // Echo
long duration, cm, inches;
int buzzer = 10;
int vibration = 1;
void setup() {
 //Serial Port begin
 Serial.begin (9600);
 //Define inputs and outputs
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(buzzer,OUTPUT);
 pinMode(vibration,OUTPUT);

}

void loop() {
 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 // Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

 // Convert the time into a distance
 cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
 inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
 
 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);

 Serial.print("cm");
 Serial.println();

   
 if(cm <=15){
       
      digitalWrite(vibration,HIGH);
      
  }
 else if(cm <= 25) {
    
      tone(buzzer,4000,500);
      delay(1000);
        
 }
 else if(cm <= 30) {
     
      tone(buzzer,4000,500);
      delay(1500);
    
  
 }
 else   if(cm <= 40) {

    tone(buzzer,4000,500);
    delay(2500);
 }

}

How is the motor driven?

Please remember to use code tags when posting code.

And... there's no such thing as an if loop.

int vibration = 1;

It is not a good idea to use pins 0 or 1 as general purpose pins because they are used by the Serial interface. Move the vibration motor to another pin

I moved it to another pin but still it isnt' working !

newtron11:
I moved it to another pin but still it isnt' working !

You haven't edited your post to add code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Please post your current modified sketch, also in code tags.

newtron11:
I moved it to another pin but still it isnt' working !

You moved what to another pin?
What is driving the motor?

Where is your code?

Hey guys UKBob thank you man you were right i moved it to pin 5 and is working allright could you tell me why it didnt work with pin 1 i dont know what serial interface is! I am new to arduino been working with it only for 2 days now although i am a php developer for a leaving !

//Serial Port begin
 Serial.begin (9600);

The serial interface - the thing that allows you to see what your sketch is doing.