(SOLVED) Using 74HC595 shift register with ULN2003 transistor chip

If anyone is seeing this now or using it as reference, I just wanted to keep you updated and let you know that all it needed was the == code fixed and a 10uf capacitor added into the circuit.

So I’m trying to turn 7 motors on/off non-sequentially with the 74HC595 shift register and ULN2003 transistor chip. I got the motors off of amazon so I’ll send the specs of those in attachments. I’ll also put the circuit schematic in the attachments below too. The code I’ve made/am using compiles, but once uploaded, doesn’t turn the motors on or off or anything. I’ll leave the code below just in the text as well. The only part of the code I’m not too sure about is the i++ section (was originally used in a sequential led fading code.) but whenever I take that portion out or try to mod it, it says error and won’t allow me to compile. Anyway though, thank you advance and I appreciate any help on my first post.

#define PIN_DATA 2
#define PIN_CLOCK 3
#define PIN_LATCH 4
int latchPin = 5;  // Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 6; // Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 4;  // Data pin of 74HC595 is connected to Digital pin 4

byte motors = 0;

int MotorValue1 = LOW;            
unsigned long previousMillis1 = 0;       
long OnTime1 = 15;           
long OffTime1 = 5;          

int MotorValue2 = LOW;             
unsigned long previousMillis2 = 0;        
long OnTime2 = 5;           
long OffTime2 = 15;         

int MotorValue3 = LOW;             
unsigned long previousMillis3 = 0;        
long OnTime3 = 5;           
long OffTime3 = 15; 

int MotorValue4 = LOW;            
unsigned long previousMillis4 = 0;       
long OnTime4 = 15;           
long OffTime4 = 5; 

int MotorValue5 = LOW;            
unsigned long previousMillis5 = 0;       
long OnTime5 = 15;           
long OffTime5 = 5; 

int MotorValue6 = LOW;            
unsigned long previousMillis6 = 0;       
long OnTime6 = 15;           
long OffTime6 = 5; 

int MotorValue7 = LOW;            
unsigned long previousMillis7 = 0;       
long OnTime7 = 15;           
long OffTime7 = 5;  

void setup() 
{
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);  
pinMode(clockPin, OUTPUT);
// set the digital pin as output:
Serial.begin(9600);
}

void loop()
{

motors = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
  bitSet(motors, i);
  updateShiftRegister();
  delay(500);
}

if (MotorValue1 == 0)
{
MotorValue1 = LOW;
}
else if (MotorValue1 == 1)
{
MotorValue1 == HIGH;
}

if (MotorValue2 == 0)
{
MotorValue2 == LOW;
}
else if (MotorValue2 == 1)
{
MotorValue2 == HIGH;
}

if (MotorValue3 == 0)
{
MotorValue3 == LOW;
}
else if (MotorValue3 == 1)
{
MotorValue3 == HIGH;
}

if (MotorValue4 == 0)
{
MotorValue4 == LOW;
}
else if (MotorValue4 == 1)
{
MotorValue4 == HIGH;
}

if (MotorValue5 == 0)
{
MotorValue5 == LOW;
}
else if (MotorValue5 == 1) 
{
MotorValue5 == HIGH;
}

if (MotorValue6 == 0)
{
MotorValue6 == LOW;
}
else if (MotorValue6 == 1)
{
MotorValue6 == HIGH;
}

if (MotorValue7 == 0)
{
MotorValue7 == LOW;
}
else if (MotorValue7 == 1)
{
MotorValue7 == HIGH;
}

unsigned long currentMillis = millis();

if((MotorValue1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
 MotorValue1 = LOW;  
 previousMillis1 = currentMillis;  
 digitalWrite(motors, MotorValue1);  
}
else if ((MotorValue1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
 MotorValue1 = HIGH;  
 previousMillis1 = currentMillis;   
 digitalWrite(motors, MotorValue1);      
}

if((MotorValue2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
{
 MotorValue2 = LOW;
 previousMillis2 = currentMillis;  
 digitalWrite(motors, MotorValue2); 
}
else if ((MotorValue2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
{
 MotorValue2 = HIGH;  
 previousMillis2 = currentMillis;   
 digitalWrite(motors, MotorValue2);      
}

if((MotorValue3 == HIGH) && (currentMillis - previousMillis3 >= OnTime3))
{
 MotorValue3 = LOW;
 previousMillis3 = currentMillis;  
 digitalWrite(motors, MotorValue3); 
}
else if ((MotorValue3 == LOW) && (currentMillis - previousMillis3 >= OffTime3))
{
 MotorValue3 = HIGH;  
 previousMillis3 = currentMillis;   
 digitalWrite(motors, MotorValue3);      
}

if((MotorValue4 == HIGH) && (currentMillis - previousMillis4 >= OnTime4))
{
 MotorValue4 = LOW;
 previousMillis4 = currentMillis;  
 digitalWrite(motors, MotorValue4); 
}
else if ((MotorValue4 == LOW) && (currentMillis - previousMillis4 >= OffTime4))
{
 MotorValue4 = HIGH;  
 previousMillis4 = currentMillis;   
 digitalWrite(motors, MotorValue4);      
}

if((MotorValue5 == HIGH) && (currentMillis - previousMillis5 >= OnTime5))
{
 MotorValue5 = LOW;
 previousMillis5 = currentMillis;  
 digitalWrite(motors, MotorValue5); 
}
else if ((MotorValue5 == LOW) && (currentMillis - previousMillis5 >= OffTime5))
{
 MotorValue5 = HIGH;  
 previousMillis5 = currentMillis;   
 digitalWrite(motors, MotorValue5);      
}

if((MotorValue6 == HIGH) && (currentMillis - previousMillis6 >= OnTime6))
{
 MotorValue6 = LOW;
 previousMillis6 = currentMillis;  
 digitalWrite(motors, MotorValue6); 
}
else if ((MotorValue6 == LOW) && (currentMillis - previousMillis6 >= OffTime6))
{
 MotorValue6 = HIGH;  
 previousMillis6 = currentMillis;   
 digitalWrite(motors, MotorValue6);      
}

if((MotorValue7 == HIGH) && (currentMillis - previousMillis7 >= OnTime7))
{
 MotorValue7 = LOW;
 previousMillis7 = currentMillis;  
 digitalWrite(motors, MotorValue7); 
}
else if ((MotorValue7 == LOW) && (currentMillis - previousMillis7 >= OffTime7))
{
 MotorValue7 = HIGH;  
 previousMillis7 = currentMillis;   
 digitalWrite(motors, MotorValue7); 
}  
}

void updateShiftRegister()
{
 digitalWrite(latchPin, LOW);
 shiftOut(dataPin, clockPin, LSBFIRST, motors);
 digitalWrite(latchPin, HIGH);
}

Right, first things first.

Please go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code (but it needs to be the complete code) as such so we can examine it conveniently and accurately. Do not post a ".ino" file as an attachment - that means that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, the code you post could well be garbled and is certainly anything but easy to read, so you will find little enthusiasm for assistance with it.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.

IMG_2596.PNG

IMG_2595.PNG

I think a TPIC6B595 could probably simplify the wiring. Have to check the current draw of those vibrators.

Mind you, the vibrators are rated at 3 V so if you are using 5 V, then the antique ULN2803 will conveniently drop the difference in voltage for you. :roll_eyes:

As a first approximation reexamine your code with the understanding that '==' is a comparison and '=' is an assignment.

I change the == to = as noted here for your else if( ) sections, and it compiles ok.

  if (MotorValue1 == 0) // 0 is the same as LOW
  {
    MotorValue1 = LOW; // So what does this do for you?
  }
  else if (MotorValue1 == 1) // 1 is the same as HIGH
  {
    MotorValue1 = HIGH; // =  So what does this do for you?
  }

I don't understand what you are doing by setting a 0 to a LOW, or 1 to a HIGH. That doesn't change anything.

Looking at them, I'm wondering that those are LRA haptic motors.
Have you you managed to get even one going?

Like I said, this is my first post, so I will modify it to fit the instructions shortly. I also don't have URL links to the images so I don't really understand how to fix that.

I will put in = and test those out and I will let you guys know if that compiles. By having any 0 or 1 value assigned to a motor and making it = to LOW or HIGH, I was hoping to just give the motors like a set on or off function, but I am seeing how that's not making sense to have both... :relaxed:

I have managed to get them going with the code that doesn't regard the shift register. I had a different circuit set up at the time, but I tried to maintain the same circuit as much as I could while putting in and trying to make use of the shift register.

Again, thank you all for your time and patience with me. I appreciate the help.