I'm sure I'm Missing Something Simple...

Hey folks! I’m hoping somebody can bring clarity to my confusion.
My program here uses a pair of DIO pins to set an hbridge high/low in order to start/brake a dc motor connected to an hbridge. My digitalwrite commands, when used alone, preform the actions requested, so that portion works. The main program loop is just a debug test to turn the motor on, pause, rinse, repeat.

What is strange is that in my program below instead of the motor turning for 2.5 seconds, pausing for .5 seconds, then looping. It does the exact opp. It spins for .5 secs, then sits idle for 2.5, then loops.

I feel this might be a basic logic failure on my part…who knows. Any help would be loved! (Arduino Uno, btw)

Thanks!

int Ap = 1;
int Am = 2;
int Bp = 3;
int Bm = 4;

void setup()
{
  pinMode(Ap, OUTPUT);
  pinMode(Am, OUTPUT);
  pinMode(Bp, OUTPUT);
  pinMode(Bm, OUTPUT);
  digitalWrite(Ap, LOW);
  digitalWrite(Am, LOW); 
  digitalWrite(Bp, LOW);
  digitalWrite(Bm, LOW); 
}

void loop()
{
  
  Forward('A', 2500);
  delay(500);
  
}


void Forward(char Motor, int SpinTime)
{
 
   if (Motor == 'A')
    {
     digitalWrite(Ap, HIGH);
     digitalWrite(Am, LOW);
     delay(SpinTime);
     Brake(Motor);
    }
  
  if (Motor == 'B')
   {
     digitalWrite(Bp, HIGH);
     digitalWrite(Bm, LOW);
     delay(SpinTime);
     Brake(Motor);
   }
  
}


void Brake(char Motor)
{
  if (Motor == 'A')
  {
   digitalWrite(Ap, LOW);
   digitalWrite(Am, LOW); 
  }
  
  if (Motor == 'B')
  {
   digitalWrite(Bp, LOW);
   digitalWrite(Bm, LOW); 
  }
  
}

Most likely not helpful, but I can't find anything wrong with the code. It should, as far as I can tell, behave as you describe. Are you sure you're uploading the code? Or that something has gone wrong with the wiring?

Let us know if you resolve it! :)

Where is spintime defined? If not defined, wouldn't it just be 0? You could SerialPrintln (SpinTime); and see what comes out.

delay(SpinTime);

In the prototype for Forward:

void Forward(char Motor, int SpinTime)

If it wasn't declared, it would be a syntax error.

Where does SpinTime come from?

As CR says it would probably be 0 but then that would mean no delay (or 65 secs depending on how the delay() function is written).

Either way that doesn't explain the actions you are seeing.


Rob

The function is called from loop():

Forward('A', 2500);

Am I the only person that saw this?

ccfreak2k: Am I the only person that saw this?

I've already said I cannot see anything wrong with the code, so you were not the only person to see it :)

The only issue I see is this:

int Ap = 1;
int Am = 2;
int Bp = 3;
int Bm = 4;

Using pin 1 is generally to be avoided, since that is part of the serial port. The variables should be declared const to, to prevent inadvertent changes in the rest of the code.

try sticking some LEDs (with suitable resistors) in place of the motor(s)
you’d be able to see what’s going on then!

PaulS; Bingo! Moved my pin set from 1-4 to 2-5, problem solved. Not sure why though… Odd.
Am I correct in thinking that dio pin states persist through the ‘delay’ function?
I’ll switch those to const int variables as well. Any other best-practice areas I’m glossing over here? I’m new to this :slight_smile:

Thanks everyone for your suggestions!

“dio pin states persist through the ‘delay’ function”
Yes. Set an output high, it will stay high until you set it lo.
Set it low, it will stay low until you set it hi.
Even if you go into sleep mode, and come back out.

As for not using pins D0, D1 - was your USB connecter to a PC for example plugged in while you were testing? That could have affected the operation of the pins also. I try not to use those in my design so I can program in place, or have Serialprint available as a debug option.