Sparkfun Pro Micro 5V 16MHz - L298N - two DC motors at once

Hi there people,

I hope you guys can help me out with my project.

Here's what I want to do (the video attached should illustrate it i guess):

I have a 12 V DC Motor, which draws about 1.7A at full speed (http://www.johnsonmotor.com/en/products/dc-motors/standard-dc-motors/low-voltage-dc-motors/datasheets/NF213G-023-metric.pdf) along with a 1,0µ Multilayer ceramic capacitor between its poles. I built a gearbox around it and mount it to a frame. Then there's a threaded shaft which rotates and pushes or pulls a counterpiece away or towards itself. This makes another piece of frame rotate.

To stear this guy with my Sparkfun Pro Micro I use this L298N Driver http://www.ebay.de/itm/Dual-H-Brucke-DC-Stepper-Motor-Treiber-Controller-Board-Modul-Arduino-L298N-AR-/301044405289?pt=Bauteile&hash=item4617a51429

I don't know if you can see it in the video, but i attached a 10kOhm potentiometer, which constantly gives a feedback of the motors position. Now i tell the motor to move according to a control curve, which consists of an int array of "positions" to reach. The motor moves towards the prompted direction. As soon as the potentiometer returns the respective list value the code moves on to the next value in the control curve.
The code looks like this:

// Pro Micro Nr. D  controlling agent   0 and 5
 
int curveA[] = {570, 570, 570, 570, 570, 576, 535, 535, 535, 534, 534, 533, 533, 533, 532, 532, 531, 531, 530, 530, 530, 529, 529, 528, 528, 527, 524, 525, 525, 525, 525, 525, 524, 523, 522, 520, 519, 517, 516, 515, 513, 511, 509, 506, 504, 501, 499, 497, 495, 494, 492, 491, 488, 486, 482, 479, 475, 471, 468, 464, 462, 460, 458, 457, 456, 455, 452, 448, 443, 440, 440, 441, 442, 442};

int curve_length; 
 
int red = 15;

int poti1 = A0;    // select the input pin for the potentiometer

int led = 17;          //led pin define

int EnA = 4;           // both the digital enable Pins (always turned HIGH)

int In1 = 3;           // analog pins for motors
int In2 = 5;

int motor_strength = 250;  //motor strength



void setup() {
  
  pinMode(red, INPUT);
  
  pinMode(led, OUTPUT);
  pinMode(EnA, OUTPUT);
  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  
  Serial.begin(9600);
  Serial1.begin(9600);
  
  curve_length = sizeof(curveA) / sizeof(int);
}



//main loop


void loop() {
  
    int i;
    for(i = 0; i < curve_length; i++)
    {
         analogWrite(In1, 0);      // set all motor strengths to zero
         analogWrite(In2, 0);       
    
         
       if (digitalRead(red) == HIGH)
       { 
         int value = curveA[i];
         go(value);  
       }
    }
 signal(500);
 }




//---------------------------------------------------------------------------------------
// move void


void go(int value)
{
  while(analogRead(poti1) < value)
  { 
     analogWrite(In1, motor_strength);
     if (analogRead(poti1) >= value)
     {analogWrite(In1, 0);
     return;}
   }
  
  
  while(analogRead(poti1) > value)
  { 
     analogWrite(In2, motor_strength);
     if (analogRead(poti1) <= value)
     {analogWrite(In2, 0);
     return;}
   }
}


  
//---------------------------------------------------------------------------------------
// signal definition

void signal(int wait){
        digitalWrite(led, HIGH);
   delay(wait);
        digitalWrite(led, LOW);
   delay(wait/5);
        digitalWrite(led, HIGH);
   delay(wait/5);
        digitalWrite(led, LOW);
   delay(wait/5);
 }

This works quite well, but here's the problem:

I need to do this with two motors simultaneously, but as soon as I upload code which intends to read two potentiometers and read two different curve control arrays, the connections to the board breaks down. it "bricks", becomes unaccessible and i have to reset it manually to upload something else.

The code I tested looks something like this:

...........
void loop() {
  
    int i;
    for(i = 0; i < curve_length; i++)
    {
         analogWrite(In1, 0);      // set all motor strengths to zero
         analogWrite(In2, 0);
         analogWrite(In3, 0);       
         analogWrite(In4, 0);    
         
       if (digitalRead(red) == HIGH)
       { 
         int valueA = curveA[i];
         int valueB = curveB[i];
         go1(valueA);  
         go2(valueB);
       }
    }
 signal(500);
 }.............

with "go1" and "go1" and the inputs and outputs modified respectively.

I have no clue why, but I was doing some tests and everytime i try to do anything that requires to write two analog signals in correlation to read analog from two potentiometers fails miserably.

could it be a problem with the current??? Maybe the Pro Micro can't provide enough current to give signals to so many pins???

At first I thought it's a problem with two while-loops interfering so I played around with code without these loops, but it's the same problem: as soon as i read to potentiometers that guy bricks

Does anyone have suggestions?
Thanks a lot

Benjamin

Mein Film.mp4 (2.32 MB)

How are you distributing 12V and 5V power to everything? And grounds? You
need to keep the signal ground wiring separate from the high current motor ground
wiring. How is the 5V being generated (from the 12V?) and what power supply do you
have?

Do you know the stall current for your motors? Is 1.7A the no-load or full-load
current?

Hi MarkT,

yes the 5V for the SparkFun comes from the Motor Driver:

I have a large external power supply (two old computer power supplies, with 12V 22A and 12V 17 A) because i actually have 22 of these pieces. From there I wired the drivers parallel, from the drivers I supply the 5V for the SparkFun (Raw input) and the sparkfun itself supplies the potentiometers.
However, i think i need to check again if i kept the signal circuits completely seperate. I might have messed up something there

1.7A is the motor's full load current. i don'T actually know the stall current.

EDIT:
Do you mean I should also use the small GND on the driver to ground the sparkfun?? because there are actually two

OMG after over a week of searching I just found the error.

thanks a lot for the quick reply MarkT but the error was simply, that my arrays for control curves were too long! I had two lists each of which consisted of 1023 integers. I shortened them and know it works perfectly. oh my god wow thanks again mark