Pages: [1]   Go Down
Author Topic: Motor Sequencer  (Read 833 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to add a delay between two motors operating i.e if one motor is operating wait one second then start the next and vice versa, I am using an H Bridge to change the direction of the motors..
Code:
const int switchPin = 2;    // switch input
   const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
   const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
   const int enablePin = 9;    // H-bridge enable pin
   const int motor3Pin = 5;   //H-bridge leg 1
   const int motor4Pin = 6;   // H-Bridge leg 2
   const int enablePin1 = 8;
   int enaablePin1 = LOW;
   long previousmillis = 0;
   long interval = 1000;
   void setup() {
     // set the switch as an input:
     pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
     pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
     pinMode (motor3Pin, OUTPUT);
     pinMode(motor4Pin, OUTPUT);
 
 
    // set enablePin high so that motor can turn on:
     digitalWrite(enablePin, HIGH);
     
  }
  void loop() {
    unsigned long currentmillis = millis();
    if(currentmillis - previousmillis > interval) {
      previousmillis = currentmillis;
      if (enablePin1 == LOW);
     
      else
      interval;
      digitalWrite(enablePin1, HIGH);
    }
   
     // if the switch is high, motor will turn on one direction:
     if (digitalRead(switchPin) == HIGH) {
       digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
       digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
       digitalWrite(motor3Pin, LOW);
       digitalWrite(motor4Pin, HIGH);
     }
    // if the switch is low, motor will turn in the other direction:
     else {
       digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
       digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
       digitalWrite(motor3Pin, HIGH);
       digitalWrite(motor4Pin, LOW);
     }
   }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      if (enablePin1 == LOW);
     
      else
      interval;
What is this trying to do? The semicolon on the if statement looks out of place. The else body is not a function call.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried adapting the blink without delay code in Arduino examples with the h-bridge code I am using which is as follows
Code:
[const int switchPin = 2;    // switch input
   const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
   const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
   const int enablePin = 9;    // H-bridge enable pin
   const int motor3Pin = 5;   //H-bridge leg 1
   const int motor4Pin = 6;   // H-Bridge leg 2
   const int enablePin1 = 8;
   
   void setup() {
     // set the switch as an input:
     pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
     pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
     pinMode (motor3Pin, OUTPUT);
     pinMode(motor4Pin, OUTPUT);
     pinMode(enablePin1, OUTPUT);
 
 
    // set enablePin high so that motor can turn on:
     digitalWrite(enablePin, HIGH);
     digitalWrite(enablePin1, HIGH);
     
  }
  void loop() {
     // if the switch is high, motor will turn on one direction:
     if (digitalRead(switchPin) == HIGH) {
       digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
       digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
       digitalWrite(motor3Pin, LOW);
       digitalWrite(motor4Pin, HIGH);
     }
    // if the switch is low, motor will turn in the other direction:
     else {
       digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
       digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
       digitalWrite(motor3Pin, HIGH);
       digitalWrite(motor4Pin, LOW);
     }
   }/code]
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have tried adapting the blink without delay code in Arduino examples with the h-bridge code I am using which is as follows
That doesn't explain what that bit of code is supposed to do.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well to be honest I am not too sure? I tend to learn by trail and error and if I cant seem to work something out I ask here... Then I learn something new from the advice I get here..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have tried adapting the blink without delay code in Arduino examples with the h-bridge code I am using
I really can't see any evidence to support that assertion
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code with the h-bridge and blink without delay to try and acheive what I want is in the first post..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have code that runs two motors at the same time when the switch is pressed, in one direction, and in the other direction when the switch is not pressed. If you want one motor to start after the other, delay() is the simplest.

If you want to avoid delay, you need to think about a state machine. You have 4 states - one motor running forward, two motors running forward, one motor running backwards, and two motors running backwards. Depending on the state of the switch, you transition to one of two states. Depending on how long it has been since the last transition, you might need to transition to one of the other two states.

Finally, depending on the state, you activate certain motors certain ways.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried that earlier and couldn`t get it to work the way I wanted? have tried it again and now it`s doing what I want? perhaps I put the delay command in the wrong place last time? Thanks...
Logged

Pages: [1]   Go Up
Jump to: