Show Posts
Pages: [1]
1  Using Arduino / Motors, Mechanics, and Power / Re: How to use the L298 Compact Motor Driver on: March 16, 2011, 11:16:06 am
OK, Here is the final, WORKING code. Feel free to use this as a starting point for projects...
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 LEDPin = 13; // Telltale LED
const int Duration = 2000;
void setup()
{
    pinMode(PWMELPin, OUTPUT);
    pinMode(PWMERPin, OUTPUT);
    pinMode(L1Pin, OUTPUT);
    pinMode(L2Pin, OUTPUT);
    pinMode(L3Pin, OUTPUT);
    pinMode(L4Pin, OUTPUT);
    pinMode(LEDPin, OUTPUT);
}

//Main Loop
void loop()
{
    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);
}

// ***********************************************************************
// Motor Driver Routines**************************************************
// ***********************************************************************
// Function(Direction, Speed)
// Direction
// 1=Forward
// 0= Reverse
// Speed
// 1 = High/250
// 0 = Low/20
// ***********************************************************************
void MotorL(int Direction, int Speed)
{
    // Left Motor
    if (Speed == 1)
    {
        analogWrite(PWMELPin, 250);
    }
    else
    {
        analogWrite(PWMELPin, 20);
    }
    if (Direction == 1)
    {
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
    }
    else
    {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
    }
}

// ***********************************************************************
void MotorR(int Direction, int Speed)
{
    // Right Motor
    if (Speed == 1)
    {
        analogWrite(PWMERPin, 250);
    }
    else
    {
        analogWrite(PWMERPin, 20);
    }
    if (Direction == 1)
    {
        digitalWrite(L3Pin, HIGH);
        digitalWrite(L4Pin, LOW);
    }
    else
    {
        digitalWrite(L3Pin, LOW);
        digitalWrite(L4Pin, HIGH);
    }
}

// ***********************************************************************
Notes:
1. Do NOT bind the - pin from the L298 to the Arduino Ground... that is not needed
2. Do giggle at me for spending forever trying to figure out what was wrong, before someone pointed out that Speed = 0 is NOT the same as Speed == 0....sigh......
2  Using Arduino / Programming Questions / Re: Calling Functions NOT working as expected... on: March 16, 2011, 11:01:43 am
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
3  Using Arduino / Programming Questions / Re: Calling Functions NOT working as expected... on: March 15, 2011, 12:23:04 pm
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...
4  Using Arduino / Programming Questions / Re: Calling Functions NOT working as expected... on: March 15, 2011, 12:00:01 pm
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...)
5  Using Arduino / Programming Questions / Re: Help with if, then, else statements... on: March 15, 2011, 11:36:31 am
FWIW, I think you are overthinking your reply...
look at his code...


not a { or } in sight.... THAT looks like it was the main issue, not the logic part....
6  Using Arduino / Programming Questions / Calling Functions NOT working as expected... on: March 15, 2011, 11:31:10 am
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?
7  Using Arduino / Motors, Mechanics, and Power / Re: How to use the L298 Compact Motor Driver on: March 15, 2011, 06:40:14 am
Yes, resistors in place.
Arduino powered by usb, motor controller,  by a 9v battery.
Are you SURE I should run a wire from the 9v -  to the Arduino ground?

Edit to add:
when I run other programs, such as "Cylon lights" I see all the lines out DO work, and all the LEDs are connected to my breadboard correctly

this is looking more and more like an issue with my code...

Am I configuring the Function correctly?
it looks like NOTHING happens in them.............
8  Using Arduino / Motors, Mechanics, and Power / Re: How to use the L298 Compact Motor Driver on: March 14, 2011, 07:51:48 pm
OK, I rechecked everything... then decided to bypass the entire Motor Controller, passing all 6 thru LEDS

I quickly discovered 1 issue...
My Left and Right motor controller subroutines were the same
EXACTLY the same... right down to the pins (D'oh!)
here's the "correct" 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);
        }
    }
// ***********************************************************************
That said, when I run this, both "E", L1, and L3 pins go on(High),  "L2 and L4" pins stay off(Low) they never move, but LED pin 13 slowly blinks

Ummm... when I add a pin 12 LED and stick that in the L side subroutine (as shown above).... 12 never blinks/flickers. it just stays ON......
as if the subroutines are not working...????
(NEW to C programming... am I missing something else???)
9  Using Arduino / Motors, Mechanics, and Power / Re: How to use the L298 Compact Motor Driver on: March 14, 2011, 04:18:10 pm
well, couple problems:
1. The motors are not that big (the GM8's actually)
2. Meter shows 0v to them

I am suspecting there's something more ... basic ... that I am missing
Am I supposed to ... I dunno... Connect the grounds of the 2 together?
stick that regulated 5V somewhere? (No, not gonna put it THERE)

What should I do next?
10  Using Arduino / Motors, Mechanics, and Power / [Working Code]How to use the L298 Compact Motor Driver on: March 14, 2011, 12:42:00 pm
OK, Got this little puppy for a Sumo project.
Put it together (only burned myself once) and tested it IAW the directions... it's good to go (yea!)
Next, I wrote this:
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 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);
    }
//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(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
void MotorR(int Direction, int Speed)
    {
    // Left Motor
    if (Speed = 1)
        {
        analogWrite(PWMELPin, 250);
        }
    else
        {
        analogWrite(PWMELPin, 50);
        }
    if (Direction = 1)
        {
        digitalWrite(L1Pin, HIGH);
        digitalWrite(L2Pin, LOW);
        }
    else
        {
        digitalWrite(L1Pin, LOW);
        digitalWrite(L2Pin, HIGH);
        }
    }
// ***********************************************************************
Seems simple enough...
Connected a 9v battery to +/- on the Motor Controller
Attached all 6 control wires as indicated in the code above
put a motor on each output
Connected the Arduino to laptop via USB (jumpered to draw power from USB)
Loaded the script and....

LED on 13 slowly blinks,so I know the code is running, but no joy on the motor control direction/speed controlling...

What (probably OBVIOUS) thing am I missing?
is there any other connection I need to make?
Pullup/Pulldown/pullout resistors?
11  Using Arduino / Programming Questions / Re: Hard Copy of the Reference pages? on: February 26, 2011, 12:32:11 pm
That's exactly what I was looking for!
About the. C++ reference... Are All things included?
I thought it was just a subset of C that was implemented
12  Using Arduino / Motors, Mechanics, and Power / Re: Solorbotics L298 Motor Driver Kit - Driver exist? on: February 25, 2011, 05:04:37 pm
Searched = yes
Success = no

working on doing it on my own
I THINK that if I keep the E pins sep, instead of binding them as shown in all the schematics, I can send a different PWN signal to each motor...

I THINK
13  Using Arduino / Programming Questions / Hard Copy of the Reference pages? on: February 25, 2011, 05:01:34 pm
1. I like the man pages here, but is there an easy way to print a hard copy?
2. where might I find detailed information on things like user defined function construction and such?
14  Using Arduino / Motors, Mechanics, and Power / Solorbotics L298 Motor Driver Kit - Driver exist? on: February 22, 2011, 01:18:58 pm
I have looked around, but have not yet found a simple, straightfwd, sketch for driving:2 motors, variable speed, either direction, from the L298 Motor Driver kit.
If anyone HAS such a sketch, could you post it?
If none exist, I'll have to write one... just want to avoid re-inventing the wheel smiley

--------------
Anyone want to chime in with helpful bits on PWM... I am kinda a babe-in-the-woods on this subject... at the moment smiley
Pages: [1]