Go Down

Topic: Calling Functions NOT working as expected... (Read 766 times) previous topic - next topic

Everseeker

Mar 15, 2011, 05:31 pm Last Edit: Mar 15, 2011, 05:44 pm by Everseeker Reason: 1
OK, full disclosure: I orig. posted this in the Motors & Controllers section, thinking I was having an issue with my hardware.
I have since decided that this is not the case. First, the code:
Code: [Select]
// Motor Controls
const int PWMELPin = 10; // Enable PWM Left Motor
const int PWMERPin = 11; // Enable PWM Right Motor
const int L1Pin = 2; // L1 = Left Motor Lead 1
const int L2Pin = 3; // L2 = Left Motor Lead 2
const int L3Pin = 4; // L3 = Right Motor Lead 1
const int L4Pin = 5; // L4 = Right Motor Lead 2
const int LEDxPin = 12; // LEDx
const int LEDPin = 13; // LED
const int Duration = 1000;
void setup()
    {
    pinMode(PWMELPin, OUTPUT);
    pinMode(PWMERPin, OUTPUT);
    pinMode(L1Pin, OUTPUT);
    pinMode(L2Pin, OUTPUT);
    pinMode(L3Pin, OUTPUT);
    pinMode(L4Pin, OUTPUT);
    pinMode(LEDPin, OUTPUT);
    pinMode(LEDxPin, OUTPUT);    }
//Main Loop
void loop()
    {
    while (true)
        {
        MotorL(0, 1);
        MotorR(0, 1);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 1);
        MotorR(1, 1);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        MotorL(0, 0);
        MotorR(0, 0);
        digitalWrite(LEDPin, LOW);
        delay(Duration);
        MotorL(1, 0);
        MotorR(1, 0);
        digitalWrite(LEDPin, HIGH);
        delay(Duration);
        continue;
        }
    }
// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// (Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/50
// ***********************************************************************
void MotorL(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMELPin, 250);
        }
    else
        {
        analogWrite(PWMELPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(LEDxPin, HIGH);
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(LEDxPin, LOW);
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
void MotorR(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMERPin, 250);
        }
    else
        {
        analogWrite(PWMERPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(L3Pin, HIGH);
        digitalWrite(L4Pin, LOW);
        }
    else
        {
        digitalWrite(L3Pin, LOW);
        digitalWrite(L4Pin, HIGH);
        }
    }
// ***********************************************************************

Here's the problem. Running this code does NOT produce the expected results.
I took L1, L2, L3, L3, PWMEL, and PWMER to LEDS to check...
When I run this, L1, L3, PWMEL,PWMER, and LEDx turn on and STAY on
L2, L3 never so much as flicker
LED (Pin 13) Blinks on and off, like clockwork

This tells me that, Although the main loop is running (13 flashing), the 2 Motor
Driver Functions are NOT working as intended.
I am assuming that I am doing something BASIC wrong, but I don't see it....
Help?!?!

-----------
Edit to add...
I am using the following, in general, to call the motor functions...

        MotorL(0, 1);

Is passing an int "pure" (Not assigned to a variable) permitted?

dafid

I dont see anything wrong with your code...

try replacing loop() with

Code: [Select]

void loop()
{
int i ;

  for (i=2; i<=5; i++)
  {
    digitialWrite(i, HIGH) ;
    delay(1000) ;
    digitalWrite(i, LOW) ;
  }

  for (i=10; i<=13; i++)
  {
    digitialWrite(i, HIGH) ;
    delay(1000) ;
    digitalWrite(i, LOW) ;
  }
}

This treats all the pins alike.
Any differences in behaviour is then down to wiring.

Everseeker

Done.

all the LED's blinked once...

Wires are fine.
(I had the same thought... Ran a "Cylon" script :) but I ran yours, just in case...)

jraskell

direction = 1 is an assignment, not a comparison.  direction == 1 is a comparison.

So:

Code: [Select]
if (direction = 1)
will always evaluate to true, as you are assigning 1 to direction

What you want is
Code: [Select]
if (direction == 1)

You have the same error with your speed comparisons as well.

On a separate note, your while(true) is accomplishing nothing.  There's no point in having that code block.

Everseeker

Oh god... == is not the same as =

I KNOW that... I am not an idiot... I would never make that kind of mistaaaa...ummm.... errrr.....

oops

I guess I did... silly me :)
Thanks for that... will solve my problems :)

---------------------------------------------------------------------------------------------
The

While (True){
//stuff
}

construct was just a way to put the thing in an endless loop...

jraskell


Everseeker

Point taken.

Whenever I want something to sit and loop, I reflexively use a while true construct.
but yeah, I guess that's not needed in this case :)

Go Up