Vibration motor troubles

Hey guys

Im having a bit of trouble with my project and Im not sure why hoping someone on here could give me some suggestions as to what is happening. Im fairly new to this so the project is simple enough. The basis of the project is a vibrating money box. You drop a coin in and when it passes an infrared sensor it sets off a vibration motor for 3 seconds.

Now heres the trouble, I had it working with the code below. The only difference was 1) I had it powered through usb on a computer and now Im using a 9v battery to power it . And 2) I switched the vibration motor from pin 13 to pin 12 due to a wire snapping in pin 13. Now when I connect the battery the vibration motor is constantly vibrating no mater what values the infrared sensor is showing. Anyone know what the problem could be? Is it the difference in power source from USB to 9v battery or is there something in the code? Thanks guys!

 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to


int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(12, OUTPUT);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
if (sensorValue>300)
{
digitalWrite(12, HIGH );
delay(3000);
digitalWrite(12, LOW );
}
  else
  {
    digitalWrite(12, LOW);
  }
  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.println(sensorValue);


  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);
}

Hey guys

Im having a bit of trouble with my project and Im not sure why hoping someone on here could give me some suggestions as to what is happening. Im fairly new to this so the project is simple enough. The basis of the project is a vibrating money box. You drop a coin in and when it passes an infrared sensor it sets off a vibration motor for 3 seconds.

Now heres the trouble, I had it working with the code below. The only difference was 1) I had it powered through usb on a computer and now Im using a 9v battery to power it . And 2) I switched the vibration motor from pin 13 to pin 12 due to a wire snapping in pin 13. Now when I connect the battery the vibration motor is constantly vibrating no mater what values the infrared sensor is showing. Anyone know what the problem could be? Is it the difference in power source from USB to 9v battery or is there something in the code? Thanks guys!

 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to


int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(12, OUTPUT);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
if (sensorValue>300)
{
digitalWrite(12, HIGH );
delay(3000);
digitalWrite(12, LOW );
}
  else
  {
    digitalWrite(12, LOW);
  }
  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.println(sensorValue);


  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);
}

One easy way to test might be to comment out digitalWrite(12,HIGH) in your main loop and see what happens- if this stops the vibration then code is the problem, if not then hardware or wiring might be at fault.

Do you have the vibration motor connected directly to the Arduino pin? If so you may have damaged the output.

Please supply a circuit drawing showing how you have wired the parts.

Drawing freehand and photographed will do.

Weedpharma

Do not cross-post. Threads merged.