Advertisement
bld

Untitled

bld
Mar 16th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #define pulseLength 50  // Set the length of a pulse
  2. #define numOfPins 8     // Set the number of pins the microcontroller got
  3.  
  4. int stepsLeft[numOfPins];            //Array to store how many times it should flash the pin
  5. int stepDelay[numOfPins];            //Array to store how long a delay there should be between the pin state changing
  6. unsigned long lastStep[numOfPins];   //Array to store when the pin was changed last
  7.  
  8. void cyclePins()
  9. {
  10.     unsigned long now = millis();
  11.     for (int i = 0; i <= numOfPins; i++) // Run through the arrays to see if any pin should be changed
  12.     {
  13.         if (stepsLeft[i] > 0) // Do we have to do any steps on this pin?
  14.         {
  15.             if (now - lastStep[i] >= stepDelay[i]) // Calculate if we are at, or passed the set delay time between pulses
  16.             {
  17.                 lastStep[i] = now; // Set when we set the pin HIGH, so we can calculate when to set it LOW again
  18.                 digitalWrite(i, HIGH); // Set pin to HIGH
  19.             }
  20.            
  21.             if (now - lastStep[i] >= pulseLength) // Check if pin has been HIGH long enough
  22.             {
  23.                 digitalWrite(i, LOW); // Set pin to LOW
  24.                 stepsLeft[i]--; // Remove one step
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement