Pages: [1]   Go Down
Author Topic: Calling Functions NOT working as expected...  (Read 706 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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?
« Last Edit: March 15, 2011, 11:44:11 am by Everseeker » Logged

UK
Offline Offline
Full Member
***
Karma: 2
Posts: 110
Kittens eat Arduinos
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont see anything wrong with your code...

try replacing loop() with

Code:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Done.

all the LED's blinked once...

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

So:

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

What you want is
Code:
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.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Thanks for that... will solve my problems smiley

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

While (True){
//stuff
}

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

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The loop() function is an endless loop.
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Jump to: