0

Pause without delay
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pause without delay

by Joe36 on Sat Apr 14, 2018 3:51 pm

I'll try to keep this pretty simple, I got an old used gauge cluster from a chevy truck that didnt work, it has 6 stepper motors x27.168 like the one on adafruit. I wanted to build a cool display so I ordered some a4988 stepper motor drivers and a metro mini, wired it up and I got it working but with delays in the code. What I want is for all the gauges to be constantly full sweeping and to do that they will need to step in different amounts and I cant use delays. I have some code that I put together from the blink without delay but I need a short pause 1-200 milliseconds in between switching directions or the motor gets thrown off. I have attached the code that I currently have.
Code: Select all | TOGGLE FULL SIZE
/*
Speedo pin 2,3
Tach pin 4,5
Oil pressure pin 6,7
Volts pin 8,9
Temp pin 10,11
Fuel pin 12,13

Temp is dirPinP,stepPinP

*/

const int dirPinS = 2;
const int stepPinS = 3;
const int dirPinT = 4;
const int stepPinT = 5;
const int dirPinO = 6;
const int stepPinO = 7;
const int dirPinV = 8;
const int stepPinV = 9;
const int dirPinP = 10; 
const int stepPinP = 11;
const int dirPinF = 12;
const int stepPinF = 13;
 


unsigned long previousMicros = 0;        // will store last time output was updated
unsigned long previousMillis = 0;
// constants won't change:
const long interval = 450;           // interval to pulse the driver (microseconds)
const long wait = 175;                // wait time before needle changes direction (milliseconds)

int pinState = HIGH;



void setup() {
  // Sets the output pins
  pinMode(stepPinS,OUTPUT);
  pinMode(dirPinS,OUTPUT);
  pinMode(stepPinT,OUTPUT);
  pinMode(dirPinT,OUTPUT);
  pinMode(stepPinO,OUTPUT);
  pinMode(dirPinO,OUTPUT);
  pinMode(stepPinV,OUTPUT);
  pinMode(dirPinV,OUTPUT);
  pinMode(stepPinP,OUTPUT);
  pinMode(dirPinP,OUTPUT);
  pinMode(stepPinF,OUTPUT);
  pinMode(dirPinF,OUTPUT);
}
void loop() {


  unsigned long currentMicros = micros();
  unsigned long currentMillis = millis();

   
     digitalWrite(dirPinS,HIGH);{
       
   for(int x = 0; x < 200; x++){ //steps in one direction
 if(currentMicros - previousMicros >= interval){
      previousMicros = currentMicros;  // Remember the time
 
 if (pinState == HIGH) {
      pinState = LOW;
    } else {
      pinState = HIGH;
    }
     
    digitalWrite(stepPinS,pinState);  // Update the pin
 
     }
    }
   }






 
digitalWrite(dirPinS,LOW);{
       
   for(int x = 0; x < 200; x++){//stepps in the other direction
 if(currentMicros - previousMicros >= interval){
      previousMicros = currentMicros;  // Remember the time
 
 if (pinState == HIGH) {
      pinState = LOW;
    } else {
      pinState = HIGH;
    }
     
    digitalWrite(stepPinS,pinState);  // Update the pin
   
 
   } 
  }
 }
}

Joe36
 
Posts: 24
Joined: Tue Dec 27, 2016 10:36 am

Re: Pause without delay

by adafruit_support_bill on Sun Apr 15, 2018 5:46 am

To manage multiple steppers, you would want to take an approach similar to the one used for sweeping multiple servos here:

https://learn.adafruit.com/multi-taskin ... 1/overview

adafruit_support_bill
 
Posts: 68551
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.