V1 Motor board

Hi everyone.
I'm very much a newbie but have spent the last 3 days cramming and it hasn't helped so..........

I have put a Rev1 motorshield onto an UNO Rev3.

Tried to get the following simple sketch to work:

[code]//Motor Shield 1-Channel DC Motor Demo
//by Randy Sarafan

//For more information see:
//https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

//*************************************************************/

void setup() {
  Serial.begin(9600);
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop(){
  
  //forward @ full speed
  Serial.println ("start");
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed
  
  delay(3000);
  Serial.println ("stop");
  digitalWrite(9, HIGH); //Engage the Brake for Channel A

  delay(1000);
  
  //backward @ half speed
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);   //Spins the motor on Channel A at half speed
  Serial.println ("reverse");
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  
  delay(1000);
  Serial.println ("end");
}

Yes, SORRY. I read all the "how to use this forum" stuff but I still can't work out how to use the code tags. Blame it on age (I do).

BUT, it gives you the details.

Code uploads, serial monitor runs through all the "printlns" but I get no output from ANY of the motor pins on the shield.

Tried another UNO, same thing.
Swapped the 293 chips on the shield, no joy.

I only tried this sketch from instructables because I was getting no joy from my own code.

Does it simply mean I've got a dead shield OR am I missing something obvious??????

Please help

Aussiewill

What happens if you connect the motor leads directly to the battery terminals?

.

Hi, mate.
Welcome to the forum.

Is this your shield?

Have you got a power supply connected to the power input terminals on the shield?

Tom.... :slight_smile:

Hi,
Thanks Tom, for your answer.

Yes, motor works OK at anything from about 3V up including 12Volt

And yes, I have 12V DC to the power terminals, UNO powered via USB (Power link on the shield removed)

The other question, NO that is not my board.
I have an original Version 1 with the 2 L293 chips, long discontinued by Arduino, I know, but don't want to toss it.
It is from DK Electronics, if that helps at all.

I've tried shifting to M3 and M4 and have swapped the L293 chips.

Nothing seems to work. Just the power light.
No error messages and the program loops quite happily.

I'm out of ideas.

Aussiewill

Hi, mate.
Like this. I have one too.
MotorDriverV1.jpg
So might give it a try with your code later, got to go shopping (hunter gathering at the shopping centre)

Tom.. :slight_smile:

“hunter gathering at the shopping centre”
LOL

Good luck with the hunt, Tom.
I'd appreciate if you would give it a run.
I've also tried it with the "instructables" single motor and 2 motor codes.
This time of the year it's going to take forever to get a replacement board too.

Have a Happy New Year (everybody)

Aussiewill

Hi,
The code in the instructibles is not working due to major changes to the board.

Try this code.
You will need to get AFMotor library
From here.. GitHub - adafruit/Adafruit-Motor-Shield-library: Adafruit Motor shield V1 firmware with basic Microstepping support. Works with all Arduinos and the Mega

// Adapted from.....By TG
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>


// DC motor on M3 and M4
AF_DCMotor motor1(1, MOTOR34_1KHZ);
AF_DCMotor motor2(2, MOTOR34_1KHZ);
int inByte;
int oldinByte = 0;
char charByte;

int j = 255;

void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor party!");
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  menu1();
}

void loop()
{
  /*  while (!Serial)
    {
      ; // wait for serial port to connect. Needed for native USB port only

    }
  */
  if (Serial.available() > 0)
  {
    //   Serial.print("Answering    ");
    inByte = Serial.read();
    //    Serial.write(inByte);
    //    Serial.print("   ");
    //    Serial.print(inByte);
    Serial.println();
    charByte = char(inByte);
  }
  if (inByte != oldinByte)
  {
    menu1();
    Serial.print(charByte);
    switch (inByte)
    {
      case 102:
        Serial.println("  Forward");
        forward();
        break;
      case 98:
        Serial.println("  Reverse");
        reverse();
        break;
      case 108:
        Serial.println("  Left");
        left();
        break;
      case 114:
        Serial.println("  Right");
        right();
        break;
      default:
        Serial.println("  Brake");
        brake();
        break;
    }
    oldinByte = inByte;
  }

}

void menu1()
{
  Serial.println("--------------------------------------------------------");
  Serial.println(" (f)orward, (b)backwards, (l)eft, (r)ight, anykey Brake ");
  Serial.println("--------------------------------------------------------");
}

void forward()
{
  motor1.run(FORWARD);
  motor1.setSpeed(175);
  motor2.run(FORWARD);
  motor2.setSpeed(200);
}

void reverse()
{
  motor1.run(BACKWARD);
  motor1.setSpeed(175);
  motor2.run(BACKWARD);
  motor2.setSpeed(175);
}

void brake()
{
  motor1.setSpeed(0);
  motor1.run(BRAKE);
  motor2.setSpeed(0);
  motor2.run(BRAKE);
}

void brakerelease()
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

void left()
{
  motor1.run(FORWARD);
  motor1.setSpeed(175);
  motor2.run(BACKWARD);
  motor2.setSpeed(200);
}

void right()
{
  motor1.run(BACKWARD);
  motor1.setSpeed(175);
  motor2.run(FORWARD);
  motor2.setSpeed(200);
}

It controls M1 and M2.

Tom... :slight_smile:

Thanks heaps for that Tom. I'll be trying to run that tomorrow and see if the board comes alive.

I'm a bit confused that the top comment line says "DC motor on M3 and M4" but your comment at the bottom says "It controls M1 and M2"

Oh, well. I'll find out tomorrow. (It's 10.30 pm here).

But if it DOES work I'll have another rather different question for you.

Thanks again

Aussiewill

Hi, mate.

Yes I adapted it for my use, and changed M3 and M4 to M1 and M2.

Tom.. :slight_smile:

Hi Tom,
Genius!!
Thanks very much, works like a charm and has taught me about 2 way serial as well. I hate being the newbie, 20 to 30 years ago I was in your position, but all with the then popular 6502 chips.(Atari)

Of course now I want to substitute my own code and to do that I would really like to understand what the .h files really are.
Arduino IDE won't open them and nothing else makes much sense, so can you give me a pointer please.

A grateful Aussiewill

Hi, good stuff mate.

The .h files can be opened in text editor.

If you google how to make arduino library

A number of links will come up showing you how they work and how to make your own.

For the driver board you have, I would be sticking with that Adafruit library.
If you look in Examples and find that library example, you can see how it works with the many code examples.

Tom... :slight_smile:
PS I cut my teeth on Z80.

Hi,
This may help too, its Adafruit tutorial on all the stuff the library can do with that board.

Adafruit refer to it as their V1 board which is now obsolete, because it uses the comparatively inefficient L293 motor drivers.

Tom... :slight_smile:

Hi Tom,
Yeah. Been through that various times but didn't really glean useful info about just running a single motor.
But I'm stupid for not trying to use a simple text editor.
Obviously too basic an idea for my mighty brain!
Thanks once again Tom. More Karma has been added.

Aussiewill

Hi, mate.
I've added karma to you for being patient with our help.

Tom... :slight_smile:
Happy New Year

Hi Tom, me again. More basic questions.

I assume the line " if (Serial.available() > 0)" will just sit and wait forever until the serial monitor comes online??? Can't see what else but have found amazingly little detailed info on that.
Also, do I think correctly that after the switch/case statement you loop back to await the next serial input and that is the end of the void loop() statement. ? Can't believe that after honestly quite a lot of research (in obviously the wrong places) I hadn't come across that possibility EVER!
PLENTY of posts saying "NEVER use GOTOs, you'll create spaghetti code" etc.

I'm assuming that this example of switch/case/break is the equivalent of the old BASIC if/then/goto (or maybe "gosub"). And that all the routines such as forward/reverse/brake etc. are basically the equivalent of subroutines?? Seems to make sense.?
Again the arduino reference is amazingly unhelpful and forum questions don't really seem to address it too well.

Have transferred bits of your brilliant code to my program but don't have a very deep understanding of it yet.
Unless you object strenuously, I'm going to finish my sketch and post it here so you (and others) can point out all the things I did wrong and all the shortcuts that would be possible etc. etc.

It seems to be my best way to learn these days..

Can I plead for enlightenment?

Again?

Aussiewill

aussiewill:
Hi Tom, me again. More basic questions.

I assume the line " if (Serial.available() > 0)" will just sit and wait forever until the serial monitor comes online??? Can't see what else but have found amazingly little detailed info on that.

Yes, it checks the serial in buffer, if empty it returns the value of "0", if there is any data in it, it returns the value "1"
and goes through the SerialRead process.

Also, do I think correctly that after the switch/case statement you loop back to await the next serial input and that is the end of the void loop() statement.

Yes, when the switch/case has finished it automatically loops back to the start of the void loop().
It keeps looping but because "Serial.available()" is "0" until the buffer has something in it, it jumps around the "SerialRead".

This code only accepts one character, not a many character type command.

Switch/case is a much more efficient way of the if..else.. if.. nesting, and is easier to read and debug.

You will notice this line.

 if (inByte != oldinByte)

It checks to see if the last command has changed.
If it hasn't then it does not do the switch/case, this just makes the loop more efficient and stops the monitor screen from continually updating.

Tom... :slight_smile:

Hi Tom,
Many thanks again.

Only just now on re reading the post did I notice your comment about starting on a Z80.
Is my memory correct that that was the teensy weensy Sinclair Z80?? And I bet you never had so much fun!

My very first one was a cobbled together (by someone else) affair with a hexadecimal input pad and, 2K of RAM and NO way to store, not even tape.
Enter the code straight in-enter code-enter code-enter and so on.
The only thing it could do really was sound a buzzer or if you literally had all day, you could make it sound like a siren.
Wore thin quite quickly. A real dead end.
Then the Atari came out and that was it for me. First Basic, then 6502 programming, then I developed replacement OS chips that allowed breaking into programs etc.
Hardware enhancements such as memory expansions, quadrupling disk drive IO and various other gadgets.

Ah, those were the days, I actually knew what I was doing then.

Thanks again

Aussiewill

Hi, mate.

My machine code was taught on the Z80 evaluation board, I still have one I got from local UNI when they were surplus to their requirements.
Tape recorder backup and S100 bus.

But before then, High School, HPBasic on these cards.

You would mark your instructions with 2B pencil.
The computer we used was 50kms away, so cards were dispatched on bus first day, loaded and run the next at an "Institute of Advanced Education"as they called them then, third day it came back on bus.
The you looked to see if it ran, or didn't, if you had made any cad errors.
Frustrating but to keen teenagers it was top technology.

Tom.. :slight_smile:

Hi Tom,
Wow, THAT was doing it the hard way!
How many cards would it take to achieve even a basic instruction?
I remember early computers running entirely on punchcards, but I also remember there'd be a huge stack in the card reader for even simple operations.

I think you need to be congratulated for your perseverance!

Happy New Year

Aussiewill