Millis() function to run twostepper at the same time Arduino mega p6

Поділитися
Вставка
  • Опубліковано 28 січ 2025

КОМЕНТАРІ • 1

  • @houssammahmoud2127
    @houssammahmoud2127  5 місяців тому

    // Define the connection pins for Motor 1:
    #define stepPin1 12 //pulse
    #define dirPin1 11 //dir
    #define enaPin1 10 //ena
    // Define the connection pins for Motor 2:
    #define stepPin2 9 //pulse
    #define dirPin2 8 //dir
    #define enaPin2 7 //ena
    // Variables to track the timing of steps
    unsigned long previousMillisMotor1 = 0;
    unsigned long previousMillisMotor2 = 0;
    const long intervalMotor1 = 100; // Interval at which to step motor 1 (speed)(microseconds)
    const long intervalMotor2 = 50; // Interval at which to step motor 2 (speed)(microseconds)
    //-------
    void setup() {
    // put your setup code here, to run once:
    pinMode(stepPin1,OUTPUT);//Pul
    pinMode(dirPin1,OUTPUT);//Dir
    pinMode(enaPin1,OUTPUT);
    pinMode(stepPin2,OUTPUT);//Pul
    pinMode(dirPin2,OUTPUT);//Dir
    pinMode(enaPin2,OUTPUT);
    }
    //-------
    void loop() {
    unsigned long currentMillis = millis();
    // Motor 1
    if (currentMillis - previousMillisMotor1 >= intervalMotor1) {
    previousMillisMotor1 = currentMillis;
    runMotor1(100);
    }
    // Motor 2
    if (currentMillis - previousMillisMotor2 >= intervalMotor2) {
    previousMillisMotor2 = currentMillis;
    runMotor2(100);
    }


    }
    //-------
    // Function to step Motor 1
    void runMotor1(int stepsPerRevolution) {
    static int stepCount = 0;
    if (stepCount < stepsPerRevolution) {
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(50); // Short pulse to trigger the step
    digitalWrite(stepPin1, LOW);
    stepCount++;
    }
    }
    // Function to step Motor 2
    void runMotor2(int stepsPerRevolution ) {
    static int stepCount = 0;
    if (stepCount < stepsPerRevolution) {
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(50); // Short pulse to trigger the step
    digitalWrite(stepPin2, LOW);
    stepCount++;
    }
    }