Go Down

Topic: Signal not going low at time when pulse tain ends; two different duty cycles (Read 169 times) previous topic - next topic

jeon0169

Hello I'm working on a project that involves a micro-drive motor to move up and down based on direction pin and speed pin. I'm utilizing a pulse train at 30% duty cycle for the speed pin. The direction pin should be high the entire time of the pulse train. However there is a delay before it goes low and and does not match the pulse train exactly; therefore remains high for to long I have provided my code and oscilloscope image to show what I mean. The purple plot is the direction pin that is remaining high for to long.
Code: [Select]
/*DC Motor simulation to prove tha Arduino can control the Microdrive as a
 control system negating the use of TDT*/
 
    //int enablePin = 11;
    int speedPin = 10;
    int upIn2 = 9;
    int v = 0;
   
void setup()
{
  // put your setup code here, to run once:
      pinMode(speedPin, OUTPUT);
      digitalWrite(speedPin, LOW);
      pinMode(upIn2, OUTPUT);
      digitalWrite(upIn2, LOW); // Low for now but will need to be used to go in reverse direction
      //digitalWrite(enablePin, HIGH);
         
}

void loop()
  {
   
   
     while(v<=10)  //setting number of overall trials
     {

       
        digitalWrite(upIn2, HIGH);
                                         //movement pin high for 100 ms; entire length of pulse train
       
       
          for(int i = 1; i<=5; i++)       //loop to increment 5 pulses forward
           
            {
             
             
              //30% duty cycle pulse train
             
              digitalWrite(speedPin, HIGH);     //Pulse width of 3 ms
              delay(30);
              digitalWrite(speedPin,LOW);       //off width of 4 ms
              delay(70);
   
            }

            // disengaging all movement and speed pins for downward movement
           
             
            digitalWrite(upIn2, LOW);                         // direction pin goes low
           
            digitalWrite(speedPin,LOW);
            delay(100);
            v++;   
             
     }

     
  }

       
           
           

   
   

slipstick

All that trace shows is the delay that you have written in there   - delay(70) after the pulse goes low and before the code moves on to set upIn2 low.

Steve

jeon0169

yes there is that delay but the question is at the of the pulse train with respect to the blow trace I want it to go low with the end of the yellow trace not the end of the 70 delay that follows. I essentially want the traces to line up like the image shown below.

slipstick

If you want to run two things with independent timing then the proper way of doing it is to change the timing to use millis() instead of delay()s.

But you could also do it simply by rearranging the way you create the main pulse train. Look at the timing in detail, particularly the part you haven't shown which is how long the gap is between both signals going low and them both going high again. 100ms? 170ms? Something else?

If you really want the code to stay simple forget the for loop and just repeat the up, delay30, down delay70 code 5 times. BTW delay(30) isn't 3ms and delay(70) definitely isn't 4ms so the comments aren't helping.

Steve

Go Up