Problem with code - 2 DC motors through an H-brdge functions run very strange.

Hi there, I'm trying to build a machine that makes a dottet line.. :wink:

Got everythng set up and it's working mechanical.
But my coding is a problem..

I wanted to design the thing by creating a function for DCmotor1 (moving) and a seperate function for
DCmotor2 (dotting)

And then i could just make a loop referncing to those two functions.

While the functions work if they are the main loop, they wont work when as a function.

code below..

This works - but is not how it should work.

int debug = false;

const int controlLeft = 5;
const int controlRight = 3;
const int enablePinA = 6;
const int buttonPin = 8;

int button = HIGH;

int tapDutyCycle = 250; // 1000 = 100% || 100 = 10%

boolean motorOn = false;
boolean motorDirection = false;  // false should be left

void setup()
{
  if (debug == true)
  {
    Serial.begin(9600);
  }
  
  pinMode(controlLeft, OUTPUT);
  pinMode(controlRight, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  
  digitalWrite(enablePinA, LOW);
 }

void loop()
{
    if (debug == true)
    {
       Serial.println("Tap");
    }

  digitalWrite(enablePinA, HIGH);

  digitalWrite(controlRight, HIGH);
  delayMicroseconds(tapDutyCycle); 
  digitalWrite(controlRight, LOW);
  delayMicroseconds(1000 - tapDutyCycle);
  
  digitalWrite(enablePinA, LOW);
}

This does not work, but is how i WANTED it to work..
I have added a button, to prevent the motor from running all the time.

int debug = false;

const int controlLeft = 5;
const int controlRight = 3;
const int enablePinA = 6;
const int buttonPin = 8;

int button = HIGH;

int tapDutyCycle = 250; // 1000 = 100% || 100 = 10%

boolean motorOn = false;
boolean motorDirection = false;  // false should be left

void setup()
{
  if (debug == true)
  {
    Serial.begin(9600);
  }
  
  pinMode(controlLeft, OUTPUT);
  pinMode(controlRight, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  
  digitalWrite(enablePinA, LOW);
 }

void tap()
{
    if (debug == true)
    {
       Serial.println("Tap");
    }

  digitalWrite(enablePinA, HIGH);

  digitalWrite(controlRight, HIGH);
  delayMicroseconds(tapDutyCycle); 
  digitalWrite(controlRight, LOW);
  delayMicroseconds(1000 - tapDutyCycle);
  
  digitalWrite(enablePinA, LOW);
}


void loop()
{
 button = digitalRead(buttonPin);
  if (debug == true)
  {
    Serial.print("Button state : ");
    Serial.print(button);
    Serial.println("");
    delay(50);
  }
 
 while(button == HIGH);
 {  
    tap();
 }

delay(500); // loop delay  
}

And i havent made the second function yet, since i cant get the first to run as intended.

Any ideas?

Your problem is with this:

 while(button == HIGH);
 {  
    tap();
 }

Ask yourself this question:

What in the while loop will change the state of "button".

Answer:

Nothing.

Result?

The while loop will run forever.

ChilliTronix:
Your problem is with this:

 while(button == HIGH);


    tap();
}




Ask yourself this question:

What in the while loop will change the state of "button".

Answer:

Nothing.

Result?

The while loop will run forever.

haha right,, gotta add that button read again :wink:
Gonna try it out :smiley:

Take a look at the blink without delay example here, and this state machine example here. It may give you some better ideas.

 while(button == HIGH);
 {  
    tap();
 }

You might also want to ask yourself about the extent of the while statement. With the semicolon on the end, THAT is the extent of what will happen when button is HIGH.

Of course, button as the name of the variable holding the state of a pin is stupid. But that's a separate issue.