# Need help with some code, unable to use vibrating motors

**URL:** <https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546>\
**Category:** Programming\
**Created:** [November 5, 2025, 6:05pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546 "2025-11-05T18:05:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![papanewgibby](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papanewgibby/32/1244313_2.png) [@papanewgibby](https://forum.arduino.cc/u/papanewgibby)\
**Post date:** [November 5, 2025, 6:05pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/1 "2025-11-05T18:05:19Z")

</div>

Hi guys, I'm very new to Arduino and c++ overall. I got a project for school and spent quite a bit of time learning about c++ and Arduino. Basically, I have 4 buttons, each one relates to a different 'goblin class'. No button pressed is 'no goblin detected'. Each class should have its own function, e.g. different colour LED and vibration.  
The issue I'm running into is that when a button is pressed, e.g. class 2, the vibration motor just doesn't vibrate at all. I've tried going through my code and am unsure with what the problem is. Below is the code please help.

```arduino

/*
Joseph Gibson 
Goblin Catcher
Oct 2025
*/

 //downloads library to control led strip
#include <Adafruit_NeoPixel.h>

 //define each pin to its corresponding component
#define TEMP_PIN A0
#define VIB_PIN 9
#define LED_PIN 6

 //define each button to its specific goblin class
#define gobClass_1 5
#define gobClass_2 4
#define gobClass_3 3
#define gobClass_4 2

 //how many LEDs attachted to the aurduino
int LED_COUNT = 8;

 //setup for neopixel library
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

 //defining what the tempature limits will be
int tempLimit = 35;
int tempWarning = 30;

 //creating a variable for goblin class
int goblinClass = 0; //0 = no goblin, 1-4 = different classes
int lastGoblinClass = -1; // stores previous state

void setup() {
  Serial.begin(9600); //initialise the serial monitor

  strip.begin(); //initialise neo pixel leds
  strip.show(); //initialise all pixels to off
  
   //set each button as an input
  pinMode(gobClass_1, INPUT_PULLUP);
  pinMode(gobClass_2, INPUT_PULLUP);
  pinMode(gobClass_3, INPUT_PULLUP);
  pinMode(gobClass_4, INPUT_PULLUP);
  
   //sets the vibrating motor as an output
  pinMode(VIB_PIN, OUTPUT);

   //prints start message
  Serial.println(" ");
  Serial.println("Goblin Catcher Now Operational");
}

void loop() {
  TempMechanics(); //runs the TempMechanics function over and over again

  UpdateGobClass(); //runs the UpdateGobClass function over and over again

   //ensures that the "Class" function is only ran if the type of goblin changes
  if (goblinClass != lastGoblinClass) {
    lastGoblinClass = goblinClass;
      HandleClass(goblinClass); //runs the HandleClass function if goblin type changes
  }
}

void UpdateGobClass() {
   /*cycles through all possible buttons if one is detected it changes 
   the class variables to the detcted class, if no buttons are pressed 
   then it will stay as 0 (no goblin detected) */
  if (digitalRead(gobClass_1) == LOW)
    goblinClass = 1;
   else if (digitalRead(gobClass_2) == LOW)
     goblinClass = 2;
   else if (digitalRead(gobClass_3) == LOW)
     goblinClass = 3;
   else if (digitalRead(gobClass_4) == LOW)
     goblinClass = 4;
    else
      goblinClass = 0;
  }

void TempMechanics() {
   //reading tempature sensor
  int TempReading = analogRead(TEMP_PIN); //defining TempReading as the voltage reading from the temp sensor
  float TempVolt = TempReading * 5.0 / 1024.0;
  float CurTemp = (TempVolt - 0.5) * 100; // TMP36 formula

   //overheating condition
  if (CurTemp >= tempLimit) {
     //prints message if the current tempature exceeds the maximum
    Serial.println("Exceeding Safe Tempature");
    Serial.print("Current tempature is ");
    Serial.print(CurTemp);
    Serial.println("deg c");
    Serial.println("Shutting down for 10 seconds");
    delay(10000); //delays 10 seconds to prevent overheating
  }

   //warning condition
  else {
    if (CurTemp >= tempWarning && CurTemp < tempLimit) {
       //prints message warning that it's almost exceeding maximum
      Serial.println("Warning almost exceeding tempature limit");
      Serial.print("Current tempature is ");
      Serial.print(CurTemp);
      Serial.println("deg C");
      delay(500);
    }
  }
}

void setLEDs(uint8_t r, uint8_t g, uint8_t b) {
   //loops through all connect LEDs and sets them to a designated RGB value
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(r, g, b)); 
  }
  strip.show(); //updates all leds
  UpdateGobClass();

}

void blinkLEDs(uint8_t r, uint8_t g, uint8_t b, int onTime, int offTime, int currentClass) {

  //allows the rgb values and the time between on and off to be changes
  while (goblinClass == currentClass) { //keep blinking till class changes
    setLEDs(r, g, b); //turn LEDs on
    delay(onTime); //turns on for set time
    setLEDs(0, 0, 0); //turn LEDs off
    delay(offTime); //turns off for set off time
    UpdateGobClass();
  }
}

void larsonScanner(uint8_t r, uint8_t g, uint8_t b, int delayTime, int currentClass) {
   //allows the rgb values to be set and for there to be a larson scanner effect
  while (goblinClass == currentClass) {
    for (int i = 0; i < LED_COUNT; i++) {
      setLEDs(0, 0, 0); //clear LEDs
      strip.setPixelColor(i, strip.Color(r, g, b)); //light one LED
      strip.show();
      delay(delayTime);
      if (goblinClass != currentClass) return; //exit if class changed
    }
    for (int i = LED_COUNT - 2; i > 0; i--) {
      setLEDs(0, 0, 0);
      strip.setPixelColor(i, strip.Color(r, g, b));
      strip.show();
      delay(delayTime);
      if (goblinClass != currentClass) return;
    }
  }
}

void vibrateOn() { 
 digitalWrite(VIB_PIN, HIGH); //turns vibrating motor on
}

void vibrateOff() { 
 digitalWrite(VIB_PIN, LOW); //turns vibrating motor off
}

void vibratePulse(int onTime, int offTime, int repeat, int intensity, int currentClass) {
   //allows vibrations to be repeated for set length and set number of times, at a set intensity
  for (int i = 0; i < repeat && goblinClass == currentClass; i++) {

    analogWrite(VIB_PIN, intensity); //vibrating motor on
    delay(onTime); //wait
    analogWrite(VIB_PIN, 0); //vibrating motor on
    delay(offTime); //wait 

}
}

void HandleClass(int currentClass) {
   //handles actions for when classes are dected
  switch (currentClass) { 
    //searches all cases below and when it finds a match with "current class" runs that code
  
   case 0:
     Serial.println("No goblins detected "); //prints message in serial monitor
     setLEDs(255, 19, 240); //sets LEDs (pink)
     vibrateOff(); //ensures motors turned off 
    
      UpdateGobClass();
    break;

   case 1:
    Serial.println("Class 1 goblin detected "); //prints message in serial monitor
    blinkLEDs(0, 255, 0, 1500, 1000, 1); //blink (green LEDS, on 1500ms, off 1000ms, class 1)
      
      UpdateGobClass();
    break;
       
   case 2:
    Serial.println("Class 2 goblin detected "); //prints message in serial monitor
    blinkLEDs(255, 255, 0, 1000, 500, 2); //blink (yellow LEDS, on 1000ms, off 500ms, class 2)
    vibratePulse(2000, 1000, 2, 64, 2);  
    
      UpdateGobClass();
    break;
       
   case 3:
    Serial.println("Class 3 goblin detected ! "); //prints message in serial monitor
    blinkLEDs(255, 92, 0, 500, 300, 3); //blink (orange LEDS, on 500ms, off 300ms, class 3)
    vibratePulse(2000, 1000, 3, 128, 3);  

      UpdateGobClass();
    break;       
         
   case 4:
    Serial.println("CLASS 4 GOBLIN DETECTED ! "); //prints message in serial monitor
    larsonScanner(255, 0, 0, 200, 4); //larson scanner (red LEDS, on 200ms, class 4)
    vibratePulse(2000, 1000, 4, 255, 4);  
  
    
      UpdateGobClass();
    break;    
       
     }
    }
 /*
const int motor_pin = 9;

void setup() { // put your setup code here, to run once:
  pinMode(motor_pin,OUTPUT);  
  Serial.begin(9600);
}

void loop() { 
analogWrite(motor_pin,255);
Serial.println("high");
delay(1000);
analogWrite(motor_pin,128);
Serial.println("med");
delay(1000);
analogWrite(motor_pin,64);
Serial.println("low");
delay(1000);
analogWrite(motor_pin,0);
Serial.println("off");
delay(1000);
}
*/

```

The last part at the bottom that is between the /\* \*/ is a test code I wrote to test that everything is wired correctly. When I run the test code, the vibrating motor vibrates at different intensities just to confirm it's wired correctly, Ill also attach a picture if the circuit below

 ![Screenshot of Circuit](https://europe1.discourse-cdn.com/arduino/original/4X/8/b/c/8bcfefddac28d8b7ef32167eaa7e631978082ce5.png)

---

<div class="post-metadata">

**Author:** ![papanewgibby](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/papanewgibby/32/1244313_2.png) [@papanewgibby](https://forum.arduino.cc/u/papanewgibby)\
**Post date:** [November 5, 2025, 6:07pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/2 "2025-11-05T18:07:12Z")

</div>

I know I'm very new, and its probably some trivial problem from a lack of understanding but at this current moment I have absolutely no idea and would appreciate any help or feedback.  
🙂

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [November 5, 2025, 6:41pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/3 "2025-11-05T18:41:30Z")

</div>

The best approach is to start with code that works, and add one feature at a time.

When that feature is working correctly, add another one.

Fritzing diagrams are not appreciated on this forum because they are difficult or impossible to interpret (for example, what is the thing labeled "TMP") and almost always misleading. Hand drawn diagrams are preferred, with all parts and pinouts clearly labeled.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [November 5, 2025, 6:44pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/4 "2025-11-05T18:44:27Z")

</div>

in separate programs

1. test a switch
2. test one motor  
when they work you can start thinking about the complete project

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [November 5, 2025, 10:15pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/5 "2025-11-05T22:15:53Z")

</div>

Where did you get the code? Did AI write it?

Start by removing the neopixel code and vibrating motor code and just use a common LED. Once you fix that code, replace the LED code (a line or two) with a function to turn on or off a neopixel and one to do the motor.  
It's called divide and conquer, it's also known as modular programming. In a few years it will be second nature but those `if else if else if` might be less than ideal.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 8, 2025, 4:21am UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/6 "2025-11-08T04:21:55Z")

</div>

Your code is a lovely mess. In the allowed time, I noticed something when I added a print statement to one of the functions:

```arduino
void vibratePulse(int onTime, int offTime, int repeat, int intensity, int currentClass) {
 Serial.print("vibratePulse goblin "); Serial.println(goblinClass);

//... 

```

a7

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [November 8, 2025, 2:09pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/7 "2025-11-08T14:09:56Z")

</div>

If your circuit is indeed wired as shown the switch capacitors essentially don't exist.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/4/7/f/47f54d195b99e31cf698e36b62944eb255827ed6.png)

wire them like this -

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/d/5/bd5d1736a7f4f3a3a9ec515927732fd542aea149.png)

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 8, 2025, 4:38pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/8 "2025-11-08T16:38:34Z")

</div>

The flow is driven by global variables and control is strewn about the sketch.

Because you _updateGobClass_() all over the place, by the time you get to _vibratePulse_(), _goblinClass_ has been reset to zero.

I got things happening by dropping the condition that _goblinClass_ == _currentClass_ in that function:

```arduino
void vibratePulse(int onTime, int offTime, int repeat, int intensity, int currentClass) {
 for (int i = 0; i < repeat; i++) {
   analogWrite(VIB_PIN, intensity); //vibrating motor on
   delay(onTime); //wait
   analogWrite(VIB_PIN, 0); //vibrating motor on
   delay(offTime); //wait
 }
}

```

You can't care about that; the _for_ loop as you left it cannot change the value of _goblinClass_ and provide an early exit here as you have written in many other places.

A goblin _struct_ (or object) would make the code much shorter.

a7

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 7, 2026, 4:38pm UTC](https://forum.arduino.cc/t/need-help-with-some-code-unable-to-use-vibrating-motors/1412546/9 "2026-05-07T16:38:58Z")

</div>

Closed on May 7, 2026, 4:38pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
