Need help with some code, unable to use vibrating motors

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.


/*
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

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.
:slight_smile:

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.

in separate programs

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

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.

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

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

//... 

a7

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

wire them like this -

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:

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