Incrementing a DC Motor with just PWM using Digital write; every 5 pulses

Hello I'm working on project where I have to increment a motor at 200ms high and 800 ms low the chip I'm using is and L293d to control the speed and direction of the motor. The motor has to change directions after every five pulses and the total cycle frequency of the up and down cycle must be 3Hz.

I figured using a for loop to control my pulses and digitalWrite() since it allows for more control of the on and off times of the pulses. The code is as such so far I have commented out the downward movement for now but you can look at it if you wish, I'm just trying to get the motor to pulse down at the pulse widths as specified above. The code is as such so far:

int enablePin = 11;
int downIn1 = 10;
int upIn2 = 9;

void setup() {
// put your setup code here, to run once:
pinMode(downIn1, OUTPUT);

pinMode(upIn2, OUTPUT);

pinMode(enablePin, OUTPUT);

}

void loop()
{

digitalWrite(upIn2, LOW); //setting Up pin on L293d Low for downward movement

for(int i = 1; i<=5; i++) //loop to increment 5 pulses down

{
digitalWrite(downIn1, HIGH); //Pulse width of 200 ms
delay(200);
digitalWrite(downIn1,LOW); //off width of 800 ms
delay(800);
}

/*digitalWrite(downIn1, LOW); // setting down pin to low so, can begin moving upward
/*setMotor(speed, reverse); //reversing direction
delay(1000);

void setMotor(int speed, boolean reverse) //reverse direction of electrode to up
{

analogWrite(enablePin, speed);
digitalWrite(up, ! reverse);
digitalWrite(in2Pin, reverse);

for(int i = 1; i<=5; i++) //moving electrode upward

{
digitalWrite(upIn2, HIGH); //Pulse width of 200 ms
delay(200);
digitalWrite(upIn2,LOW); //off width of 800 ms
delay(800);

}*/
}

I've also provided the breadboard schematic and the chip profile below, as well as a logic table for the chip that shows directionality for when certain pins are high and low.

The logic table was obtained from:

The breadboard schematic was obtained from

https://learn.adafruit.com/assets/2488

I'm pretty new to arduino so any help would be appreciated. I'm going to work on it some more probably rewire the chip and see if maybe I missed something, but any help would be appreciated but this is just step one of a multi step project; let me know if my code is on the riight track or if is completely off. The bread board image that I provided I'm not using the potentimeter or the button at all so the only wiring in to the chip

Thanks

l293.pdf (281 KB)

Logic for L293d chip.pdf (295 KB)

You should always use the code button </> when posting code so it looks like this and is easy to copy to a text editor

int enablePin = 11;
    int downIn1 = 10;
    int upIn2 = 9;
   
   

void setup() {
  // put your setup code here, to run once:
      pinMode(downIn1, OUTPUT);
     
      pinMode(upIn2, OUTPUT);
     
      pinMode(enablePin, OUTPUT);

}

void loop()
  {
   
   digitalWrite(upIn2, LOW);           //setting Up pin on L293d Low for downward movement
     
      for(int i = 1; i<=5; i++)       //loop to increment 5 pulses down
       
        {
          digitalWrite(downIn1, HIGH);     //Pulse width of 200 ms
          delay(200);
          digitalWrite(downIn1,LOW);       //off width of 800 ms
          delay(800);
        }



   /*digitalWrite(downIn1, LOW);        // setting down pin to low so, can begin moving upward
          /*setMotor(speed, reverse);       //reversing direction
          delay(1000);
         
              void setMotor(int speed, boolean reverse) //reverse direction of electrode to up
              {
               
                analogWrite(enablePin, speed);
                digitalWrite(up, ! reverse);
                digitalWrite(in2Pin, reverse);
               
 
       for(int i = 1; i<=5; i++)  //moving electrode upward
                 
           {
              digitalWrite(upIn2, HIGH); //Pulse width of 200 ms
              delay(200);
              digitalWrite(upIn2,LOW); //off width of 800 ms
              delay(800);

            }*/
          }

Your requirement seems very strange, but let's assume you have a reason for it.

You have set the pinMode for all the pins but you have not set the states of the enablePin and the upIn2 pin. You need to use digitalWrite() to make them either HIGH or LOW as required.

...R

Thanks man I finally got it to work for the downward direction or in my case to rotate a certain number of times and then stop, thanks for the assist. Oh about the unusal scope of the project its to simulate a biological phenomon called micromotion which is displacement for the brain, where it displaces due to breathing and pumping blood.

We are doing a bench top experiment will cells and and electrode that will move up and down by these displacements while cross correlating with action potential firing, and we want to use arduino to control our micro drive to simulate this. First I have to show that the control system can be done using a DC motor, because the drive is $10,000. So we don't want to be trouble shooting the control system with that