2 Motoren mit unterschiedl. Intervall laufen lassen

Hallo miteinander,

ich habe erfolgreich zwei DC Motoren mit Hilfe eines Motoshields zum laufen gebracht.
Momentan laufen die Motoren synchron vor- oder rückwärts.

// you need this so you can use the adafruit motor shield library:
#include <AFMotor.h>

// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(3, MOTOR12_64KHZ);  

// we do it again, this time our second motor will be called "motor2" on port
// 4 of the motorshield:
AF_DCMotor motor2(4, MOTOR12_64KHZ);
// so now we have 2 motors called "motor" and "motor2"

void setup() {
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Motor test!");

// set the speed to 200 of 255 of "motor".  Note that 0 is stop,
// 255 is full speed:
motor.setSpeed(200);    

// set the speed to 200 of 255 of "motor2":
motor2.setspeed(200);
}

void loop() {
Serial.print("tick");

motor.run(FORWARD);      // turn it on going forward
motor2.run(FORWARD);  // motor 2 goes forward as well
delay(1000);

Serial.print("tock");
motor.run(BACKWARD);     // the other way
motor2.run(BACKWARD);  //again for motor 2
delay(1000);

Serial.print("tack");
motor.run(RELEASE);      // stopped
motor2.run(RELEASE);  // command motor 2 to stop
delay(1000);
}

Ich möchte jedoch, dass die Motoren unabhängig voneinander laufen.
Der erste Motor soll 2 Sekunden vorwärts laufen und 2 Sekunden pausieren
Der zweite Motor soll 1 Sekunde vorwärts laufen und 1 Sekunde pausieren.

Hat jemand eine Idee wie ich das hinbekomme?

Och komm, dieses Thema ist doch hier schon zigmal durchgekaut worden. Ob jetzt LEDs blinken oder Motoren laufen ist doch dabei egal, das Prinzip ist immer das gleiche.

handyaner: Hat jemand eine Idee wie ich das hinbekomme?

Beispiel Blinkwithoutdelay verstehen. dann erweitern.

Hallo miteinander,

danke für den Tipp.

bekommt man den code noch sauberer hin?

#include <AFMotor.h>

// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(1, MOTOR12_64KHZ);  

// we do it again, this time our second motor will be called "motor2" on port

AF_DCMotor motor2(2, MOTOR12_64KHZ);

// so now we have 2 motors called "motor" and "motor2"
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

int ledState2 = LOW;             // ledState used to set the LED
long previousMillis2 = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long intervalon = 1000;           // interval at which to blink (milliseconds)
long intervaloff = 5000;

long intervalon2 = 2000;           // interval at which to blink (milliseconds)
long intervaloff2 = 7000;

void setup() {
    Serial.begin(9600);           // set up Serial library at 9600 bps
    Serial.println("Motor test!");
    
    // set the speed to 200 of 255 of "motor".  Note that 0 is stop,
    // 255 is full speed:
    motor.setSpeed(200);    
    
    // set the speed to 200 of 255 of "motor2":
    motor2.setSpeed(200);  
   
   //pinMode(ledPin, OUTPUT);   
}

void loop() {
    Serial.print("tick");
     unsigned long currentMillis = millis();
  
    if(ledState == HIGH && currentMillis - previousMillis > intervalon ) {
       previousMillis = currentMillis;  
       ledState = LOW;
       motor.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState == LOW && currentMillis - previousMillis > intervaloff) {
       previousMillis = currentMillis;  
       ledState = HIGH;
       motor.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }
    
    unsigned long currentMillis2 = millis();
    if(ledState2 == HIGH && currentMillis2 - previousMillis2 > intervalon2 ) {
       previousMillis2 = currentMillis2;  
       ledState2 = LOW;
       motor2.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState2 == LOW && currentMillis2 - previousMillis2 > intervaloff2) {
       previousMillis2 = currentMillis;  
       ledState2 = HIGH;
       motor2.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }  
    
}