Go Down

Topic: DC motor control (with L293D IC) gets skipped when controlling LEDs (via 74HC595 (Read 927 times) previous topic - next topic


Hi there,

I am trying to build an object that shows a heatmap with lights and moves at the same time. Getting both parts to work was quite easy, but now that I have put them together it's a whole different story.

The movement of the object is done using a DC motor, controlled by a L293D motor control chip. The motor has to move in one direction for some time and than go in the other direction, and this cycle continues. I used adafruits arduino lesson 15 (dc mototr reversing, http://goo.gl/Ma9Lq) for this and it works brilliantly for me.
I am using this motor: (http://goo.gl/WJYwU), I have tested others (servo and other DC) but they were either too fast, not strong enough or just not working for another reason, so changing the motor is not a possible solution really.

For the heatmap I am using 16 SMD RGB LEDs (datasheet: http://goo.gl/0kncU), attached to six 74HC595 shift registers. I control the color and brightness of the LEDs with the ShiftPWM library (http://goo.gl/ixsJA).

Seperately all of this works perfectly, but when I put them together a problem appears:
The DC motor changes direction every 500 milliseconds now, but when I tell a few LEDs to change color, this takes too much time/interrupts the program too long so that one movement command of the motor gets skipped. Let me illustrate:
How it should go:            How it goes when LEDs are instructed:
motor down                  motor down
motor down                  motor down
motor down                  motor down
motor up                     -*
motor up                     -*
motor up                     -*
motor down                  -*
motor down                  motor down
motor down                  motor down

*LEDs are instructed here, and the motor doesn't get commands during this time

This way, the motor does not get the command to go back up before the second command to go down. Because of this the motor goes down for way too long and it (literally) crushes the object.

I don't know how to circumvent this interruption of the motor. One think I thought about is using two timers, but I don't know how to do this and I also don't know if it would work. If more information is needed, please ask me and I'll try to provide it. I'm a beginner when it comes to arduino and electronics, but usually I pick up things relatively fast.

I am using an Arduino Uno v3 and arduino software 1.0.5. A Fritzing file and an image file of my hardware setup are attached as well as my arduino file.

This is my code:

Code: [Select]
const int ShiftPWM_latchPin=8;
const bool ShiftPWM_invertOutputs = false;
const bool ShiftPWM_balanceLoad = false;
#include <ShiftPWM.h>
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 60;
int numRegisters = 7;
int numRGBleds = numRegisters*8/3;
int enablePin = 6;
int in1Pin = 10;
int in2Pin = 9;
int speed = 150;
boolean motorOn = false;
boolean motorOff = false;

unsigned long time;

void setup(){
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  for(int i=1; i<17; i++){
    setLed(i, 4, 176);

void loop(){
  time = (round(millis()/5)*5);
  if(motorOff == false){
    if((time % 1000) < 500) {
      motorOn = true;
      if((time % 1000) == 0) {
        Serial.println(" : Motor up");
    if((time % 1000) > 500) {
      motorOn = false;
      if((time % 1000) == 510) {
        Serial.println(" : Motor down");

  if(time >= 10000){
    motorOff = true;
    setMotor(0, motorOn);
    Serial.println("- - - END OF STORY - - -");

void setMotor(int speed, boolean reverse){
  analogWrite(enablePin, speed);
  digitalWrite(in1Pin, ! reverse);
  digitalWrite(in2Pin, reverse);

void setLed(int ledId, int H_v, int S_v) {
  String ledColorString1 = String("Led set: ");
  String ledColorString2 = String(" HSV: ");
  Serial.print(" : ");
  Serial.println(ledColorString1 + (ledId-1) + ledColorString2 +H_v+" "+S_v);
  ShiftPWM.SetHSV(ledId-1, H_v, S_v, 255);

void setLedOff(int ledId) {
  String ledSetOffString1 = String("Led set: ");
  String ledSetOffString2 = String(" off");
  Serial.print(" : ");
  Serial.println(ledSetOffString1 + (ledId-1) + ledSetOffString2);
  ShiftPWM.SetOne(ledId-1, 0);

void LEDs() {
  switch (time) {
  case 3540:
    Serial.println("case 3540");
    setLed(12, 347, 222);
    setLed(13, 359, 255);
  case 5000:
    Serial.println("case 5000");
    setLed(2, 347, 222);
    setLed(3, 347, 222);
    setLed(4, 347, 222);
    setLed(5, 347, 222);
    setLed(6, 347, 222);
    setLed(7, 347, 222);
    setLed(11, 347, 222);
  case 5510:
    Serial.println("case 5510");
    setLed(4, 359, 255);
    setLed(6, 359, 255);
    setLed(11, 359, 255);
  case 7490:
    Serial.println("case 7490");
    setLed(4, 347, 222);
    setLed(2, 359, 255);


Go Up